本文共 3043 字,大约阅读时间需要 10 分钟。
目标跟踪一直是计算机视觉领域中一个比较重要的研究课题,实际应用较多。本文主要采用背景差分法、帧差法和光流法实现简单的目标跟踪系统。
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)));endw_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 offend
在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 原文: 版权声明:本文为博主原创文章,转载请附上博文链接!