Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bounding box

448 views
Skip to first unread message

meena rao

unread,
Dec 22, 2010, 2:06:05 AM12/22/10
to
can anyone pls help with how to draw a bounding box for an irregular shaped image.

Yumnam Kirani

unread,
Dec 22, 2010, 2:14:05 AM12/22/10
to
You look for minimum enclosing rectangle in Matlab Forum for your problem. I hope you will get it.

Yumnam Kirani Singh
Tronglaobi Awang Leikai


"meena rao" <meen...@yahoo.co.in> wrote in message <ies80t$ph9$1...@fred.mathworks.com>...

ImageAnalyst

unread,
Dec 22, 2010, 10:51:16 AM12/22/10
to
On Dec 22, 2:06 am, "meena rao" <meenar...@yahoo.co.in> wrote:
> can anyone pls help with how to draw a bounding box for an irregular shaped image.

-------------------------------------------------------
To *draw* it you can use the line() or plot() function. You of course
have to know the coordinates of the bounding rows and columns. If you
don't know that then you have to do some image segmentation, namely
using regionprops to get the BoundingBox property of the object.

See my BlobsDemo for an example of image segmentation and cropping out
of the image the various objects inside their bounding boxes:
http://www.mathworks.com/matlabcentral/fileexchange/25157

meena rao

unread,
Dec 22, 2010, 7:52:04 PM12/22/10
to
Thanks, i will check the blobsdemo. I actually wanted to draw the bounding box for a silhouette and compute the aspect ratio signals (width/height).

ImageAnalyst <imagea...@mailinator.com> wrote in message <23a83424-16ca-4715...@s4g2000yql.googlegroups.com>...

ImageAnalyst

unread,
Dec 22, 2010, 8:23:27 PM12/22/10
to
% Find objects in image and put bounding boxes around them.
% Also calculate areas and aspect ratios.
% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

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.
fontSize = 20;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Program Files\MATLAB\R2010a\toolbox\images\imdemos';
baseFileName = 'coins.png';
fullFileName = fullfile(folder, baseFileName);
% 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 = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.

% Binarize the image.
binaryImage = grayImage > 100;
% Display the image.
subplot(2, 2, 3);
binaryImage = imfill(binaryImage, 'holes');
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);

% Label each blob so we can make measurements of it
[labeledImage numberOfBlobs] = bwlabel(binaryImage, 8);
% Get all the blob properties.
blobMeasurements = regionprops(labeledImage, 'BoundingBox',
'Area');
allBlobAreas = [blobMeasurements.Area];

% Display the original gray scale image.
subplot(2, 2, 4);
imshow(grayImage, []);
title('Original Grayscale Image with Bounding Boxes', 'FontSize',
fontSize);

% Loop through all blobs, putting up Bounding Box.
hold on; % Prevent boxes from blowing away the image and prior boxes.
for k = 1 : numberOfBlobs
boundingBox = blobMeasurements(k).BoundingBox; % Get box.
x1 = boundingBox(1);
y1 = boundingBox(2);
x2 = x1 + boundingBox(3) - 1;
y2 = y1 + boundingBox(4) - 1;
verticesX = [x1 x2 x2 x1 x1];
verticesY = [y1 y1 y2 y2 y1];
% Calculate width/height ratio.
aspectRatio(k) = boundingBox(3) / boundingBox(4);
fprintf('For blob #%d, area = %d, aspect ratio = %.2f\n', ...
k, allBlobAreas(k), aspectRatio(k));
% Plot the box in the overlay.
plot(verticesX, verticesY, 'r-', 'LineWidth', 2);
end
msgbox('Done with demo. See command window for areas and aspect
ratios.');

meena rao

unread,
Dec 23, 2010, 5:02:05 AM12/23/10
to
Hi
Thanks for the code. It worked. Is it possible to plot the aspect ratio as i need to do the gait period analysis.

ImageAnalyst <imagea...@mailinator.com> wrote in message <9e8438b3-e6bd-43f1...@w2g2000yqb.googlegroups.com>...

ImageAnalyst

unread,
Dec 23, 2010, 9:55:31 AM12/23/10
to
Of course. Just use the plot() function.

nurul

unread,
Mar 9, 2011, 2:53:05 AM3/9/11
to
"meena rao" <meen...@yahoo.co.in> wrote in message <iev6mt$8um$1...@fred.mathworks.com>...


I've try this code. How I want to know height of object???

ImageAnalyst

unread,
Mar 9, 2011, 6:10:01 AM3/9/11
to
On Mar 9, 2:53 am, "nurul " <niy2...@gmail.com> wrote:
> I've try this code.  How I want to know height of object???
-------------------------------------------------------------------------
The height is BoundingBox(4).

nurul

unread,
Mar 9, 2011, 9:18:05 PM3/9/11
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <24f85b49-f6aa-483c...@q40g2000prh.googlegroups.com>...

So, that's mean width for the object is BoundingBox(3)? Is it true?

ImageAnalyst

unread,
Mar 9, 2011, 9:27:50 PM3/9/11
to
On Mar 9, 9:18 pm, "nurul " <niy2...@gmail.com> wrote:
> So, that's mean width for the object is BoundingBox(3)? Is it true?
------------------------------------------------------------------------------------------
nurul :
No. The mean width is not the width of the bounding box. If I put a
circle in a box, the bounding box width is the diameter. But is the
mean width of the circle in the horizontal direction the diameter?
No, of course not. The circle's width is the diameter at only one
place - at one and only one line. For most of the lines the circle
has a shorter width, even going to down close to zero at the very top
and very bottom line.
ImageAnalyst

Harsha Vardhan Rao Avunoori

unread,
Jul 24, 2011, 1:14:08 PM7/24/11
to
Hello Mr.Image Analyst,

I wrote this code on a foreground separated video (separated foreground using mixture of Gaussian method) and I intend to plot the centroid and bounding box,velocity of the moving object in the video( velocity of one connected component = distance(connected component in second frame,connected component in first frame)/fps.)Though I have not written the code for displaying velocity on the image.I wrote the code for bounding box and centroid.Please have a look at this and the video which I have is 100 frames.

clear all
close all
clc
tic
disp('Execution Started')
vidobj = VideoReader('mixture_of_gaussians_output1.avi');
NumFrames = vidobj.NumberOfFrames;
frames = cell(1,NumFrames);
bwframe = cell(1,NumFrames);
cc = cell(1,NumFrames);


for m=1:NumFrames
frames{m} = rgb2gray(read(vidobj,m));
end

[r c] = size(frames{20});

for m = 1:NumFrames

level = max(frames{m}(:));

for n = 1:r
for o = 1:c

if frames{m}(n,o)> level-10 && frames{m}(n,o) <= level+10
frames{m}(n,o) = 255;
else
frames{m}(n,o) = 0;

end

end
end
bwframe{m} = frames{m};
end

figure,

for m=1:NumFrames
imshow(bwframe{m})
hold on
aropn{m} = bwareaopen(bwframe{m},15);
bw{m} = bwconncomp(aropn{m},8);
stats{m} = regionprops(bw{m},'BoundingBox','Centroid');


for object = 1:length(stats{m})-1
bb = stats{m}(object).BoundingBox;
bco = stats{m}(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bco(1),bco(2),'-m+')
a = text(bco(1)+15,bco(2),strcat('X: ',num2str(round(bco(1))),' Y: ',num2str(round(bco(2)))));
set(a,'FontName','Arial','FontWeight','normal','FontSize',12,'Color','blue');
ha = gca;
set(ha,'xdir','normal')
set(ha,'ydir','reverse')
set(ha,'xlim',[1 c])
set(ha,'ylim',[1 r])
grid on


end

end


hold off
toc
disp('Execution Ended')


So I would like to plot the image frame first then the bounding box and the value of the centroid (and velocity of the object in the future) and the user who executes my code should be able to see this as a video (earlier I have used im2frame to convert image into video so intially I want to plot image,bounding box and centroid all together)

Kindly help me out,
-Harsha

ImageAnalyst

unread,
Jul 24, 2011, 1:18:39 PM7/24/11
to
On Jul 24, 1:14 pm, "Harsha Vardhan Rao Avunoori"

<harsha.avuno...@hotmail.com> wrote:
> Hello Mr.Image Analyst,
>
> I wrote this code on a foreground separated video (separated foreground using mixture of Gaussian method) and I intend to plot the centroid and bounding box,velocity of the moving object in the video( velocity of one connected component = distance(connected component in second frame,connected component in first frame)/fps.)Though I have not written the code for displaying velocity on the image.I wrote the code for bounding box and centroid.Please have a look at this and the video which I have is 100 frames.
>
[code snipped]

>
> So I would like to plot the image frame first then the bounding box and the value of the centroid (and velocity of the object in the future) and the user who executes my code should be able to see this as a video (earlier I have used im2frame to convert image into video so intially I want to plot image,bounding box and centroid all together)
>
> Kindly help me out,
> -Harsha

---------------------------------------------------------------------------------
And where did you upload "'mixture_of_gaussians_output1.avi"?

By the way, you should have started your own thread rather than add on
to someone else's unrelated thread. Do that for your response.

donny jason

unread,
Sep 30, 2011, 3:36:13 AM9/30/11
to
Sir,
Can Sir explain the following:

boundingBox = objects(i).BoundingBox; % Get box.
x1 = boundingBox(1);
y1 = boundingBox(2);
x2 = x1 + boundingBox(3) - 1;
y2 = y1 + boundingBox(4) - 1;

for the above, why x1 bounding box(1) and x2 for boundingbox(2)?
why bounding box(3) needs to minus 1 and so do boundingbox(4)?

verticesX = [x1 x2 x2 x1 x1];
verticesY = [y1 y1 y2 y2 y1];

wat is the above for?

% Calculate width/height ratio.
aspectRatio(k) = boundingBox(3) / boundingBox(4);
why the above command only divide the boundingbox(3) and boundingbox(4) only and not boundingbox(1) and boundingbox(2)

Thank you Sir.

bhushanp...@gmail.com

unread,
Sep 29, 2014, 4:17:53 AM9/29/14
to
Respected mam,
Mam im currently working on vehicle detection from satellite image. Mam im detecting the vehicles form the main image but facing problem for mark the rectangle around it . Please help .
0 new messages