Save plot to image variable
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 6 Juin 2018
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.
Réponse acceptée
Jan
le 7 Juin 2018
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 commentaires
Jan
le 8 Juin 2018
@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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Convert Image Type dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!