開始座標と終了座標がわかっている場合、MATLAB で白黒 (バイナリ) 画像の上に線を引く最良の方法は何ですか?
注釈行を追加しようとしているわけではないことに注意してください。線を画像の一部にしたいです。
開始座標と終了座標がわかっている場合、MATLAB で白黒 (バイナリ) 画像の上に線を引く最良の方法は何ですか?
注釈行を追加しようとしているわけではないことに注意してください。線を画像の一部にしたいです。
画像マトリックスに行を追加することに関する 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
結果の画像は次のとおりです。
他の方法の例外的なケースに悩まされている場合は、次の行になる防弾方法があります。
入力 (このコードから関数を作成するのに便利):
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 本の線が描かれたサンプル画像を次に示します。
このアルゴリズムは 1 つのアプローチを提供します。