Polygon/patch data to RGB image matrix
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Kyle Chudler
am 18 Sep. 2015
Kommentiert: Kyle Chudler
am 18 Sep. 2015
I am looking for a way to convert a list of polygons into an RGB image matrix.
I have a vector of polygon data structures, where each polygon has the following:
polygon.x: x-coordinate of vertices that make up the polygon
polygon.y: y-coordinates of verticies
polygon.r/g/b: R/G/B color components of polygon face color
polygon.alpha: alpha (transparency) component of polygon face color
All these polygons are stacked on top of each other to make a final picture.
I am looking for a way to convert this picture into a RGB image matrix (X-by-Y-by-3 where X and Y are the size of the image).
I know I could do this by plotting each polygon stack with the patch function, and then doing some combination of getframe and frame2im, but I will be needing to do this with tens of thousands of images (as in tens of thousands of seperate lists of polygons), so plotting each one would be inefficient.
Let me know if you need any more clarification.
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 18 Sep. 2015
total_rows = 1328; %adjust as needed
total_cols = 2719;
background_r = 7; %adjust as needed
background_g = 1;
background_b = 44;
total_image = zeros(total_rows, total_cols, 3);
total_image(:,:,1) = background_r;
total_image(:,:,2) = background_g;
total_image(:,:,3) = background_b;
pane_size = total_rows * total_cols; %we need it to index easily later
for K = 1 : length(polygons)
polygon = polygons(K);
thismask = poly2mask(polygon.x, polygon.y, total_rows, total_columns);
thismask_idx = find(thismask);
total_image(thismask_idx + 0 * pane_size) = total_image(thismask_idx + 0 * pane_size) .* (1- polygon.alpha) + polygon.r .* polygon.alpha; %red plane
total_image(thismask_idx + 1 * pane_size) = total_image(thismask_idx + 1 * pane_size) .* (1- polygon.alpha) + polygon.g .* polygon.alpha; %green plane
total_image(thismask_idx + 2 * pane_size) = total_image(thismask_idx + 2 * pane_size) .* (1- polygon.alpha) + polygon.b .* polygon.alpha; %blue plane
end
In the above code, the fact that I initialized as zeros() with the default double data type allows the colors to be specified as intensities in the 0 to 1 range, but also allows 0-255 or 0-65535 or whatever other color range is appropriate for the image you are building up. It also has the effect that intensities are not quantized to (say) uint8 after each operation, so a fractional result after the alpha calculation will propagate to all further calculations, with weaker and weaker effect (until an alpha of 1 is hit for that location.) At the end you can convert to an integer range with uint8().
This might not be exactly the same as would happen if you were to patch() everything into layers on the screen. If you do wish to work with uint8() range, and you do wish to have quantization after every step, then you can change the initialization to
total_image = zeros(total_rows, total_cols, 3, 'uint8');
and no further changes need to be made. The code was designed to make it easy for you to implement either behaviour.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Polygons finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!