matlab:归一化距离图

时间:2012-08-08 11:57:35

标签: matlab

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

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)),我会得到所有其他像素的标准化距离(x y z位置格式):

>> 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

我只是想知道我是否以正确的方式这样做(在这个阶段对效率不太感兴趣)

1 个答案:

答案 0 :(得分:1)

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

使用meshgrid可以更轻松地完成xpypXY的初始化:

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

我不会将'norm'用作变量名,因为它也是function

vecdir是正确的; dist也是,但实际上,它应该与norm(vecdir(i,:),2)(函数norm()相同,而不是您的变量!)

应用这个yiels:

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

是imo how you usually normalize a vector。当然,你得到了正确的结果,但由于你已经有了距离向量,因此没有必要使用pdist2,你只需要将其标准化。