2012-08-08 62 views
1

我想取一个位置网格,然后计算每个像素的归一化距离。我不知道这是做正确的方式: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 

在那里,如果我拿第一像素的3×3的图像(大小(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 

我只是想知道,如果我这样做的正确的方式(在这个阶段不要太费心约效率)

+0

你是什么意思与归一化距离?标准化通常意味着:在0和1之间,但是你最小和最大的参考是什么? – 2012-08-08 12:09:47

+0

嗨Gunther - 是的就是这样。它是网格上两点之间的单位矢量方向,除以这两点之间的距离。在我的情况下,我除以距离的平方。 – brucezepplin 2012-08-08 12:24:48

+0

不,结果是单位矢量,你现在有了真实的距离矢量,所以你想用那个真实距离矢量的2-范数来除。我对吗?也看到我的回答 – 2012-08-08 12:31:42

回答

1

不是一个问题的答案,但此言约代码:

xpypXY整个初始化可以更容易地与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 

我不会用 '规范' 作为变量名称,因为它也是function

vecdir是正确的; dist也不过essentialy,它应该是一样norm(vecdir(i,:),2)(功能norm(),不是你的变量!)

运用这一yiels:

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

这是海事组织how you usually normalize a vector。你得到了正确的结果,当然,但使用pdist2是没有必要的,因为你已经有了距离矢量,你只需要对它进行归一化。

+0

更新感谢Gunther,将编辑我的代码与您的建议 – brucezepplin 2012-08-08 12:11:12