how to extract boundaries of connected components and display one by one
Show older comments
Hellow Everyone.
I preprocessed my image and got connected components now i want to only extract boundaries of that connected components and want to display them one by one on screen and i also want to find out their centers and then distance from center to the end points of that connected components.
i used regionprops, bwboundaries and bwlabel but i cant do it, may be i m doing some kind of mistake please help me out of doing all these above mentioned tasks. it will be heighly appricated.
7 Comments
QuestionsAccount
on 8 Jun 2020
QuestionsAccount
on 9 Jun 2020
Rik
on 9 Jun 2020
Youd obviously did not read the advice in the thread I linked, so why should I bother spending time on your problem if you are going to ignore my advice.
Prove me wrong and I will try to help you.
QuestionsAccount
on 9 Jun 2020
Rik
on 9 Jun 2020
I'm fine with ignoring your question, but then I don't really get why you posted it in the first place. If everybody should ignore your post, why did you post it?
Anyway, since you have x and y values, you can use a function like pdist, or calculate all pair-wise differences in a loop. Since you didn't share any input data, I can't give you concrete advice.
Image Analyst
on 9 Jun 2020
Are the connected components the white blobs inside the black outline? If so, you really don't want the huge white surround going all the way out to the edge of the image do you?
Answers (1)
Image Analyst
on 10 Jun 2020
Try this:
% Demo to find centroid of blobs and draw a line from the centroid to the farthest boundary point.
% By Image Analyst
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 = 22;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'Screenshot (125).png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION OF IMAGE
% Get a binary image
binaryImage = imbinarize(grayImage);
% Get rid of huge white frame surrounding the image the user posted.
binaryImage = imclearborder(binaryImage);
subplot(2, 2, 2);
imshow(binaryImage, []);
impixelinfo;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
%--------------------------------------------------------------------------------------------------------
% CONNECTED COMPONENTS LABELING
se = strel('disk', 1, 0);
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 4);
subplot(2, 2, 3);
imshow(labeledImage, []);
impixelinfo;
title('Labeled Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
subplot(2, 2, 4);
imshow(coloredLabelsImage);
title('Pseudocolored Labeled Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
%--------------------------------------------------------------------------------------------------------
% MEASUREMENT OF CENTROIDS.
props = regionprops(labeledImage, 'Centroid');
xy = vertcat(props.Centroid);
xCentroid = xy(:, 1);
yCentroid = xy(:, 2);
%--------------------------------------------------------------------------------------------------------
% MEASUREMENT OF BOUNDARIES
boundaries = bwboundaries(binaryImage, 4);
hold on;
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
x = thisBoundary(:, 2);
y = thisBoundary(:, 1);
plot(xCentroid(k), yCentroid(k), 'r*');
% Find distances
distances = sqrt((xCentroid(k) - x) .^ 2 + (yCentroid(k) - y) .^ 2);
% Find out which boundary point is the farthest from the centroid.
[maxDistance, indexOfMax] = max(distances);
xFar = x(indexOfMax);
yFar = y(indexOfMax);
% Draw a line between them.
line([xCentroid(k), xFar], [yCentroid(k), yFar], 'Color', 'y', 'LineWidth', 2);
end

See the yellow lines going from each blob's centroid to the farthest point on the boundary.
9 Comments
QuestionsAccount
on 10 Jun 2020
Edited: QuestionsAccount
on 10 Jun 2020
QuestionsAccount
on 12 Jun 2020
Image Analyst
on 12 Jun 2020
That doesn't look too robust. I suggest you look at openpose instead. Google it.
QuestionsAccount
on 13 Jun 2020
Image Analyst
on 13 Jun 2020
For the most general type of blob, it could be difficult and I'm not taking on private consulting contracts now. So that's why I told you to look at openpose where very smart people have spent years solving the problem. If you don't want to do that then about all I can suggest is that you look at my shape recognition demos. One of them uses findpeaks() to determine the polygon.
QuestionsAccount
on 14 Jun 2020
Image Analyst
on 14 Jun 2020
He asked how to get the extremum points and using convhull() will return those. However there is a problem with that. For example, you could have an arm of a star that is too short to be on the convex hull. Or you could have multiple points that are completely adjacent to each other. In that case, you'd probably just want the middle point. If you had 20 points and wanted the 5 extrema then it might identify all 5 that are in an adjacent part and not give you the other 4 that are widely spaced from those. So there are some problems with that method. The findpeaks() method is more robust than that. But findpeaks() is tricky - just look at the diagram with all the different kinds of peaks you find. You have to tweak all those parameters to get the kinds of peaks that you want. I'd do no better at that than you. Plus, once you have found valid peaks, you have to find the "best" 5 (if you want only 5). And that could be a whole other set of rules. It's a lot of work. Work that the openpose people have probably already worked through.
QuestionsAccount
on 25 Jun 2020
Image Analyst
on 25 Jun 2020
That's not well defined. You might have a pixel that is the lowest in y but not left-most in x. Perhaps you'd just like to find out the (x,y) of pixel that is closest to the lower left corner of the image.
[y, x] = find(binaryImage); % Make sure you get the order right. It's [y, x], NOT [x,y].
[rows, columns] = size(binaryImage)
distances = sqrt((x - 1).^2 + (y - rows).^2);
[minDistance, indexOfMin] = min(distances)
xLowerLeft = x(indexOfMin)
yLowerLeft = y(indexOfMin)
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!