帧差法代码6页

上传人:文库****9 文档编号:169105411 上传时间:2021-02-23 格式:DOC 页数:6 大小:23.50KB
返回 下载 相关 举报
帧差法代码6页_第1页
第1页 / 共6页
帧差法代码6页_第2页
第2页 / 共6页
帧差法代码6页_第3页
第3页 / 共6页
帧差法代码6页_第4页
第4页 / 共6页
帧差法代码6页_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《帧差法代码6页》由会员分享,可在线阅读,更多相关《帧差法代码6页(6页珍藏版)》请在金锄头文库上搜索。

1、 % By lyqmath Matlab中文论坛clc; clear all; close all;avi = mmreader(samplevideo.avi);for i = 1 : avi.NumberOfFrames img = read(avi, i); pixels(:, :, :, i) = img; figure(1); imshow(img, ); text(1, 15, sprintf(原视频:%d帧 By lyqmath, i), FontWeight, Bold, Color, r);endtracking(pixels); function d = tracking(

2、video)if ischar(video) % 载入视频数据 for i = 1 : avi.NumberOfFrames img = read(avi, i); pixels(:, :, :, i) = img; endelse pixels = video;endnFrames = size(pixels, 4);rows = size(pixels, 1);cols = size(pixels, 2);% 转换成灰度图像for i = 1 : nFrames pixel(:, :, i) = (rgb2gray(pixels(:,:,:,i);endfor i = 2 : nFrame

3、s 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)

4、 0.5 rightEdge = w; if coun = 1 leftEdge = rightEdge; coun = coun+1; end 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,leftE

5、dge topEdge wd hg, EdgeColor, r, LineWidth, 2); plot(cenx, ceny, ko, MarkerFaceColor, y, MarkerSize, 20, LineWidth, 2); text(1, 15, sprintf(跟踪视频:%d帧 By lyqmath, i), FontWeight, Bold, Color, r); hold off End另一段代码% By lyqmathclc; clear all; close all;% 原始视频targetavi = traffic.avi;% 检测结果视频resultavi = c

6、:result.avi;% 读取视频mov = mmreader(targetavi);fnum = mov.NumberOfFrames;% 建立结果视频aviobj = avifile(resultavi);aviobj.Quality = 100;aviobj.Fps = 25;pression = Indeo5;% 帧间差分法figure(1);for i = 2 : fnum x = read(mov, i-1); y = read(mov, i); subplot(1, 2, 1); imshow(x, ); title(sprintf(第%d帧视频,By lyqmath, i-1

7、), FontWeight, Bold, Color, r); % 灰度化 if ndims(x) = 3 m = rgb2gray(x); else m = x; end if ndims(y) = 3 n = rgb2gray(y); else n = y; end % 中值滤波 m = medfilt2(m); n = medfilt2(n); % 数据类型转换 q = im2double(n); w = im2double(m); % 差分 c = q-w; % 阈值,此值可以调节 t = 40/256; % 阈值分割 c(abs(c)=t)=255; c(abs(c)=3) s=st

8、rcat(n,e); n=imread(s); n=rgb2gray(n); s=strcat(h,e); h=imread(s); h=rgb2gray(h); n=medfilt2(n,3,3); h=medfilt2(h,3,3); q=im2double(m);%将图像数组转换为double型 w=im2double(n); g=im2double(h); c=q-w;j=w-g; th=10/255; k=find(abs(c)=th); c(k)=1; k=find(abs(c)=th); j(k)=1; k=find(abs(j)0 a(i,j+1)=1; end endend for i=1:m for j=n:-1:2 if b(i,j)0 b(i,j-1)=1; end endend th=a&b;%列扫描填充for i=1:n for

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号