how to extract boundaries of connected components and display one by one

29 views (last 30 days)
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
Rik
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
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?

Sign in to comment.

Answers (1)

Image Analyst
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
QuestionsAccount on 25 Jun 2020
hi Image Analyst thanx for your help and support i somehow mange to find the 4 extreme ponts now the only point remains is left leg point
actually i need your help in "how to find the white pixel that has minimum x and minmum y cordinates." i want to point out the position of that pixel that is represented by minimum x and minimum y cordinate. what i want to say is explained in attach figure.plz give a suggestion how i code this to achive my goal or how i will have to run the loop for desired goal.
plz help me out image analyst.
Image Analyst
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)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!