1

我正在尝试采用位置网格,然后计算每个像素的归一化距离。我不确定这是否是正确的方法:

clear all;
im = imread('test1.png');                   % read in the image
im = im(:,:,1);                                %vectorize image

n = size(im,1);   % No of grids in the X-Y plane

xp(1:n)=1:1:n; % Y-coordinates of the plane where we are interested
yp(1:n)=1:1:n;% X-coordinates of the plane where we are interested

Y(1:n,1:n)=0; % This array is for 1-d to 2-d conversion of coordinates
X(1:n,1:n)=0;

for i=1:n
    Y(i,:)=yp(i); % all y-coordinates value in 2-d form
end
for i=1:n
    X(:,i)=xp(i);% all x-coordinates value in 2-d form
end

Z = zeros(size(X)); % Z dimension is appended to 0

pos = [X(:),Y(:),Z(:)];        %position co-ordinates for x y z dimensions

N = size(pos,1);               % size of position matrix
v = cell(N,1);                 %define cell for storage of x-y plane direction vectors
for j = 1:N
    for i = 1:N
        vecdir(i,:) = pos(i,:) - pos(j,:);               %direction of vectors between each point in the x-y plane
        dist(i,:) = pdist2(pos(i,:),pos(j,:));           %distance between each point in the x-y plane
        norm(i,:) = vecdir(i,:)./(dist(i,:)).^2;         %normalised distance between each point in the x-y plane
    end
    v{j} = vecdir;
    d{j} = dist;
    r{j} = norm;                                         %store normalised distances into a cell array

end

R = cellfun(@isnan,r,'Un',0);
for ii = 1:length(r)
r{ii}(R{ii}) =0;
end

如果我在 3x3 图像(大小(im))中获取第一个像素,我会得到到所有其他像素(以 xyz 位置格式)的归一化距离为:

>> r{1}

ans =

         0         0         0
         0    1.0000         0
         0    0.5000         0
    1.0000         0         0
    0.5000    0.5000         0
    0.2000    0.4000         0
    0.5000         0         0
    0.4000    0.2000         0
    0.2500    0.2500         0

我只是想知道我是否以正确的方式这样做(现阶段不太关心效率)

4

1 回答 1

1

不是问题的答案,而是对代码的评论:

xpypX的整个初始化Y可以使用meshgrid更轻松地完成:

xp=1:n;
yp=xp;
[X,Y]=meshgrid(xp,yp);

关于问题本身:

vecdir(i,:) = pos(i,:) - pos(j,:);               %direction of vectors between each point in the x-y plane
dist(i,:) = pdist2(pos(i,:),pos(j,:));           %distance between each point in the x-y plane
norm(i,:) = vecdir(i,:)./(dist(i,:)).^2;         %normalised distance between each point in the x-y plane

我不会使用“规范”作为变量名,因为它也是一个函数

vecdir是正确的; dist也,但本质上,它应该与norm(vecdir(i,:),2)(函数norm(),而不是你的变量!)

应用这个产量:

vecdir(i,:) = pos(i,:) - pos(j,:);
normvec = vecdir(i,:)./norm(vecdir(i,:),2);

这是 imo你通常如何规范化 vector。当然,你得到了正确的结果,但pdist2由于你已经有了距离向量,所以不需要使用,你只需要对其进行归一化。

于 2012-08-08T12:06:52.610 回答