I = imread('ans627.png');
I = I(35:276,86:381,1)==255;
I(:,:,2) = imtranslate(I,[10 -14]);
I(5:11,270:275,2) = true;
szI = size(I);
Ds = cell(szI(3)-1);
CCold = bwconncomp(I(:,:,1));
RPold = regionprops(CCold,'centroid');
centsA = vertcat(RPold(:).Centroid);
for ii = 1:(szI(3)-1)
CCnew = bwconncomp(I(:,:,ii+1));
RPnew = regionprops(CCnew,'centroid');
centsB = vertcat(RPnew(:).Centroid);
dim = 2;
if CCold.NumObjects>CCnew.NumObjects
dim = 1;
end
xyDiff = bsxfun(@(x,y)abs(x-y),reshape(centsA,[],1,2),reshape(centsB,1,[],2));
[~,idx] = min(hypot(xyDiff(:,:,1),xyDiff(:,:,2)),[],dim);
if dim==2;
displacements = centsB(idx,:)-centsA;
H = quiver(centsA(:,1),centsA(:,2),displacements(:,1),displacements(:,2));
else
displacements = centsB-centsA(idx,:);
H = quiver(centsB(:,1),centsB(:,2),displacements(:,1),displacements(:,2));
end
RPold = RPnew;
CCold = CCnew;
centsA = centsB;
Ds{ii} = displacements;
end
Best Answer