Detection and tracking of a moving car
I am doing a project on "car tracking". I am trying to develop a code that will run in real time and will be able to detect car approaching a junction. Ι subtract two consecutive frames and then thresholding.Implement
morpho开发者_高级运维logical closing and then I find the moving car. My problem is that I can not detect dark cars. I would realy I appreciate if you give me some guidelines to successfully finish this project.
%Code for tracking
vid = videoinput('winvideo', '1', 'RGB24_640x360');
set(vid,'ReturnedColorSpace','rgb');
set(vid,'TriggerRepeat',Inf);
src = getselectedsource(vid);
src.FocusMode = 'manual';
%pause(0.02);
[i rect]=imcrop(getsnapshot(vid));
set(vid,'ROIPosition',rect);
h = fspecial('gaussian',[3,3],5);
%structial element
se90 = strel('line',5, 90);
se0 = strel('line', 5, 0);
ss=strel('diamond',10);
var=3;
start(vid);
for i=1:500
tic
im1= getsnapshot(vid);
imrgb=im1;
im1=im1(:,:,1);
if var==3
im2= getsnapshot(vid);
im2=im2(:,:,1);
im2=imfilter(im2,h);
var=0;
end
%-------------------------------------------------
if 1
m1=imfilter(im1,h);
% c2=im2-im1
%cbw=im2bw(c2);
%allagi stin evais8isia tou threshold
cbw=imsub(im1,im2,14,h);
end
cbw1 = imerode(cbw, [se90,se0]); %sistoli
% cbw1 = imerode(cbw1, [se901,se01]);
%diastoli
cbw1= imdilate(cbw1,ss);
%fill holes
cbw2= imfill(cbw1, 'holes');
cbw2= imfill(cbw2, 'holes');
if 1
cbw3 = imerode(cbw2, [se90 se0]); %sistoli
cbw3 = imerode(cbw3, [se90 se0]); %sistoli
cbw3 = imerode(cbw3, [se90 se0]); %sistoli
cbw3 = imerode(cbw3, [se90 se0]);
cbw3= imfill(cbw3, 'holes'); %fill hole
end
if 1
%Boundary box
s = regionprops(cbw3, 'BoundingBox');
if ~isempty(s)
cd = s.BoundingBox;
end
figure(1),
%imshow(cbw3);
imshow(imrgb)
hold on;
if ~isempty(s)
rectangle('Position',[cd(:,1) cd(:,2) cd(:,3) cd(:,4)],'LineWidth',2,'EdgeColor','b');
end
hold('off');
end
%empty ram
flushdata(vid)
var=var+1;
toc
end
stop(vid)
Check out vision.ForegroundDetector in the Computer Vision System Toolbox, and the following example.
精彩评论