Area Calculation Using polyarea: Figure vs PNG

I'm encountering an issue while working with the polyarea function. I've created a 2D scatter plot and used drawfreehand to select my roi and polyarea to calculate its area. However, when I save the figure as a PNG and recalculate the area from the image, the result is significantly larger. From what I can understand it has to do with how I am saving the figure to a png (?).
What I eventually want to do is have a png displaying a set of points and a figure displaying a different set of points and calculate their respective areas and compare them. The two will not be displaying the same thing necesserily so that why I need their areas to be comperable in dimensions.
I am attaching an example script.

 Accepted Answer

There is no spatial calibration when you save a figure, or a non-image graph in an axes on a figure. So the image you get upon saving the figure or axes is just basically a screenshot, so the area will depend on that size of the figure. If you resize the figure before saving you will get a different area.
You should be writing to a digital image and saving it with imwrite.

2 Comments

Thank you for your response. I am not resizing the figure at all. I tried saving using imwrite as well as:
F = getframe(gca);
F = frame2im(F);
[A,map] = rgb2ind(F, 256);
imwrite(A, map, 'File.png');
Still, when I calculate the area of a 1x1 square from the png I get something around 30000.
So 30,000 pixels, or sqrt(30000) pixels per unit. So your calibration factor is
unitsPerPixel = 1/sqrt(30000);
Multiply linear pixel distances by that and areas by the square of that to get quantities in real world units.
See attached demo.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!