How can I determine the number of closed regions in an edge image?

Given an edge image as shown in the figure, how can I find the number of closed regions (4 in the figure) in it?

 Réponse acceptée

Trivial - just 3, or even 2, lines of code. Just try this:
% Invert the image to form black curves on a white background
binaryImage = ~binaryImage;
% Get rid of huge background that touches the border
binaryImage = imclearborder(binaryImage);
% Count the objects remaining
[~, numberOfClosedRegions] = bwlabel(binaryImage);

4 commentaires

coolguynp
coolguynp le 20 Avr 2015
Modifié(e) : coolguynp le 20 Avr 2015
Thanks for the answer. It's a clever idea. However, it did not work for the image provided because there are some noise. Could you suggest any workaround for such images?
These are the resulting images after binary inversion and background border clearance.
Thanks.
Looks like the image was not the result of a calculation but gotten after reading in some image that was jpeg'ed with horrible compression artifacts. That's why you NEVER use jpeg format when doing image analysis. Don't do that. Use the original binary image.
Try this code, where I threshold to get rid of the artifacts.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 20;
% Read in the horribly jpegged image
binaryImage = imread('sample.jpg');
[rows, columns, numberOfColorChannels] = size(binaryImage);
% Display the original image.
subplot(2, 2, 1);
imshow(binaryImage, []);
title('Original Binary Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Threshold to get rid of jpeg artifacts
binaryImage = binaryImage > 127;
% Invert the image to form black curves on a white background
binaryImage = ~binaryImage;
% Display the final image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Intermediate Binary Image', 'FontSize', fontSize);
% Get rid of huge background that touches the border
binaryImage = imclearborder(binaryImage, 4);
% Display the final image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Final Binary Image', 'FontSize', fontSize);
% Count the objects remaining
[~, numberOfClosedRegions] = bwlabel(binaryImage);
message = sprintf('There are %d closed regions.', numberOfClosedRegions);
uiwait(helpdlg(message));
Thank you for the detailed answer. The small blob at the bottom was missing because of small opening. bwmorph with 'bridge' after obtaining the binary image solved that as well.
binaryImage = bwmorph(binaryImage, 'bridge');
The small blob at the bottom was SUPPOSED to be eliminated because it is not a closed curve.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by