I want to make bounding box

I want to make a bounding box in the area of the eye in this picture, so I can find the midpoint of the eye area, but I do not know how to make it. I was expecting help from the experienced. thanks in advance.
this is the picture:
href="http://www.freeimagehosting.net/3z35z"<img src="http://www.freeimagehosting.net/t/3z35z.jpg"></a>

 Accepted Answer

Image Analyst
Image Analyst on 7 Dec 2012
I have the perfect demo for you: BlobsDemo http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 It has a bunch of blobs on the screen (coins) and then finds the centroids of them and cuts them out, using the bounding box, to new smallsub images.
But basically, you call regionprops and ask for 'BoundingBox'. Then use imcrop or regular indexing to extract the box containing your blob.

12 Comments

Your code is too experience. But, i just wanna know, what did you mean with regular indexing ? I'm amateur in matlab, but thanks in advance for your help Mr.Image Analyst. :)
Come on - you're a smart engineer. You can follow a demo that's well commented at every single step can't you? Try again.
Regular indexing means
croppedImage = fullImage(row1:row2, column1:column2);
Of course you have to know what row1, row2, column1, and column2 are. The other alternative is to use imcrop():
croppedImage = imcrop(fullImage, [column1, row1, width, height]);
but in that case you need to know column1, row1, width, and height.
Lidank Abiel
Lidank Abiel on 10 Dec 2012
Edited: Lidank Abiel on 10 Dec 2012
I've try to follow your demo, and now, i have a question I've been getting coordinate the midpoint of the eye area, using your code : if true % for k = 1 : numberOfBlobs blobCentroid = s(k).Centroid; fprintf('#%2d %17.0f %11.0f \n', k, blobCentroid); end "
how i can to save the result in a variable ? it is possible to draw a line from 1 coordinate to another coordinate ?
thanks in advance Mr.Image Analyst.
Please just run this code. Make sure you change the name/folder of the image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Read in a Lindank's image.
folder = 'C:\Users\Lindank\Documents';
baseFileName = 'eyes.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- 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 in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage)
% Convert to grayscale
if numberOfColorBands > 1
grayImage = grayImage(:, :, 2);
end
binaryImage = grayImage > 128;
% Display the original gray scale image.
% subplot(2, 2, 1);
imshow(binaryImage, []);
title('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')
hold on;
labelShiftX = 5;
% Label
labeledImage = bwlabel(binaryImage, 8); % Label each blob so we can make measurements of it
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, 'Centroid', 'BoundingBox', 'Area');
numberOfBlobs = size(blobMeasurements, 1);
% Loop over all blobs printing their measurements to the command window.
for k = 1 : numberOfBlobs % Loop through all blobs.
blobArea = blobMeasurements(k).Area; % Get area.
blobBoundingBox = blobMeasurements(k).BoundingBox; % Get Bounding box.
% Plot bounding box for this blob.
x1 = blobBoundingBox(1);
y1 = blobBoundingBox(2);
x2 = x1 + blobBoundingBox(3);
y2 = y1 + blobBoundingBox(4);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
plot(x, y, 'r-');
% Get centroid for this blob.
blobCentroid = blobMeasurements(k).Centroid; % Get centroid one at a time
plot(blobCentroid(1), blobCentroid(2), 'r+', 'MarkerSize', 13);
fprintf(1,'#%2d %17.1f %11.1f\n',...
k, blobArea, blobCentroid);
% Put the "blob number" labels on the "boundaries" grayscale image.
text(blobCentroid(1) + labelShiftX, blobCentroid(2), num2str(k), 'FontSize', fontSize, 'FontWeight', 'Bold');
end
I got the result. It work for get the midpoint of the eye. Now, i just trying to search how to draw a line from 1 point to another point using the result.
Use the line() or plot() function if you want a line drawn in the overlay.
It work Mr.Image Analyst. :). did u want to see program what i made ?
No but thanks anyway. Glad it's working for you. Just go ahead and mark the question as "Answered".
ok. thank you so much for your help.
sorry, I forgot to ask you something. I do not understand %2d %17.0f %11.0f. This is a formula or anything ?
That's a format specifier string. I guess you don't know C or C++ or C# do you? It's the same as in those languages. % means that you stick a variable there. The number after the % says how many spaces to allot for it. The first number (if present) is the total length, and the number after the . is the number of digits after the decimal place. Finally the letter is what class the variable is: f = floating point, d = integer, c = character, s = string. So %17.3f means a floating point number in a space of 17 characters with 3 of those 17 being to the right of the decimal place.
Thank you so much Mr.Image Analyst. Like i told to you, i'm so amateur in programming, but i hope it not prevent me to learn.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 7 Dec 2012
Edited: Matt J on 7 Dec 2012
You can use h=IMRECT if you want to draw the bounding box interactively. Then use createMask(h) to get a logical map of the region.

9 Comments

so, how i can get the midpoint and coordinat pixel in the bounding box ?.
Matt J
Matt J on 10 Dec 2012
Edited: Matt J on 10 Dec 2012
It's not clear to me what role you want the bounding box to play in your ultimate goals. However, the regionprops command can find things like centroids of regions.
I = imread('close up face.jpg'); I = rgb2gray(I); BW = imread('eye.jpg'); BW = im2bw(BW); s = regionprops(BW, I, {'Centroid','WeightedCentroid'}); imshow(I) hold on numObj = numel(s); for k = 1 : numObj plot(s(k).WeightedCentroid(1), s(k).WeightedCentroid(2), 'r*'); plot(s(k).Centroid(1), s(k).Centroid(2), 'bo'); end hold off s = regionprops(BW, I, {'Centroid','PixelValues','BoundingBox'}); imshow(I); hold on for k = 1 : numObj s(k).StandardDeviation = std(double(s(k).PixelValues)); text(s(k).Centroid(1),s(k).Centroid(2), ... sprintf('%2.1f', s(k).StandardDeviation), ... 'EdgeColor','b','Color','r'); end hold off sStd = [s.StandardDeviation]; lowStd = find(sStd < 50); imshow(I); hold on; for k = 1 : length(lowStd) rectangle('Position', s(lowStd(k)).BoundingBox, ... 'EdgeColor','y'); end hold off;
can imrect make such a detection box automatically in the search area?
thanks in advance Mr.Matt J
I can't read your code. Please use the "{} Code" menu tool to format it.
if true
% code
end
I = imread('close up face.jpg');
I = rgb2gray(I);
BW = imread('eye.jpg');
BW = im2bw(BW);
s = regionprops(BW, I, {'Centroid','WeightedCentroid'});
imshow(I)
hold on
numObj = numel(s);
for k = 1 : numObj
plot(s(k).WeightedCentroid(1), s(k).WeightedCentroid(2), 'r*');
plot(s(k).Centroid(1), s(k).Centroid(2), 'bo');
end
hold off
s = regionprops(BW, I, {'Centroid','PixelValues','BoundingBox'}); imshow(I);
hold on
for k = 1 : numObj
s(k).StandardDeviation = std(double(s(k).PixelValues)); text(s(k).Centroid(1),s(k).Centroid(2), ... sprintf('%2.1f', s(k).StandardDeviation), ... 'EdgeColor','b','Color','r');
end
hold off
sStd = [s.StandardDeviation];
lowStd = find(sStd < 50);
imshow(I);
hold on;
for k = 1 : length(lowStd)
rectangle('Position', s(lowStd(k)).BoundingBox, ... 'EdgeColor','y');
end
hold off;
Matt J
Matt J on 10 Dec 2012
Edited: Matt J on 10 Dec 2012
Well, I can't run your code because I don't have eye.jpg, etc..., but the image you posted here indicates that you are already able to segment the region of the eyes into 2 separate black & white regions. REGIONPROPS should work directly on that black and white map.
What exactly is the problem you still have, which the bounding box is supposed to fix? Is it a problem of automating the segmentation? How have you been doing the segmentation of the eyes so far?
I already gave him complete code that finds both the centroids and bounding boxes on his eyes image below in my answer. I haven't heard from him yet either.
i still try Mr.Image Analyst :)
Just copy, paste, change filename to make it valid, and run. It shouldn't take more than a few seconds to try it.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!