brucezepplin
brucezepplin

Reputation: 9782

matlab : normalised distance map

I am trying to take a a position grid, and then calculate the normalised distances from each pixel. I'm not sure if this is the right way to do it:

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

where if I take the first pixel in a 3x3 image (size(im)) I get the normalised distances to all other pixels (in x y z position format) as:

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

I just wanted to know if I am doing this the right way (not too bothered about efficiency at this stage)

Upvotes: 1

Views: 1187

Answers (1)

Gunther Struyf
Gunther Struyf

Reputation: 11168

Not an answer to the question, but remark about the code:

the whole initialization of xp, yp, X and Y can be done more easily with meshgrid:

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

Regarding the question itself:

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

I wouldn't use 'norm' as a variable name as it is also a function

vecdir is correct; dist also, but essentialy, it should be the same as norm(vecdir(i,:),2) (the function norm(), not your variable!)

Applying this yiels:

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

which is imo how you usually normalize a vector. You got the right result, sure, but using pdist2 wasn't necessary as you already had the distance vector, you only needed to normalize it.

Upvotes: 1

Related Questions