14

開始座標と終了座標がわかっている場合、MATLAB で白黒 (バイナリ) 画像の上に線を引く最良の方法は何ですか?

注釈行を追加しようとしているわけではないことに注意してください。線を画像の一部にしたいです。

4

5 に答える 5

9

画像マトリックスに行を追加することに関する SO の質問に対する私の回答をご覧になることをお勧めします。その回答で私が持っているものと同様の例を次に示します。これにより、行と列のインデックスから まで走る白い線が作成されます。(10, 10)(240, 120)

img = imread('cameraman.tif');  % Load a sample black and white image
x = [10 240];                   % x coordinates
y = [10 120];                   % y coordinates
nPoints = max(abs(diff(x)), abs(diff(y)))+1;    % Number of points in line
rIndex = round(linspace(y(1), y(2), nPoints));  % Row indices
cIndex = round(linspace(x(1), x(2), nPoints));  % Column indices
index = sub2ind(size(img), rIndex, cIndex);     % Linear indices
img(index) = 255;  % Set the line points to white
imshow(img);       % Display the image

結果の画像は次のとおりです。

ここに画像の説明を入力

于 2010-03-17T18:51:31.863 に答える
5

他の方法の例外的なケースに悩まされている場合は、次の行になる防弾方法があります。

  • そのピクセルは、ラインの全長にわたって常に互いに接触しています (ピクセルは互いに 8 つの隣接点です)。
  • ラインの密度は、追加パラメータ に依存しませんが、最初のポイントからの保証に対応するために柔軟に決定されます。

入力 (このコードから関数を作成するのに便利):

  • img- 画像を含む行列
  • x1, y1, x2, y2- 描画する線の終点の座標。

コード:

% distances according to both axes
xn = abs(x2-x1);
yn = abs(y2-y1);

% interpolate against axis with greater distance between points;
% this guarantees statement in the under the first point!
if (xn > yn)
    xc = x1 : sign(x2-x1) : x2;
    yc = round( interp1([x1 x2], [y1 y2], xc, 'linear') );
else
    yc = y1 : sign(y2-y1) : y2;
    xc = round( interp1([y1 y2], [x1 x2], yc, 'linear') );
end

% 2-D indexes of line are saved in (xc, yc), and
% 1-D indexes are calculated here:
ind = sub2ind( size(img), yc, xc );

% draw line on the image (change value of '255' to one that you need)
img(ind) = 255;

3 本の線が描かれたサンプル画像を次に示します。 ここに画像の説明を入力

于 2013-01-13T21:47:03.353 に答える
3

このアルゴリズムは 1 つのアプローチを提供します。

于 2010-03-17T18:03:13.927 に答える