ردیابی ماشین به منظور کنترل سرعت با متلب
کدهای پروژه
clc;
clear all;
close all;
%It is an object for reading video files
trafficVid = VideoReader(‘traffic.mj2’);
get(trafficVid)
%This command is used to view videos and sequences of images
implay(‘traffic.mj2’);
darkCarValue = 50;
darkCar = rgb2gray(read(trafficVid,70));
%Read image into workspace.
noDarkCar = imextendedmax(darkCar, 50);
imshow(darkCar)
figure, imshow(noDarkCar)
%creates a disk-shaped structuring element, where r specifies the radius.
sedisk = strel(‘disk’,0/001);
% performs morphological opening on the grayscale or binary image I using the structuring element SE.
noSmallStructures = imopen(noDarkCar, sedisk);
imshow(noSmallStructures)
nframes = 120%trafficVid.NumFrames;
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, 50);
% 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
frameRate = trafficVid.FrameRate;
implay(taggedCars,frameRate);
End_Mean_speed=k;
دیدگاهتان را بنویسید