How can I count the number of connected components in a map
6 views (last 30 days)
Show older comments
I am working on a project which requires finding number of connected components in binary image of map. I applied bwconncomp() function but its giving the wrong result. The code I am using is :
A=imread('contours.jpg'); figure,imshow(A); title('Original Image'); n = bwconncomp(A,8) fprintf('\nnumber of components = %.0f\n',n.NumObjects);
output is coming as : number of components = 113
Please tell me where I am making mistake. I am new to MatLab.
1 Comment
Image Analyst
on 21 Sep 2015
How many do you see? All the contours are connected by the white frame, so that's 1. The 700 and 800 each have 3 individual blobs. So that's 7 in total. Is 7 what you want?
Accepted Answer
Image Analyst
on 21 Sep 2015
I'm only getting 14 blobs with this code, where I cropped off the outer white frame:
% Demo to do blob counting.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a demo image.
folder = pwd % or wherever the image lives
baseFileName = 'contours.jpg';
%===============================================================================
% Read in a color reference image.
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(grayImage);
if size(grayImage, 3) > 1
grayImage = rgb2gray(grayImage);
end
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
% Threshold the image
binaryImage = grayImage > 127;
% Crop the image
binaryImage = binaryImage(9:310, 10:387);
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Label the image
[labeledImage, numBlobs] = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'Area');
allAreas = [measurements.Area]
% Let's assign each blob a different color to visually show the user the distinct blobs.
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% coloredLabels is an RGB image. We could have applied a colormap instead (but only with R2014b and later)
subplot(2, 2, 3);
imshow(coloredLabels);
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
caption = sprintf('Pseudo colored labels, from label2rgb().\n%d Blobs are numbered from top to bottom, then from left to right.', numBlobs);
title(caption, 'FontSize', fontSize);
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!