一、获取代码方式


获取代码方式2:

完整代码已上传我的资源:​​【运动检测】基于matlab最大互信息运动目标检测【含Matlab源码 1607期】​


二、数字图像处理简介

图像处理基础教程链接

1 【基础教程】基于matlab图像处理(表示方法+数据结构+基本格式+类型转换+读取+点运算+代数运算)【含Matlab源码 834期】

2 【基础教程】基于matlab图像处理(读写+显示+运算+转换+变换+增强+滤波+分析+统计)【含Matlab源码 144期】

3 【基础教程】基于matlab图像增强+复原+分割【含Matlab源码 056期】

三、部分源代码

function  MovingTargetDetectionByMMI() 
%Moving Target Detection
%Based on Maximun Mutual Information
%


%读文件
Im1=imread('001.jpg');
Im2=imread('002.jpg');
Im3=imread('003.jpg');

Im1=rgb2gray(Im1);
Im2=rgb2gray(Im2);
Im3=rgb2gray(Im3);

tic;

d12=GetDifferenceImg(Im2,Im1);
d23=GetDifferenceImg(Im2,Im3);

d=d12.*d23;
se =[0 0 0 1 0 0 0
0 0 1 2 1 0 0
0 1 2 4 2 1 0
1 2 4 8 4 2 1
0 1 2 4 2 1 0
0 0 1 2 1 0 0
0 0 0 1 0 0 0];
for i=1:4
d = imfilter(d,se);
end
for i=1:2
d = medfilt2(d,[4 4]);
end
%%d=abs((d12-d23).^0.7);
d=uint8(d/max(max(d))*255);
level = graythresh(d);
BW = im2bw(d,level);
s=regionprops(BW,'BoundingBox');

figure(1)
subplot(2,2,1);
imshow(uint8(d12/max(max(d12))*255));
title('参考帧与前一帧的差值')
subplot(2,2,2);
imshow(uint8(d23/max(max(d23))*255));
title('参考帧与后一帧的差值')
subplot(2,2,3);
imshow(BW);
title('由前后帧得出的差值')
subplot(2,2,4);
imshow(Im2);
%imshow(d);
rectangle('Position',s(1).BoundingBox,'Curvature',[0,0],'LineWidth',2,'LineStyle','--','EdgeColor','r')
title('参考帧与检测结果')

%求相邻两帧重合部分差值主函数
function outImg=GetDifferenceImg(R,F)
[CA1,~,~,CD1]=dwt2(R,'db1');
[CA2,~,~,CD2]=dwt2(F,'db1');
CA1=uint8(CA1);
CA2=uint8(CA2);


fprintf('\n------PSO start\n');
[pa,mi]=PSO(CA1,CA2);
while mi<1.2
[pa,mi]=PSO(CA1,CA2);
end
fprintf('tx:%f ty:%f ang:%f mi:%f\n',pa(1),pa(2),pa(3),mi);
fprintf('------PSO end\n\n');
%pa=[0,0,0];
fprintf('------Powell start\n');
mi_old=0;
while abs(mi-mi_old)>0.01
mi_old=mi;
[pa,mi]=powell(R,F,pa);
end
fprintf('------Powell end\n\n');

time=toc;
fprintf('tx:%.4f ty:%.4f ang:%.2f mi:%f\n',pa(1),pa(2),pa(3),mi);
fprintf('time:%f\n',time);

outImg=GetDifference(pa(1),pa(2),pa(3),R,F);
%figure(6);imshow(outImg);

%求相邻两帧重合部分差值
function outImg=GetDifference(tx,ty,ang,R,F)

[m,n]=size(R);
%
R=im2double(R);
F=im2double(F);

theta=ang*pi/180; %旋转角度转弧度
cx=floor(n/2); %旋转的中心点
cy=floor(m/2);
outImg=zeros(m,n);

for j=1:m
for i=1:n
%参考图像在浮动图像平移后的对应点
% x=i-tx; %列
% y=j-ty; %
x=(i-cx)*cos(theta)-(j-cy)*sin(theta)+cx-tx;
y=(i-cx)*sin(theta)+(j-cy)*cos(theta)+cy-ty;
x1=floor(x);
y1=floor(y);
rval=R(j,i);
%图像重合部分求差
if(x1>=1&&x1<n&&y1>=1&&y1<m)
dy=y1-y;dx=x1-x;
%双线性插值
fval=(F(y1+1,x1)-F(y1,x1))*dy+(F(y1,x1+1)-F(y1,x1))*dx+(F(y1+1,x1+1)+F(y1,x1)-F(y1,x1+1)-F(y1+1,x1))*dy*dx+F(y1,x1);
outImg(j,i)=abs((rval-fval).^0.7*exp(-min([rval fval])/20));
%outImg(j,i)=abs((rval-fval).^2.5/(min([rval fval])).^0.2);
end
end
end
%outImg=uint8(outImg/max(max(outImg))*255);

%双线性插值求互信息
function out=BI_mi(tx,ty,ang,R,F)
[m,n]=size(R);
hist=zeros(256,256);
ha = zeros(1,256);
hb = zeros(1,256);
%归一化到256级灰度
% if max(max(r))~=min(min(r)) %max(max(a))结果是A中最大的元素,max(A)结果一个行向量,元素分别是A的每个列向量的最大的元素
% r = (r-min(min(r)))/(max(max(r))-min(min(r)));
% else
% r = zeros(M,N);
% end
%
% if max(max(f))-min(min(f))
% f = (f-min(min(f)))/(max(max(f))-min(min(f)));
% else
% f = zeros(M,N);
% end
%
% r = double(int16(r*255))+1;
% f = double(int16(f*255))+1;

R=R+1;
F=F+1;
theta=ang*pi/180; %旋转角度转弧度
cx=floor(n/2); %旋转的中心点
cy=floor(m/2);

%求联合概率密度
for j=1:m
for i=1:n
%参考图像在浮动图像平移后的对应点
% x=i-tx; %列
% y=j-ty; %
x=(i-cx)*cos(theta)-(j-cy)*sin(theta)+cx-tx;
y=(i-cx)*sin(theta)+(j-cy)*cos(theta)+cy-ty;
x1=floor(x);
y1=floor(y);
rval=R(j,i);
%图像重合部分求差
if(x1>=1&&x1<n&&y1>=1&&y1<m)
dy=y1-y;dx=x1-x;
%双线性插值
fval=(F(y1+1,x1)-F(y1,x1))*dy+(F(y1,x1+1)-F(y1,x1))*dx+(F(y1+1,x1+1)+F(y1,x1)-F(y1,x1+1)-F(y1+1,x1))*dy*dx+F(y1,x1);
hist(fval,rval)=hist(fval,rval)+1;
end

end
end

四、运行结果

【运动检测】基于matlab最大互信息运动目标检测【含Matlab源码 1607期】_目标检测

五、matlab版本及参考文献

1 matlab版本

2014a

2 参考文献

[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.

[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.

[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.

[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.