Can we plot the boundary of an image??

I am working on a project of object detection (Human). I have segmented the foreground and have calculated boundaries and centroid of the object. I want to connect the centroid to extreme points like hands, legs and head (in shape of a star). Can anyone help me how to find and connect those extreme points to centroid. Thanks in advance for your help

 Accepted Answer

Use bwboundaries() to get the coordinates of the outline of your blob (human). Then pass them in to convhull() to get the convex hull, which is the official name of the extreme points you're talking about.

5 Comments

Thanks alot for your answer. IS it possible to plot this convex hull. If yes. How? And how can i connect these points to centroid.
Of course. Try this untested code.
boundaries = bwboundaries(binaryImage);
x = boundaries(:, 2);
y = boundaries(:, 1);
indexes = convhull(x, y);
for k = indexes
line([x(k), xCentroid], [y(k), yCentroid], 'Color', 'r', 'LineWidth', 2);
end
I tried this segment of code. the statement x = boundaries(:, 2); gives error (index exceeds matrix dimension). when i change it with x = boundaries(:, 1); then the line: indexes = convhull(x, y); gives error (Error using convhull Invalid arguments - the input does not match the allowable argument specification.)
I am attaching an image,i need something like this
Try with
x = boundaries{1}(:,2);
y = boundaries{1}(:,1);
The documentation indicates
bwboundaries returns B, a P-by-1 cell array, where P is the number of objects and holes. Each cell in the cell array contains a Q-by-2 matrix. Each row in the matrix contains the row and column coordinates of a boundary pixel. Q is the number of boundary pixels for the corresponding region.
You might want to use the 'noholes' option.
Convex hull will give you too many points for that. You need to choose just a few, like 5. I know people have done this and you have probably seen the papers. If not, look here in VIsion Bib for papers on pose and gait analysis. They will tell you how to do it better than me because they have worked for years in the area and published papers on what you want to do, and I haven't.

Sign in to comment.

More Answers (2)

You might be able to take advantage of the Extrema property of regionprops

7 Comments

I have tried to use 'extrema', however, its not what i am looking for. i need the to plot the image boundaries to get the peak points. I need maximum five peaks(head, two hands and two legs).
You have the difficulty that the outlying points move around and exchange positions. You need to track left leg separately from right leg, for example, and that means you need to distinguish whether the leg that is "more x" is the left or right leg. That calls for tracking, not just static analysis. For example an arm might be at the same location as the leg during the swing, so there might be fewer than 5 major outlying points in any one image. When you don't give us this kind of information in advance, anything we suggest is not likely to work.
Image Analyst I am sorry for commenting so late due to some problems. Can you tell me what i am doing wrong. I am getting error "dimension of matrices concatenated are not consistent". Below is just an example code for testing.
I= imread('silhouete.jpg');
BW= im2bw(I);
s= regionprops( BW,'centroid');
centroids = cat(1, s.Centroid);
Centroid= centroids';
boundaries = bwboundaries(BW,'noholes');
x = boundaries{1}(:,2);
y = boundaries{1}(:,1);
indexes = convhull(x, y);
for k = indexes
line([x(k), Centroid(1)], [y(k),Centroid(2) ], 'Color', 'r', 'LineWidth', 2);
end
indexes is an array, so x(k) is also an array. Try it this way.
for k = 1 : length(indexes)
line([x(indexes(k)), Centroid(1)], [y(indexes(k)),Centroid(2) ], 'Color', 'r', 'LineWidth', 2);
end
Thanks alot for your prompt reply. The code now works fine, even i am getting the lines but its continuously giving me warning " Warning: This concatenation operation includes an empty array with an incorrect number of rows. Concatenation including empty arrays will require all arrays to have the same number of rows in a future release." And the code than crashes giving the same previous error " Error using horzcat Dimensions of matrices being concatenated are not consistent. " Can you tell me what is the problem?
Not really sure. Why don't you attach 'silhouete.jpg' so we can run your code with your actual image?
With this image, its not giving correct output, i don't know why. But with my human detection video its is working well, only it crashes and is giving warnings. U can test on this image, i hope u figure it out. I will be really thankful

Sign in to comment.

Palwasha:
I fixed your code. The main problem is that your binarized image doesn't have the man as the white foreground. Your surrounding background is actually considered the foreground so I had to invert your image. Here is the fixed code:
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;
grayImage = imread('silhouete.jpg');
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 1, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% 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 the image.
binaryImage = ~im2bw(grayImage);
% Display the original gray scale image.
subplot(2, 1, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Label the image
labeledImage = bwlabel(binaryImage);
s= regionprops(labeledImage, 'Centroid');
centroids = [s.Centroid];
Centroid= centroids';
boundaries = bwboundaries(binaryImage, 'noholes');
x = boundaries{1}(:,2);
y = boundaries{1}(:,1);
indexes = convhull(x, y);
% Plot the convex hull in magenta
hold on;
plot(x(indexes), y(indexes), 'm-', 'LineWidth', 2);
% Plot lines from the centroid to the convex hull vertices.
for k = 1 : length(indexes)
line([x(indexes(k)), Centroid(1)], [y(indexes(k)),Centroid(2) ], 'Color', 'r', 'LineWidth', 2);
end

3 Comments

Thanks alot Image Analyst. I really appreciate your help. Can we reduce to only 5 lines i.e. one for head, one, one for each hand and one, one for legs. How to choose those specific lines??
I don't know, but there are papers on that here: http://www.visionbib.com/bibliography/contents.html. Pick one and code it up.
OK,and once again thanks alot

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!