当前位置:文档之家› 帧差法代码讲课稿

帧差法代码讲课稿

% By lyqmath @ Matlab中文论坛clc; clear all; close all;avi = mmreader('samplevideo.avi');for i = 1 : avi.NumberOfFramesimg = 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(video)if ischar(video)% 载入视频数据for i = 1 : avi.NumberOfFramesimg = read(avi, i);pixels(:, :, :, i) = img;endelsepixels = video;endnFrames = size(pixels, 4);rows = size(pixels, 1);cols = size(pixels, 2);% 转换成灰度图像for i = 1 : nFramespixel(:, :, i) = (rgb2gray(pixels(:,:,:,i)));endfor i = 2 : nFramesd(:, :, i) = (abs(pixel(:,:,i) - pixel(:,:,i-1)));bw(:, :, i) = im2bw(d(:, :, i), 0.2);% 寻找上下边界cou=1;for h = 1:rowsfor w = 1:colsif bw(h, w, i) > 0.5bottomEdge = h;if cou == 1topEdge = bottomEdge;endcou = cou+1;break;endendend% 寻找左右边界coun=1;for w = 1:colsfor h = 1:rowsif bw(h, w, i) > 0.5rightEdge = w;if coun == 1leftEdge = rightEdge;coun = coun+1;endbreak;endendend% 矩形框生成wd = rightEdge-leftEdge;hg = bottomEdge-topEdge;widt = wd/2;heit = hg/2;cenx = leftEdge+widt;ceny = topEdge+heit;% 显示并标记figure(1);imshow(pixels(:, :, :, i), []);hold onrectangle('Position',[leftEdge 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 offEnd另一段代码% By lyqmathclc; clear all; close all;% 原始视频targetavi = 'traffic.avi';% 检测结果视频resultavi = 'c:\\result.avi';%% 读取视频mov = mmreader(targetavi);fnum = mov.NumberOfFrames;%% 建立结果视频aviobj = avifile(resultavi);aviobj.Quality = 100;aviobj.Fps = 25;pression = 'Indeo5';%% 帧间差分法figure(1);for i = 2 : fnumx = read(mov, i-1);y = read(mov, i);subplot(1, 2, 1); imshow(x, []); title(sprintf('第%d帧视频,By lyqmath', i-1), 'FontWeight', 'Bold', 'Color', 'r');% 灰度化if ndims(x) == 3m = rgb2gray(x);elsem = x;endif ndims(y) == 3n = rgb2gray(y);elsen = 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)<t) = 0;c = logical(c);x1 = x(:, :, 1); x2 = x(:, :, 2); x3 = x(:, :, 3);x1(c) = 255; x2(c) = 0; x3(c) = 0;xc = cat(3, x1, x2, x3);subplot(1, 2, 2); imshow(xc, []); title(sprintf('第%d帧视频识别结果,By lyqmath', i-1), 'FontWeight', 'Bold', 'Color', 'r');f = getframe(gcf);f = frame2im(f);% 生成视频aviobj = addframe(aviobj, f);end%% 关闭视频句柄aviobj = close(aviobj);第三段代码mov=aviread('CIMG0003.AVI');temp=size(mov);fnum=temp(2);for i=1:fnum,strtemp=strcat(int2str(i),'.','JPG');imwrite(mov(i).cdata(:,:,:),strtemp);end%% 从视频中提出每一帧图像o=1;e='.jpg';for i=1:15u=o-1;v=o-2;m=int2str(o);n=int2str(u);h=int2str(v);s=strcat(m,e);%%把字符串b与m连接后在连接e得到图像文件存储的位置m=imread(s);%%从S处把图像读取出来m=rgb2gray(m);%%将图像M灰度化m=medfilt2(m,[3,3]);if(o>=3)s=strcat(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);c(k)=0;c=bwareaopen(c,15);se90=strel ('line',3,90);se0=strel ('line',3,0);c=bwmorph(c,'close'); %对上述图像进行形态学闭运算c=imdilate(c,[se90,se0]);c=bwmorph(c,'close');c=bwareaopen(c,50);k=find(abs(j)>=th);j(k)=1;k=find(abs(j)<th);j(k)=0;j=bwareaopen(j,15);j=bwmorph(j,'close'); %对上述图像进行形态学闭运算j=imdilate(j,[se90,se0]);j=bwmorph(j,'close');c=bwareaopen(c,50);c=c&j;c=imerode(c,[se90,se0]);figure,imshow(c);a=c;b=c; d=c;f=c;[m,n]=size(c);%%行扫描填充for i=1:mfor j=1:n-1if a(i,j)>0a(i,j+1)=1;endendendfor i=1:mfor j=n:-1:2if b(i,j)>0b(i,j-1)=1;endendendth=a&b;%%列扫描填充for i=1:nfor j=1:m-1if d(j,i)>0d(j+1,i)=1;endendendfor i=1:nfor j=m:-1:2if f(j,i)>0f(j-1,i)=1;endendendtd=d&f;c=th&td;endo=o+1;end。

相关主题