我制作了这个似乎可以完成这项工作的小代码:
A=[0 0 1 1 1 0 ;
0 0 1 1 1 0 ;
0 0 0 0 0 0 ;
2 2 0 0 0 0 ;
2 2 0 0 0 0 ;
2 2 0 0 0 0 ];
B=[3 0 2 2 0 0 ;
3 0 2 2 0 0 ;
0 0 2 2 0 0 ;
0 0 0 0 0 0 ;
1 1 1 1 1 0 ;
1 1 1 1 1 0 ];
%give a unique id to each rectangle
lblA=bwlabel(A);
lblB=bwlabel(B);
%search for overlap
overlap= A & B;
% find out overlaping rectangle s unique id
overlapLbl= unique([lblA(overlap(:)) lblB(overlap(:))],'rows');
%eliminate rectangle with lower score
for ov=1:size(overlapLbl,1)
scoreA=A(find((lblA==overlapLbl(ov,1)),1));
scoreB=B(find((lblB==overlapLbl(ov,2)),1));
if scoreA>scoreB
B(B==scoreB)=0;
else
A(A==scoreA)=0;
end
end
C=A+B
输出:
C =
3 0 2 2 0 0
3 0 2 2 0 0
0 0 2 2 0 0
2 2 0 0 0 0
2 2 0 0 0 0
2 2 0 0 0 0