fo(:,:,j)=imopen( D(:,:,j),se);%直接开运算 subplot(2,2,4);imshow( fo(:,:,j),[]); C=(C+D(:,:,j)); E=(E+ fo(:,:,j)); end C=C/10;
figure,subplot(2,2,1);imshow( C,[]); subplot(2,2,2);imshow(E,[]);
imwrite(uint8(C),'C:\\Users\\Administrator\\Desktop\\16.jpg')
a=imread('C:\\Users\\Administrator\\Desktop\\16.jpg'); figure,imshow(a) count=imhist(a); [m,n]=size(a); N=m*n; L=256;
count=count/N;%%每一个像素的分布概率
for i=1:L
if count(i)~=0 st=i-1; break; end end
for i=L:-1:1 if count(i)~=0 nd=i-1; break; end end
f=count(st+1:nd+1); %f是每个灰度出现的概率 size(f) E=[];
for Th=st:nd-1 %%%设定初始分割阈值为Th av1=0; av2=0;
Pth=sum(count(1:Th+1)); %%%第一类的平均相对熵为 for i=0:Th
av1=av1-count(i+1)/Pth*log(count(i+1)/Pth+0.00001); end
%%%第二类的平均相对熵为 for i=Th+1:L-1
av2=av2-count(i+1)/(1-Pth)*log(count(i+1)/(1-Pth)+0.00001); end
E(Th-st+1)=av1+av2;
% E(Th-st+1)=av1*(1-Pth)^15; end
position=find(E==(max(E))); th=st+position-1;
for i=1:m for j=1:n if a(i,j)>th a(i,j)=255; else
a(i,j)=0; end end end
figure,imshow(a);title('1')
se=strel('disk',1');%圆盘型结构元素 fo=imopen(a,se);%直接开运算
figure, imshow(fo);title('kai'); % B=[1 1 % 1 1]; B=[1 1 1 1 1 1 1 1 1];
A2=imdilate(fo,B);
figure, imshow(A2);title('fushi');
imwrite(A2,'C:\\Users\\Administrator\\Desktop\\10.jpg') Ibw=imread('10.jpg'); % I=rgb2gray(I); % Ibw=im2bw(I); [l,m]=bwlabel(Ibw,8);
status=regionprops(l,'BoundingBox'); imshow(Ibw);hold on; for i=1:m
rectangle('position',status(i).BoundingBox,'edgecolor','r'); end hold off;
frame = read(videoObj,start); figure,imshow(frame); for i=1:m
rectangle('position',status(i).BoundingBox,'edgecolor','r'); end
本算法的结果:
表格 1原始视频
表格 2显著图
表格 3最大熵算法处理后的二值图
表格 4二值图中的移动物体
表格 5原始图像中检测出的移动的物体
相关推荐: