检测和跟踪移动的汽车

时间:2011-06-25 23:14:21

标签: matlab image-processing

我正在做一个关于“汽车追踪”的项目。我正在尝试开发一个实时运行的代码,并能够检测到接近交叉点的汽车。 Ι减去两个连续的帧然后thresholding.Implement形态闭合然后我找到移动的汽车。我的问题是我无法检测到黑车。如果你给我一些指导来成功完成这个项目,我真的很感激。

%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)

1 个答案:

答案 0 :(得分:1)

在计算机视觉系统工具箱中查看vision.ForegroundDetector以及以下example