# 用MATLAB实现对运动物体识别与跟踪

bwareaopen（src，int），src为二值图像，int为设置的联通域的大小，是对帧差法，在转化为二值的图像进行操作，结果是将大小小于设定的int的连通域置为0；

regionprops（src，’‘）其中src为传入的二值图像，’‘内的为你所需要的属性

yuandian=zeros(2,2);
i=1293;
filename1 = strcat('I:\2d3d\2\dxshiyan2\C',num2str(i),'.jpg');
%src1=rgb2gray(src1);
%src2=rgb2gray(src2);
zhic1=src2-src1;
zhic1=im2bw(zhic1,0.2);
imshow(zhic1);
zhic2= bwareaopen(zhic1,3000);
imshow(zhic2);
quyu=regionprops(zhic2,'Centroid');
[u,v]=zhixin(zhic2,int16(quyu.Centroid(2)),int16(quyu.Centroid(1)));
yuandian(1,1)=u;
yuandian(1,2)=v;
%去一个模板为400*400；
src1=src2;
%src2=rgb2gray(src2);
zhic2=src2-src1;
zhic1=im2bw(zhic2,0.2);
zhic2= bwareaopen(zhic1,3000);
imshow(zhic2);
quyu=regionprops(zhic2,'Centroid');
[u,v]=zhixin(zhic2,int16(quyu.Centroid(2)),int16(quyu.Centroid(1)));
yuandian(2,1)=u;
yuandian(2,2)=v;
for i=1294:1386
filename1 = strcat('I:\2d3d\2\dxshiyan2\C',num2str(i),'.jpg');
src1=src2;
src3=src2;
zhic2=src2-src1;
zhic2=im2bw(zhic2,0.2);
imshow(zhic2);
houx=2*yuandian(2,2)-yuandian(1,2);
houy=2*yuandian(2,1)-yuandian(1,1);
[u,v]=zhixin(zhic2,houy,houx);
[state,result]=draw_rect(src3,[v-250,u-250],[500,500],0);
%imshow(result);
pause(0.01);
yuandian(1,1)=yuandian(2,1);
yuandian(1,2)=yuandian(2,2);
if(abs(yuandian(1,1)-u<400)&&abs(yuandian(1,2)-v<400))
yuandian(2,1)=u;
yuandian(2,2)=v;
end
end

fanction：

%找质心
function [u,v]=zhixin(erzhi,y,x)
u=0;
v=0;
zuobiaox=0;
zuobiaoy=0;
number=0;
jilux=0;
jiluy=0;
for i=y-200:y+200
for j=x-200:x+200
if(j<3839)
if(erzhi(i,j)==1)
number=number+1;
u=(int64(i)+int64(u));
v=(int64(j)+int64(v));
zuobiaox=zuobiaox+double(erzhi(i,j));
zuobiaoy=double(erzhi(i,j))+zuobiaoy;
jilux=int64(jilux)+int64(j);
jiluy=int64(jiluy)+int64(i);
end
end
end
end
u=int64(u/zuobiaoy);
%p=jilux/int64(number)-u;
v=int64(v/zuobiaox);
%q=jiluy/int64(number)-v;
%k=0;

draw-rect

function [state,result]=draw_rect(img,startPosition,windowSize,showOrNot)
% 函数调用：[state,result]=draw_rect(img,startPosition,windowSize,showOrNot)
% 函数功能：在图像画个长方形框
% 函数输入：img为原始的大图，可为灰度图，可为彩色图
%          startPosition 框的左上角在大图中的坐标(每行代表x,y坐标)，startPosition=[10,30],分别表示x,y为10,30
%          windowSize 框的大小 windowSize=[112,92] 分别表示宽、高
%          showOrNot 是否要显示结果?默认为显示出来?
% 函数输出：state -- 表示程序结果状态?
%          result - 结果图像数据

if nargin < 4
showOrNot = 1;
end

rgb = [255 0 0];                                 % 边框颜色
lineSize = 3;                                      % 边框大小，取1,2,3

windowSize(1,1)=windowSize(1,1);
windowSize(1,2) = windowSize(1,2);
if windowSize(1,2) > size(img,1) ||...
windowSize(1,1) > size(img,2)
state = -1;                                     % 说明窗口太大，图像太小，
disp('the window size is larger then image...');
return;
end

result = img;
if size(img,3) == 3
for k=1:3
for i=1:size(startPosition,1) %矩形框的总数
if(startPosition(i,1)>=0 && startPosition(i,2)>=0)
result(startPosition(i,2),startPosition(i,1):startPosition(i,1)+windowSize(i,1),k) = rgb(1,k); %画上边框
result(startPosition(i,2):startPosition(i,2)+windowSize(i,2),startPosition(i,1)+windowSize(i,1),k) = rgb(1,k);%画右边框
result(startPosition(i,2)+windowSize(i,2),startPosition(i,1):startPosition(i,1)+windowSize(i,1),k) = rgb(1,k);  %画下边框
result(startPosition(i,2):startPosition(i,2)+windowSize(i,2),startPosition(i,1),k) = rgb(1,k);   %画左边框

if lineSize == 2 || lineSize == 3
result(startPosition(i,2)+1,startPosition(i,1):startPosition(i,1)+windowSize(i,1),k) = rgb(1,k);
result(startPosition(i,2):startPosition(i,2)+windowSize(i,2),startPosition(i,1)+windowSize(i,1)-1,k) = rgb(1,k);
result(startPosition(i,2)+windowSize(i,2)-1,startPosition(i,1):startPosition(i,1)+windowSize(i,1),k) = rgb(1,k);
result(startPosition(i,2):startPosition(i,2)+windowSize(i,2),startPosition(i,1)-1,k) = rgb(1,k);

if lineSize == 3
result(startPosition(i,2)-1,startPosition(i,1):startPosition(i,1)+windowSize(i,1),k) = rgb(1,k);
result(startPosition(i,2):startPosition(i,2)+windowSize(i,2),startPosition(i,1)+windowSize(i,1)+1,k) = rgb(1,k);
result(startPosition(i,2)+windowSize(i,2)+1,startPosition(i,1):startPosition(i,1)+windowSize(i,1),k) = rgb(1,k);
result(startPosition(i,2):startPosition(i,2)+windowSize(i,2),startPosition(i,1)+1,k) = rgb(1,k);
end
end
end
end
end
end

state = 1;

if showOrNot == 1
figure;
hold on;
imshow(result);
end

+ 订阅