draw line in a picture

112 ビュー (過去 30 日間)
lena kappa
lena kappa 2021 年 3 月 26 日
編集済み: Walter Roberson 2021 年 4 月 4 日
Hello guys does anyone know how can i draw a line in a picture.
For example i want to draw a 45 degree red line in the next picture starting from the top left corner of the picture and ending n the other end of the picture.

採用された回答

Walter Roberson
Walter Roberson 2021 年 3 月 26 日
編集済み: Walter Roberson 2021 年 4 月 4 日
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/562881/image.jpeg');
sz = size(img);
endx = min(sz(1), sz(2));
h(1) = image(img);
hold on
h(2) = plot([1 endx], [1 endx], 'r');
hold off
Or if you need the line "burned in" to the image:
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/562881/image.jpeg');
sz = size(img);
endx = min(sz(1), sz(2));
nimg = insertShape(img, 'Line', [1 1 endx endx], 'Color', 'r');
h(1) = image(nimg);
The difference between these two is that in the first version, the red line is only visible in what is drawn, with it not being made part of the image array; h will end up as a vector containing the handle to the image display object that only knows about the gray scale image, and a handle to the line display object. In the second version, the red line is part of the array stored in nimg, suitable for further processing (or writing to a file), and h will be the handle of the image display object that only knows about displaying the array that inherently has the red line as part of the data.
  11 件のコメント
lena kappa
lena kappa 2021 年 3 月 31 日
Thank you Walter for the help!
Walter Roberson
Walter Roberson 2021 年 4 月 4 日
I corrected the insertShape call,
nimg = insertShape(nimg, 'Line', [0 0 endx endy], 'Color', line_cols{k});

サインインしてコメントする。

その他の回答 (1 件)

MG
MG 2021 年 3 月 26 日
Would this work for you:
hold on
ax=axis;
x = ax(1:2);
y = ax([3 4]); %might want to change cordinates (check with 'axis on')
line(x,y,'Color','red','LineStyle','-')
  4 件のコメント
Walter Roberson
Walter Roberson 2021 年 3 月 26 日
編集済み: Walter Roberson 2021 年 3 月 26 日
In my code, the approaches do not change if you use imshow() instead of image()
lena kappa
lena kappa 2021 年 3 月 26 日
thanks for the answer michael

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by