Faster Alternative to export_fig

7 vues (au cours des 30 derniers jours)
Joseph Henry
Joseph Henry le 27 Août 2019
Commenté : Joseph Henry le 28 Août 2019
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:
  1. Create a current axis of the same dimensions as the final image.
  2. Draw a solid black image to the current axis.
  3. Load each shape to the workspace and draw on the current axis (on top of the black).
  4. Export the current figure with export_fig into an image matrix.
  5. Sum the resulting image matrix (so that you have a 2D matrix instead of a 3D/RGB matrix)
  6. 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:
  1. Draws several shapes on an axis which are stored from a .mat file.
  2. Creates a matrix of zeros of the same dimensions (e.g., a 1600 x 900 axis would become imgMatrix = zeros(900,1600))
  3. 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 commentaires
Joseph Henry
Joseph Henry le 27 Août 2019
Modifié(e) : Joseph Henry le 27 Août 2019
I am using R2019a.
>> which isinterior
C:\Program Files\MATLAB\R2019a\toolbox\matlab\polyfun\@polyshape\isinterior.m % polyshape method
Sorry, how would I use the ellipse formula to help my problem?
Walter Roberson
Walter Roberson le 27 Août 2019
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.

Connectez-vous pour commenter.

Réponse acceptée

Jon
Jon le 28 Août 2019
Modifié(e) : Jon le 28 Août 2019
Improving somewhat on my earlier comment you could do something like this
% provide matrix dimension
m = 900;
n = 1600;
% define a few ellipses
% x coordinates are column numbers in image matrix
% y coordinates are row numbers in image matrix
ellipse(1).xf = [300 400];
ellipse(1).yf = [350 500];
ellipse(1).a = 200;
ellipse(2).xf = [500 800];
ellipse(2).yf = [700 100];
ellipse(2).a = 400;
% 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(ellipse)
inside = inEllipse(ellipse(k).xf,ellipse(k).yf,ellipse(k).a,x,y);
imgMatrix(inside) = 1;
end
% display the image matrix
imshow(imgMatrix)
where I have made the inEllipse function
function tf = inEllipse(xf,yf,a,xq,yq)
%INELLIPSE determine if query points are inside of specified ellipse
% tf = inEllipse(xf,yf,a,xq,yq) returns a vector of ones and zeros
% corresponding to the query points xq,yq with tf(i) = 1 when xq(i),yq(i)
% is in the interior of an ellipse with focus points xf(1),yf(1) and
% xf(2),yf(2) where the sum of the distances to any point on the ellipse to
% the two foci equals 2a
%
% find distances from query points to foci
d = sqrt([(xf(1) - xq(:)).^2 + (yf(1) - yq(:)).^2,...
(xf(2) - xq(:)).^2 + (yf(2) - yq(:)).^2]);
% determine if sum of distances is less than 2*a
tf = sum(d,2) <= 2*a;
You will have to do a little more work to get the foci and and value of a used in my definition of an ellipse from the properties of your ellipse, center, semiaxes etc. I just wanted a straightforward definition of an ellipse to illustrate the idea.
  4 commentaires
Jon
Jon le 28 Août 2019
Glad that this approach is working for you. Sorry you had some problems not realizing the rotation angle in my function was in radians. I was hoping you would see it in the example where I do the conversion, but I should have made it more explicit. I think for the most part MATLAB takes radian arguments to trig functions, but your output from the roi is in degrees so I guess you have to be careful.
Joseph Henry
Joseph Henry le 28 Août 2019
No worries, I should have looked more carefully. All is well though, thank you again!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by