Faster Alternative to export_fig
이전 댓글 표시
Hi,
A while back, I asked if there was any way I could convert an axis into an image matrix. I was pointed to export_fig, which while certainly is functional takes a while for each image you export. The program I am running converts 90 figures into matrices, so you can see how this time adds up. Additionally, I will need to use the same program for a much larger dataset soon, so I'd really like to increase the program efficiency if I can. Essentially, I have several shapes stored in a .mat file which must be converted into an image matrix of 1's (area of a shape) and 0's (area of nothing).
My current process is as follows:
- Create a current axis of the same dimensions as the final image.
- Draw a solid black image to the current axis.
- Load each shape to the workspace and draw on the current axis (on top of the black).
- Export the current figure with export_fig into an image matrix.
- Sum the resulting image matrix (so that you have a 2D matrix instead of a 3D/RGB matrix)
- Divide the image matrix by itself so that all 0's remain 0 but all non-zero values are converted to 1.
I wanted to reclarify my initial question and see if anyone else had additional ideas. I need a tool/method that:
- Draws several shapes on an axis which are stored from a .mat file.
- Creates a matrix of zeros of the same dimensions (e.g., a 1600 x 900 axis would become imgMatrix = zeros(900,1600))
- For every pixel with color, the corresponding location in the matrix becomes a 1 (e.g., if 1400,700 falls within a shape then imgMatrix(700,1400) = 1)
To sum, I start with nothing but several shapes stored in a .mat file. I need to end up with a matrix which contains those shapes "drawn" onto the matrix where 1's represent area covered by the shape and 0's represent area outside of the shapes.
댓글 수: 8
From my initial reading of your problem description it does not seem like you ever need to draw the shapes to an axis. I think you should be able to directly construct your "image matrix" just using MATLAB matrix operations. If this is the case, then you do not have to worry about how to export figures more quickly, you can just focus on doing your matrix manipulations efficiently. How are the shapes currently defined?
Joseph Henry
2019년 8월 27일
Before I saw your comment indicating that you had ellipse objects I made the attached example that shows how you could do it with polygons. Probably a similar approach could be adapted for ellipses. Maybe this will give you a starting point.
% provide matrix dimension
m = 900;
n = 1600;
% define a few shapes as polygons
% x coordinates are column numbers in image matrix
% y coordinates are row numbers in image matrix
shape(1) = polyshape([300 400 600],[200,800,300]) % triangle
shape(2) = polyshape([100 800 800 100],[700 830 1030 900]) % parallelogram
% initialize image matrix
imgMatrix = zeros(m,n);
% make a vector with all of the "x" value coordinates (column numbers)
% and a vector with all of the corresponding "y" value (row numbers)
[y,x] = ind2sub([m,n],1:m*n);
% loop through shapes setting elements of image matrix inside of each shape to true
for k = 1:length(shape)
inside = isinterior(shape(k),y,x);
imgMatrix(inside) = 1;
end
% display the image matrix
imshow(imgMatrix)
I don't have the image processing toolbox so I can't load your .mat file.
Maybe there is a better approach, but one way (extending what I have outlined above) would be to 1) make a function (or find one on the Matlab File Exchange) that could provide the vertices of your elipse approximated as a polygon, and 2) then use the above approach. Note I don't think that the vertices will need to be integers (exact column and row coordinates) as in my simple example for this to work.
Joseph Henry
2019년 8월 27일
Walter Roberson
2019년 8월 27일
isinterior() was introduced in R2017b; which release are you using?
Joseph Henry
2019년 8월 27일
편집: Joseph Henry
2019년 8월 27일
Walter Roberson
2019년 8월 27일
Image Analyst's demo creates rotate ellipses from points and lines joining them. The coordinates of those lines can be put into the insertShape() function of Computer Vision Toolbox, to draw ellipses into a matrix.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!