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 into video

Subject: Bounding box into video

From: Martin Skodacek

Date: 11 Jan, 2011 18:48:05

Message: 1 of 7

hello everybody, can anyone help me how to create put bounding box instead of centroid into this example??? I know how to get bounding box rectangle into videoframes.
http://www.mathworks.com/products/image/demos.html?file=/products/demos/shipping/images/ipextraffic.html



...code from web...
    if ~isempty([stats.Area])
        areaArray = [stats.Area];
        [junk,idx] = max(areaArray);
        % Centroid drawing
        c = stats(idx).Centroid;
        c = floor(fliplr(c));
        width = 2;
        row = c(1)-width:c(1)+width;
        col = c(2)-width:c(2)+width;
        taggedCars(row,col,1,k) = 255;
        taggedCars(row,col,2,k) = 165;
        taggedCars(row,col,3,k) = 0;

        % Bounding Box drawing
        bb = stats(idx).BoundingBox;
        bb = floor(bb);
        rectangle('Position', [bb(:,1) bb(:,2) bb(:,3) bb(:,4)], 'EdgeColor',[0 1 1],'LineWidth',2);

       % here something which creates structure like taggedCars above
end

but I don´t know how to place each rectangle into correspoding frame. I spend a lot of time on itand I am useless :/ Thank you for your help.

Subject: Bounding box into video

From: Sean de

Date: 11 Jan, 2011 19:05:05

Message: 2 of 7

"Martin Skodacek" wrote in message <igi8l5$kpl$1@fred.mathworks.com>...
> hello everybody, can anyone help me how to create put bounding box instead of centroid into this example??? I know how to get bounding box rectangle into videoframes.
> http://www.mathworks.com/products/image/demos.html?file=/products/demos/shipping/images/ipextraffic.html
>
>
>
> ...code from web...
> if ~isempty([stats.Area])
> areaArray = [stats.Area];
> [junk,idx] = max(areaArray);
> % Centroid drawing
> c = stats(idx).Centroid;
> c = floor(fliplr(c));
> width = 2;
> row = c(1)-width:c(1)+width;
> col = c(2)-width:c(2)+width;
> taggedCars(row,col,1,k) = 255;
> taggedCars(row,col,2,k) = 165;
> taggedCars(row,col,3,k) = 0;
>
> % Bounding Box drawing
> bb = stats(idx).BoundingBox;
> bb = floor(bb);
> rectangle('Position', [bb(:,1) bb(:,2) bb(:,3) bb(:,4)], 'EdgeColor',[0 1 1],'LineWidth',2);
>
> % here something which creates structure like taggedCars above
> end
>
> but I don´t know how to place each rectangle into correspoding frame. I spend a lot of time on itand I am useless :/ Thank you for your help.


Perhaps:
BBimg = false(bb([4 3]))
BBimg(:, [1 end]) = true;
BBimg([1 end],:) = true;
taggedCars(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3))) = double(BBimg).*255

%Not tested in MATLAB

Subject: Bounding box into video

From: Martin Skodacek

Date: 12 Jan, 2011 19:28:07

Message: 3 of 7

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <igi9l1$p6e$1@fred.mathworks.com>...
> "Martin Skodacek" wrote in message <igi8l5$kpl$1@fred.mathworks.com>...
> > hello everybody, can anyone help me how to create put bounding box instead of centroid into this example??? I know how to get bounding box rectangle into videoframes.
> > http://www.mathworks.com/products/image/demos.html?file=/products/demos/shipping/images/ipextraffic.html
> >
> >
> >
> > ...code from web...
> > if ~isempty([stats.Area])
> > areaArray = [stats.Area];
> > [junk,idx] = max(areaArray);
> > % Centroid drawing
> > c = stats(idx).Centroid;
> > c = floor(fliplr(c));
> > width = 2;
> > row = c(1)-width:c(1)+width;
> > col = c(2)-width:c(2)+width;
> > taggedCars(row,col,1,k) = 255;
> > taggedCars(row,col,2,k) = 165;
> > taggedCars(row,col,3,k) = 0;
> >
> > % Bounding Box drawing
> > bb = stats(idx).BoundingBox;
> > bb = floor(bb);
> > rectangle('Position', [bb(:,1) bb(:,2) bb(:,3) bb(:,4)], 'EdgeColor',[0 1 1],'LineWidth',2);
> >
> > % here something which creates structure like taggedCars above
> > end
> >
> > but I don´t know how to place each rectangle into correspoding frame. I spend a lot of time on itand I am useless :/ Thank you for your help.
>
>
> Perhaps:
> BBimg = false(bb([4 3]))
> BBimg(:, [1 end]) = true;
> BBimg([1 end],:) = true;
> taggedCars(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3))) = double(BBimg).*255
>
> %Not tested in MATLAB

Thank you Sean.

I used it as

        taggedCars2(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),1,k) = 255;
        taggedCars2(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),2,k) = 165;
        taggedCars2(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),3,k) = 0;

this creates me a new structure where tracked object is covered by orange rectangle. Is it possible to create just rectangle around tracked vehicle? I mean a bounding box consisting of lines around vehicle. Thank you...

Subject: Bounding box into video

From: Sean de

Date: 12 Jan, 2011 20:03:04

Message: 4 of 7


> > Perhaps:
> > BBimg = false(bb([4 3]))
> > BBimg(:, [1 end]) = true;
> > BBimg([1 end],:) = true;
> > taggedCars(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3))) = double(BBimg).*255
> >
> > %Not tested in MATLAB
>
> Thank you Sean.
>
> I used it as
>
> taggedCars2(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),1,k) = 255;
> taggedCars2(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),2,k) = 165;
> taggedCars2(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),3,k) = 0;
>
> this creates me a new structure where tracked object is covered by orange rectangle. Is it possible to create just rectangle around tracked vehicle? I mean a bounding box consisting of lines around vehicle. Thank you...

That's why I created the BBimg, it's just the border. However, I didn't implement it correctly. Try this:

%%%%
I = imread('peppers.png');
bb = [70 70 30 80];
M = false(size(I,1),size(I,2));
M(bb(2):bb(2)+bb(4)+1,[bb(1), bb(1)+bb(3)+1]) = true;
M([bb(2),bb(2)+bb(4)+1],bb(1):bb(1)+bb(3)+1) = true;
idx = find(M);
nM = numel(M);
I(idx) = 255;
I(idx+nM) = 165;
I(idx+nM*2) = 0;
imtool(I)
%%%%
It should be ready to go if you change I to your tagged cars.

-Sean

Subject: Bounding box into video

From: Sean de

Date: 12 Jan, 2011 20:19:04

Message: 5 of 7

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <igl1do$rdu$1@fred.mathworks.com>...
>
> > > Perhaps:
> > > BBimg = false(bb([4 3]))
> > > BBimg(:, [1 end]) = true;
> > > BBimg([1 end],:) = true;
> > > taggedCars(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3))) = double(BBimg).*255
> > >
> > > %Not tested in MATLAB
> >
> > Thank you Sean.
> >
> > I used it as
> >
> > taggedCars2(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),1,k) = 255;
> > taggedCars2(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),2,k) = 165;
> > taggedCars2(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),3,k) = 0;
> >
> > this creates me a new structure where tracked object is covered by orange rectangle. Is it possible to create just rectangle around tracked vehicle? I mean a bounding box consisting of lines around vehicle. Thank you...
>
> That's why I created the BBimg, it's just the border. However, I didn't implement it correctly. Try this:
>
> %%%%
> I = imread('peppers.png');
> bb = [70 70 30 80];
> M = false(size(I,1),size(I,2));
> M(bb(2):bb(2)+bb(4)+1,[bb(1), bb(1)+bb(3)+1]) = true;
> M([bb(2),bb(2)+bb(4)+1],bb(1):bb(1)+bb(3)+1) = true;
> idx = find(M);
> nM = numel(M);
> I(idx) = 255;
> I(idx+nM) = 165;
> I(idx+nM*2) = 0;
> imtool(I)
> %%%%
> It should be ready to go if you change I to your tagged cars.
>
> -Sean

Actually, since it's 4-dimensional, you'll probably have to have
nM = numel(M)*(k-1);
Or extract that RGB slice, set it with my method above, then reinsert it, i.e.

I = taggedcars(:,:,:,k);
above code
taggedcars(:,:,:,k) = I;

Subject: Bounding box into video

From: asoj16

Date: 2 Jul, 2012 04:50:28

Message: 6 of 7

who person have this full code with bounding box rectangle? can you give me? I need this code.please help me.
I dont know very much about mathlab cod.But i need this code for tracking car in mathlab with bounding box rectangle.i want when i recognize car then with one box around of that became determined.
please help me

Subject: Bounding box into video

From: Martin Skodacek

Date: 5 Jul, 2012 07:21:15

Message: 7 of 7

Hello, here is the source code, here you can show centroids or bounding boxes, just uncomment/comment particular code parts. Don´t forget to change input video and check diacritic when you copy this code to matlab... good luck

clc;close all;clear all;
%%info = mmfileinfo('traffic.avi');
%trafficObj = aviread('MEDIA\traffic.avi');%,1:numFrame
trafficObj = mmreader('MEDIA\traffic.avi', 'tag', 'traffic'); %traffic TrafficTunnel_15s

%% image pre-processing
TresholdValue = 50;
darkCar = rgb2gray(read(trafficObj, 1));
noDarkCar = imextendedmax(darkCar, TresholdValue);
figure, imshow(darkCar)
figure, imshow(noDarkCar)

sedisk = strel('disk',2);
noSmallStructures = imopen(noDarkCar, sedisk);
figure, imshow(noSmallStructures);

nframes = get(trafficObj, 'NumberOfFrames');
I = read(trafficObj, 1);
taggedCars = zeros([size(I,1) size(I,2) 3 nframes], class(I));

%% frame processing
for k = 1 : nframes
    singleFrame = read(trafficObj, k);

    % Convert to grayscale to do morphological processing.
    I = rgb2gray(singleFrame);

    % Remove dark cars.
    noDarkCars = imextendedmax(I, TresholdValue);

    % Remove lane markings and other non-disk shaped structures.
    noSmallStructures = imopen(noDarkCars, sedisk);

    % Remove small structures.
    noSmallStructures = bwareaopen(noSmallStructures, 150);

    % Get the area and centroid or bounding box of each remaining object in the frame. The
    % object with the largest area is the light-colored car. Create a copy
    % of the original frame and tag the car by changing the orange centroid pixel
    % or putting moving object into orange bounding box.
    
    taggedCars(:,:,:,k) = singleFrame;
    
    %allow this when using bounding box
    %taggedCars2(:,:,:,k) = singleFrame;
    %I = taggedCars2(:,:,:,k);
        
    
    framehandle = read(trafficObj, k);
        
    noSmallStructures = bwlabel(noSmallStructures);
    stats = regionprops(noSmallStructures, {'Area','Centroid','BoundingBox'}); %'Centroid' instead of 'BoundingBox'
%% standalone tracking algorithm
    if ~isempty([stats.Area])
        areaArray = [stats.Area];
        [junk,idx] = max(areaArray);
        
%% CENTROID drawing
        c = stats(idx).Centroid;
        c = floor(fliplr(c));
        width = 2;
        row = c(1)-width:c(1)+width;
        col = c(2)-width:c(2)+width;
        taggedCars(row,col,1,k) = 255;
        taggedCars(row,col,2,k) = 165;
        taggedCars(row,col,3,k) = 0;
        
        
%% BOUNDING BOX rectangle drawing
% bb = stats(idx).BoundingBox;
% bb = floor(bb);
% BBimg = false(bb([4 3]));
% BBimg(:, [1 end]) = true;
% BBimg([1 end],:) = true;
% %taggedCars(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),1,k) = double(BBimg).*255;
% taggedCars(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),1,k) = 255;
% taggedCars(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),2,k) = 165;
% taggedCars(bb(2):(bb(2)+bb(4)),bb(1):(bb(1)+bb(3)),3,k) = 0;


%% BOUNDING BOX drawing
% bb = stats(idx).BoundingBox;
% bb = floor(bb);
% BBimg = false(bb([4 3]));
% BBimg(:, [1 end]) = true;
% BBimg([1 end],:) = true;
% M = false(size(I,1),size(I,2));
% M(bb(2):bb(2)+bb(4)+1,[bb(1), bb(1)+bb(3)+1]) = true;
% M([bb(2),bb(2)+bb(4)+1],bb(1):bb(1)+bb(3)+1) = true;
% idx = find(M);
% nM = numel(M);
% I(idx) = 255;
% I(idx+nM) = 165;
% I(idx+nM*2) = 0;
% taggedCars2(:,:,:,k) = I;
        
     
    end
end

%% playing results
frameRate = get(trafficObj,'FrameRate');
implay(taggedCars,frameRate);

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