Save plot to image variable

조회 수: 12 (최근 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 CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by