我有一个 Nx2 数组K1
,其中包含 N 个关键点的位置和一个 3 维 WxHx3 数组Kart1(width,height,coordinates)
,它将坐标映射到图像的每个像素。对于中的每个关键点,K1
我想读取像素的位置Kart1
并评估其周围 3x3 内核中的坐标(搜索最小值/最大值或计算平均值),并为 中的当前像素分配一个值KPCoor1
。
我目前的方法如下所示:
for ii=1:length(K1(:,1)) %for every keypoint in K1
MinDist=sqrt(sum(Kart1(K1(ii,2)-1,K1(ii,1)-1,:).^2)); %Calculate distance
xShift=0;
yShift=0;
for kk=-1:1 %for every pixel in a 3x3 kernel...
for ll=-1:1
Distance=sqrt(sum(Kart1(K1(ii,2)+kk,K1(ii,1)+ll,:).^2));
if Distance<MinDist %... if the current distance is smaller than MinDist
MinDist=Distance; %... update MinDist...
xShift=kk; %... and take the kernel coordinate of the pixel
yShift=ll;
end
end
end
KP1Coor(ii,:)=Kart1(K1(ii,2)+xShift,K1(ii,1)+yShift,:); %assign the coordinates of the pixel with the minimal distance in kernel.
end
它运行,但很丑,我怀疑它正在做我想做的事。我对这件事的“多维性”有点困惑,不知道评估内核的许多函数,也想不出一种方法来使用矢量化函数bsxfun()
或逻辑运算(意味着我被卡住了,我的大脑是干燥的 :/)
关于如何消除这些循环/更正代码的任何建议?