car detection

by

 

this codes helps to identify the car

[targetVideoFile]=detectCarsInVideo(videoFile)
%Group # 10
%function 'detectCarsInVideo(videoFile)' take a video file in 'AVI' format
%and detect Cars in that video.

function [targetVideoFile]=detectCarsInVideo(videoFile)

%read the video file
trafficObj = mmreader(videoFile);

%take number of frames
nframes = get(trafficObj, 'NumberOfFrames');

%take first frame of video
I = read(trafficObj, 1);

%design a buffer for storing that frame.
taggedCars = zeros([size(I,1) size(I,2) 3 nframes], class(I));

darkCarValue = 50;

%mark red color point on detected cars for all frames
%which is taken by the for loop as following
for k = 1 : nframes
    singleFrame = read(trafficObj, k);
    darkCar = rgb2gray(read(trafficObj,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

%take frame rate from the original video file
frameRate = get(trafficObj,'FrameRate');
%assign the frame rate to the taggedCars video file and stor it.
implay(taggedCars,frameRate);
targetVideoFile = VideoWriter(taggedCars, 'Uncompressed AVI');

Contact us