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 画像 (size(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およびの初期化全体は、 meshgridYを使用するとより簡単に実行できます。

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」は関数でもあるため、変数名として使用しません

vecdir正しい; また、しかし本質的に、それは(あなたの変数ではなく関数!)distと同じでなければなりませんnorm(vecdir(i,:),2)norm()

これを適用すると、次のようになります。

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

これは、通常、ベクトルを正規化する方法です。確かに正しい結果が得pdist2られましたが、距離ベクトルが既にあるため、使用する必要はありませんでした。それを正規化するだけで済みました。

于 2012-08-08T12:06:52.610 に答える