【Matlab/CV系列】简单目标跟踪系统设计

Date: 2019-5-2


前言

    目标跟踪一直是计算机视觉领域中一个比较重要的研究课题,实际应用较多。本文主要采用背景差分法、帧差法和光流法实现简单的目标跟踪系统。

1、参考

https://www.ilovematlab.cn/thread-542170-1-1.html
https://www.ilovematlab.cn/thread-493241-1-1.html

2、Matlab实现(部分)
function d = tracking(video)
pixels = video;
nFrames = size(pixels, 4);
rows = size(pixels, 1);
cols = size(pixels, 2);

% 转换成灰度图像
for i = 1 : nFrames
    pixel(:, :, i) = (rgb2gray(pixels(:,:,:,i)));
end

w_history = 0;
for i = 2 : nFrames
    d(:, :, i) = (abs(pixel(:,:,i) - pixel(:,:,i-1)));
    bw(:, :, i) = im2bw(d(:, :, i), 0.2);
    % 寻找上下边界
    cou=1;
    for h = 1:rows
        for w = 1:cols
            if bw(h, w, i) > 0.5
                bottomEdge = h;
                if cou == 1
                    topEdge = bottomEdge;
                end
                cou = cou+1;
                break;
            end
        end
    end
    % 寻找左右边界
    coun=1;
    for w = 1:cols
        for h = 1:rows
            if bw(h, w, i) > 0.5
                if i <= 2/3*nFrames
                    if abs(w-w_history) < 72.1 
                        rightEdge = w;
                    else
                        rightEdge = w_history+1;
                    end
                else
                    if abs(w-w_history) < 62.5 
                        rightEdge = w;
                    else
                        rightEdge = w_history+1;
                    end
                end
                if coun == 1
                    leftEdge = rightEdge;
                    coun = coun+1;
                end
                w_history = rightEdge;
                break;
            end
        end
    end
    % 矩形框生成
    wd = rightEdge-leftEdge;
    hg = bottomEdge-topEdge;
    
    widt = wd/2;
    heit = hg/2;
    cenx = leftEdge+widt;
    ceny = topEdge+heit;
    % 显示并标记
    figure(1);
    imshow(pixels(:, :, :, i), []);
    hold on
    rectangle('Position',[cenx-10 topEdge 20 hg], 'EdgeColor', 'r', 'LineWidth', 2);
    plot(cenx, ceny, 'ko', 'MarkerFaceColor', 'r', 'MarkerSize', 10, 'LineWidth', 2);
    
    
    text(1, 15, sprintf('跟踪视频:%d帧', i), 'FontWeight', 'Bold', 'Color', 'r');
    hold off
end
3、实验效果图
3.1、背景差分法实现目标跟踪

【Matlab/CV系列】简单目标跟踪系统设计_第1张图片

3.2、帧差法实现目标跟踪

【Matlab/CV系列】简单目标跟踪系统设计_第2张图片

3.3、光流法实现目标跟踪

【Matlab/CV系列】简单目标跟踪系统设计_第3张图片
【Matlab/CV系列】简单目标跟踪系统设计_第4张图片

4、更多知识
4.1、matlab GUI中imshow刷新问题

在matlab 2012b中GUI中显示视频是可以正常显示的,但是在更高版本的2014b中,在axes中不能刷新出视频中每帧数据,只能显示出来最后一帧。
解决方案:在imshow后面加上drawnow。

help drawnow
drawnow Flush pending graphics events.
drawnow “flushes the event queue” and updates the figure window.
drawnow causes figure windows and their children to update and
flushes the system event queue. Any callbacks generated by incoming
events - e.g. mouse or key events - will be dispatched before
drawnow returns.
drawnow(‘EXPOSE’) causes only graphics objects to refresh, if
needed. It does not allow callbacks to execute and does not
process other events in the queue.
drawnow(‘UPDATE’) causes only non-graphics objects to refresh, if
needed. It does not allow callbacks to execute and does not
process other events in the queue.
drawnow is called implicitly upon returning to the MATLAB prompt and
when executing the following functions:

figure
getframe
ginput
input
keyboard
pause
waitfor
waitforbuttonpress


作者:SoaringLee_fighting
来源:CSDN
原文:https://blog.csdn.net/SoaringLee_fighting/article/details/89763298
版权声明:本文为博主原创文章,转载请附上博文链接!


THE END!

在这里插入图片描述

你可能感兴趣的:(【计算机视觉与图像处理】,【实用毕设项目】)