背景差分法MATLAB实现

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

程序1背景差分法MATLAB实现functiontemp3d=60;b='d\6\capfile3.avi';e='.bmp';fori=0:13u=d+1;m=int2str(d);n=int2str(u);s=strcat(b,m,e);%连接字符m=imread(s);m=rgb2gry(m);%灰度值%imshow(m);if(d=61)s=strcat(b,n,e);n=imread(s);n=rgb2gry(n);q=im2double(m);figure(18);imshow(q);w=in2double(n);figure(19);imshow(w)c=q-w;%进行差分处理figure(20);imshow(c)figure(99);imshow(c);%绘制直方图t=100;%此値可以调节(阈值调整)t=t/255;k=find(abs(c)=t);c(k)=1;k=find(abs(c)t);c(k)=0;figure;imshow(c);sc=strel('square',5);%用平坦的5*5结构元素进行了膨胀c=imdilate(c,sc);endd=d+1;end程序2图像处理:f0=imread('capfile3.avi61.bmp');g=rgb2gray(f0);%图像灰度化imshow(g);g2=imnoise(g,'salt&pepper',0.05);%加淑盐噪声figure(3);imshow(g2);g3=medfilt2(g2,[3,3],'symmtric');%以3*3窗口中值滤波figure(4);imshow(g3);g1=im2bw(g);%图像二值化figure(2);imshow(g1);程序3%“高速公路汽车阴影”视屏的阴影去除的方法clc;clearall;closeall;%打开视频文件对话框h1=figure('toolbar','name','ShadowRemoval');[FileName.PathName]=uigetfile({'*.avi','avi(*.avi)','*.*','AllFiles(*.*)'},'Openvideosequence');ifisequal([FileName,PathName],[0,0])returnelsepic_path=fullfile(PathName,FileName);mov=aviread(pic_path);endn=100;%读取视频帧数s=1;%起始为1e=n;%结束为nfori=s:emov_gray(i).image=mov(i).cdate;%mov_gray(i).image为第i帧图像end[ih,iw,id]=size(mov_gray(1).image);%取得视频帧的大小%计算1到第n帧的各个像素的均值fori=s:eu=mov_gray(i).image;h=u(:)';t(i,:)=h(:);endtmp=median(t);%对t数组各列取均值mean=reshape(tmp,ih,iw,id);%将n帧均值图像恢复成h*w*d的图像矩阵,得到背景figure,imshow(uint8(mean));title('背景图像');fn=18;ok=mean;tmp(1:3)=0;di=20;th=ones(1,1,3)*di;%预设阈值diff=double(ok)-double(mov_gray(fn).image);%计算第fn帧图像与背景的差值diff1=abs(diff);forai=1:ihforbi=1:iwifabs(diff(ai,bi,1:3))=th(1,1,:)ok(ai,bi,1:3)=mov_gray(fn).image(ai,bi,1:3);elseok(ai,bi,1:3)=0;diff1(ai,bi,1:3)=0;endendendfigure,imshow(uint8(ok));title('前景图像');impixelinfo;g=rgb2hsv(mean);%将背景图像由RGB格式转为HSV格式figure,imshow(g);title('背景图像的hsv格式');impixelinfo;figure,imtooi(g);g1=rgb2hsv(ok);%将前景图像由RGB格式转为HSV格式figure,imshow(g1);title('前景图像的hsv格式');impixelinfo;figure,imtooi(g1);%阈值分割%这个地方的阈值选取是手动的,根据经验得到的!forci=1:ihif(g1(ci,di,3)/g(ci,di,3)=0.4)&&(g1(ci,di,3)/g(ci,di,3)=0.5)&&abs(g1(ci,di,2)-g(ci,di,2))=0.5&&abs(g1(ci,di,1)-g(ci,di,1))=0.1;h1(ci,di,1:3)=1;elseh1(ci,di,1:3)=0;endendendh=hsv2rgb(h1);figure,imshow(h);title('阴影去除结果');

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功