How to get the minimum mode image and a plot, using pixel values of all the frames of the video in the given program?

1 view (last 30 days)
That is, to get the image background of the site, so that i would be able to subtract the original frame to the mode frame and get the number of vehicles passing through?
The code is as follows:
nframes = trafficVid.NumberOfFrames;
I = read(trafficVid, 1);
taggedCars = zeros([size(I,1) size(I,2) 3 nframes], class(I));
for k = 1 : nframes
singleFrame = read(trafficVid, k);
% Convert to grayscale to do morphological processing.
I = rgb2gray(singleFrame);
% Remove dark cars.
noDarkCars = imextendedmax(I, darkCarValue);
% 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 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 centroid pixel
% value to red.
taggedCars(:,:,:,k) = singleFrame;
stats = regionprops(noSmallStructures, {'Centroid','Area'});
if ~isempty([stats.Area])
areaArray = [stats.Area];
[junk,idx] = max(areaArray);
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) = 0;
taggedCars(row,col,3,k) = 0;
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 4 Jul 2015
before your "for" loop, add
minimg = inf(size(I));
for k = 1 : nframes
singleFrame = read(trafficVid, k);
minimg = min(minimg, singleFrame);
end
now minimg contains the minimum of all of the RGB components.
Note: the minimum RGB components for any given pixel will not necessarily be the darkest value. It will also not necessarily be the "background". Consider that if pedestrian wearing a black jacket walks through the scene, then the black may become the minimum ever found at the locations traveled through, but it would not be the background.

More Answers (1)

Image Analyst
Image Analyst on 4 Jul 2015
The mode frame sounds like a good idea, though it seems like people usually use Gaussian Mixture Models to get background from video. See http://www.mathworks.com/help/vision/examples/detecting-cars-using-gaussian-mixture-models.html?prodcode=VP&language=en
  2 Comments
ramya ullagaddimath
ramya ullagaddimath on 12 Jul 2015
Sir/Ma'am, Thank you so much for the above link, it was a life-saver. Is there any extension that I could add, so as to make this code read both the cars and motorcycles too? Give me a separate list of the total number of cars, motorcycles, trucks and other classification of the vehicles in a given traffic video?
Thank you.
Image Analyst
Image Analyst on 12 Jul 2015
Personally, I don't know because I don't have that toolbox and haven't used that code, but I would think you could do that.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!