function [I2,plotx,ploty,Num,indxx,Lensx2,Lensy2]=func_tracking_multi_object(I0,I1);
[rows,cols]= size(I1);
[L,n] = bwlabel(I1);%计算连通域的标记
Num = n;
Xc = 0;
Yc = 0;
L2(1:rows,1:cols,1:3) = 0;
I2(:,:,1) = I0(:,:,1);
I2(:,:,2) = I0(:,:,2);
I2(:,:,3) = I0(:,:,3);
S = zeros(1,Num);
X = zeros(2,Num);
Y = zeros(2,Num);
for i=1:n
[r,c]=find(L==i);%搜索目标
a1(i)=max(r);
a2(i)=min(r);
b1(i)=max(c);
b2(i)=min(c);
%用蓝色方框标记目标
L2(a2(i):a2(i)+4 , b2(i):b1(i) ,1) = 0;
L2(a1(i):a1(i)+4 , b2(i):b1(i) ,1) = 0;
L2(a2(i):a1(i) , b2(i):b2(i)+4,1) = 0;
L2(a2(i):a1(i) , b1(i):b1(i)+4,1) = 0;
L2(a2(i):a2(i)+4 , b2(i):b1(i) ,2) = 0;
L2(a1(i):a1(i)+4 , b2(i):b1(i) ,2) = 0;
L2(a2(i):a1(i) , b2(i):b2(i)+4,2) = 0;
L2(a2(i):a1(i) , b1(i):b1(i)+4,2) = 0;
L2(a2(i):a2(i)+4 , b2(i):b1(i) ,3) = 255;
L2(a1(i):a1(i)+4 , b2(i):b1(i) ,3) = 255;
L2(a2(i):a1(i) , b2(i):b2(i)+4,3) = 255;
L2(a2(i):a1(i) , b1(i):b1(i)+4,3) = 255;
%计算面积
S(i) = length(r);
X(:,i) = [a2(i);a1(i)];
Y(:,i) = [b2(i);b1(i)];
%单独提取不同的目标
Isub = zeros(rows,cols);
for j = 1:length(r)
Isub(r(j),c(j)) = 1;
end
%保存不同的目标
Isub_save{i} = Isub;
Lensx(i) = a1(i)-a2(i);
Lensy(i) = b1(i)-b2(i);
end
%输出带方框的图像
for i = 1:rows
for j = 1:cols
if L2(i,j,3) == 255
I2(i,j,1) = 0;
I2(i,j,2) = 0;
I2(i,j,3) = 255;
else
I2(i,j,1) = I0(i,j,1);
I2(i,j,2) = I0(i,j,2);
I2(i,j,3) = I0(i,j,3);
end
end
end
%根据面积进行目标独特性区分
[V,I] = sort(S);
% V
% I
%求质心
%获得从小到大的目标排序
indxx = I;
for k = 1:Num
sumx = 0;
sumy = 0;
area = 0;
Is = Isub_save{indxx(k)};
[height,width] = size(Is);
for i = 1 : height
for j = 1 : width
if Is(i,j) == 1
sumx = sumx + i;
sumy = sumy + j;
area = area + 1;
end
end
end
%%质心坐标
plotx(1,k) = fix(sumx / area);
ploty(1,k) = fix(sumy / area);
Lensx2(k) = Lensx(indxx(k));
Lensy2(k) = Lensy(indxx(k));
end
for k = 1:Num
I2(round(plotx(k))-4:round(plotx(k))+4,round(ploty(k))-4:round(ploty(k))+4,1) = 255;
I2(round(plotx(k))-4:round(plotx(k))+4,round(ploty(k))-4:round(ploty(k))+4,2) = 0;
I2(round(plotx(k))-4:round(plotx(k))+4,round(ploty(k))-4:round(ploty(k))+4,3) = 0;
end
A09-56