Save plot to image variable

13 ビュー (過去 30 日間)
Marc Franzen
Marc Franzen 2018 年 6 月 6 日
コメント済み: Marc Franzen 2018 年 6 月 11 日
Hi,
I have several lines (definded by startpoint x1, y1 and endpoint x2, y2). Now I want to print them directly onto an image without showing it as plot on the screen first. I want to write them directly over an existing image.
Background: I have the borders of an object through a hough-transformation and now I want to write those lines over the image for further processing (detecting the single objects in the image). Every answer so far was about plotting them and then using getframe and frame2img, but I want to avoid this out of performance and quality issues (images are larger than my screen, so the figure will get scaled -> information loss).
Thanks a lot in advance!
  2 件のコメント
Stephen23
Stephen23 2018 年 6 月 6 日
An image displayed on a screen is just an array/matrix anyway, so if you already have a raster image and you know the locations that you want to change, then you don't need to plot the data, you can simply change the required locations in the array/matrix directly.
In your case you have the start and end points of line segments, so you can calculate the intermediate points corresponding to that line. You will need to consider some kind of threshold or aliasing to decide if the line changes the values or not.
Marc Franzen
Marc Franzen 2018 年 6 月 7 日
Thanks for the reply. I also thought of that, but I can't think of a way on how to do that right now... Could you help me out with some example code?

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

採用された回答

Jan
Jan 2018 年 6 月 7 日
img = rand(100, 100, 3);
p1 = [30, 20]; % Start point
p2 = [90, 40]; % End point
nPoint = max(abs(p1 - p2)) + 1;
y = linspace(p1(1), p2(1), nPoint);
x = linspace(p1(2), p2(2), nPoint);
siz = size(img);
s = siz(1) * siz(2);
index = sub2ind(siz, round(y), round(x));
img(index) = 0; % R
img(index + s) = 0; % G
img(index + 2 * s) = 0; % B
image(img)
Isn't there a nicer method to set the RGB values?
  3 件のコメント
Jan
Jan 2018 年 6 月 8 日
@Stephen: I was afraid of this.
There is a need for applying 2D logical masks and a list of indices for the first 2 dimensions to a RGB array. Such code looks too awkward:
rgb = rand(200, 200, 3);
mask = rand(200, 200) < 0.1;
rgb(cat(3, mask, mask, mask)) = 0; % Wastes time!
Auto-expand does not work:
rgb(mask, 1:3) = 0 % Does not do what is wanted!
But writing a Mex script for this, cannot modify the array inplace, but creates a deep data copy, which might be a waste of time also. The old undocumented methods for shared data copies are not reliably anymore, and using the modern Mex API would exclude all users for Matlab < R2018a.
Marc Franzen
Marc Franzen 2018 年 6 月 11 日
Thank you very much! This is perfectly what I needed! In my case I don't even bother about RGB, because I use a logical Black/White mask for my later image. Thank you again!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by