Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
bounding box

Subject: bounding box

From: meena rao

Date: 22 Dec, 2010 07:06:05

Message: 1 of 14

can anyone pls help with how to draw a bounding box for an irregular shaped image.

Subject: bounding box

From: Yumnam Kirani

Date: 22 Dec, 2010 07:14:05

Message: 2 of 14

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" <meenaraos@yahoo.co.in> wrote in message <ies80t$ph9$1@fred.mathworks.com>...
> can anyone pls help with how to draw a bounding box for an irregular shaped image.

Subject: bounding box

From: ImageAnalyst

Date: 22 Dec, 2010 15:51:16

Message: 3 of 14

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

Subject: bounding box

From: meena rao

Date: 23 Dec, 2010 00:52:04

Message: 4 of 14

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 <imageanalyst@mailinator.com> wrote in message <23a83424-16ca-4715-a259-6eb2647d3d6d@s4g2000yql.googlegroups.com>...
> 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

Subject: bounding box

From: ImageAnalyst

Date: 23 Dec, 2010 01:23:27

Message: 5 of 14

% 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.');

Subject: bounding box

From: meena rao

Date: 23 Dec, 2010 10:02:05

Message: 6 of 14

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 <imageanalyst@mailinator.com> wrote in message <9e8438b3-e6bd-43f1-bc04-5db5064fbfc0@w2g2000yqb.googlegroups.com>...
> % 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.');

Subject: bounding box

From: ImageAnalyst

Date: 23 Dec, 2010 14:55:31

Message: 7 of 14

Of course. Just use the plot() function.

Subject: bounding box

From: nurul

Date: 9 Mar, 2011 07:53:05

Message: 8 of 14

"meena rao" <meenaraos@yahoo.co.in> wrote in message <iev6mt$8um$1@fred.mathworks.com>...
> 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 <imageanalyst@mailinator.com> wrote in message <9e8438b3-e6bd-43f1-bc04-5db5064fbfc0@w2g2000yqb.googlegroups.com>...
> > % 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.');


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

Subject: bounding box

From: ImageAnalyst

Date: 9 Mar, 2011 11:10:01

Message: 9 of 14

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).

Subject: bounding box

From: nurul

Date: 10 Mar, 2011 02:18:05

Message: 10 of 14

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <24f85b49-f6aa-483c-980e-bde54353ba71@q40g2000prh.googlegroups.com>...
> 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).
 

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

Subject: bounding box

From: ImageAnalyst

Date: 10 Mar, 2011 02:27:50

Message: 11 of 14

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

Subject: bounding box

From: Harsha Vardhan Rao Avunoori

Date: 24 Jul, 2011 17:14:08

Message: 12 of 14

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

Subject: bounding box

From: ImageAnalyst

Date: 24 Jul, 2011 17:18:39

Message: 13 of 14

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.

Subject: bounding box

From: donny jason

Date: 30 Sep, 2011 07:36:13

Message: 14 of 14

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.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us