1

2行n列の行列として表されるポイントのセットがあります。これらのポイントは、接続された境界またはエッジを構成します。この輪郭を始点P1からトレースし、終点P2で停止する関数が必要です。また、時計回りまたは反時計回りの方向に輪郭をトレースできる必要があります。これは、Matlabの関数のいくつかを使用して実現できるかどうか疑問に思いました。

独自の関数を作成しようとしましたが、これにはバグがたくさんあり、インデックスの使用bwtraceboundaryとインデックス作成も試みましたが、マトリックス内のポイントが輪郭を作成する順序になっていないため、問題のある結果になります。

よろしくお願いします。

ところで、私はポイントのセットのプロットへのリンクを含めました。手の輪郭の半分です。

この関数は、理想的には、赤い星から緑の三角形までの輪郭をトレースします。トラバーサルの順序でポイントを返します。

編集:これはおそらく私が解決しようとしているより大きな問題の回避策ですが、青い境界エッジ上の点が赤い星または緑の三角形の点の間にある輪郭に接続されているかどうかをテストすることは可能でしょうか。

つまり、青い境界上のポイントの場合、左の赤いアステリックスから緑の三角形まで手作業で輪郭をトレースすると、そのポイントが2つのポイント間の接続された境界上にある場合は関数がtrueになり、そうでない場合はfalseになります。

代替テキストhttp://img717.imageshack.us/img717/9814/hand1.png

4

1 に答える 1

2

ポイントが非常に接近している場合は、常にリスト内の次に近いポイントを探すことでトレースを実行できるはずです。

ポイントがさらに離れている場合、問題は解決できません。4 つがコーナーで 1 つが中央にある 5 つのポイントを想像してみてください。線をトレースする「正しい」方法は何ですか?

%%# create some points
npts = 100;
x = linspace(-1,1,100)'; %'
y = 1 - x.^2;
pts = [x,y];

%# shuffle the points
newOrder = randperm(npts);
pts = pts(newOrder,:);

%# find index of start, end point
startIdx = find(newOrder == 1);
endIdx = find(newOrder == npts);

%# this brings us to where you are - pts as a nx2 array
%# startIdx indicates the star, and endIdx indicates the triangle.

%# pre-assign output - traceIdx, which contains the ordered indices of the point on the trace
traceIdx = NaN(npts,1);

%# create distance matrix
distances = squareform(pdist(pts));

%# eliminate zero-distance along the diagonal, b/c we don't want points linking to themselves
distances(logical(eye(npts))) = NaN;

%# starting from startIdx: always find the closest next point, store in traceIdx,
%# check whether we've arrived at the end, and repeat if we haven't
done = false;
traceCt = 1;
traceIdx(1) = startIdx;

while ~done
    %# find the index of the next, closest point
    [dummy,newIdx] = min(distances(traceIdx(traceCt),:));

    %# store new index and up the counter
    traceCt = traceCt + 1;
    traceIdx(traceCt) = newIdx;

    %# check whether we're done
    if newIdx == endIdx
        done = true;
    else
        %# mask the backward distance so that there's no turning back
        distances(newIdx,traceIdx(traceCt-1)) = NaN;
    end %# if
end %# while ~done

%# remove NaNs
traceIdx(~isfinite(traceIdx)) = [];

%# plot result with a line connecting the dots to demonstrate that everything went well.
figure,
plot(pts(traceIdx,1),pts(traceIdx,2),'-o')
hold on,
plot(pts(startIdx,1),pts(startIdx,2),'*r')
plot(pts(endIdx,1),pts(endIdx,2),'>g')
于 2010-04-17T13:40:41.227 に答える