Hello everyone,

I need to know the total area covered by black pixels in a matlab figure. I do not know the polygons and can't use polyarea() or polybool(). So, as an example, I wrote a little code for estimating the total area of three squares (with two of them interlaced, and one "distant"). Here is the code:

`%Plot 3 squares (two of them are melted):`

%So exact surface is known (11).

x = [0,2; 0,0; 2,0 ;2,2; 0,2];y = x + 1;z = x + 5;h = figure('Color',[1 1 1]);plot(x(:,1),x(:,2),'b-'); hold on;plot(y(:,1),y(:,2),'r-');plot(z(:,1),z(:,2),'r-');axis equal;%axis square;

%patch:

patch(x(:,1),x(:,2),'k')patch(y(:,1),y(:,2),'k')patch(z(:,1),z(:,2),'k')axis off;%get actual frame:

f = getframe;%Binarization:

ZZ = rgb2gray(f.cdata) > 255/2;STATS = regionprops(double(~ZZ),'Area');%same as: sum(sum(~ZZ))

aire = STATS.Area;inter = axis;dx = (inter(2)-inter(1))/size(ZZ,1);dy = (inter(4)-inter(3))/size(ZZ,2);totalarea = aire*(dx*dy)

My question is that 'totalarea' returns approximatively 11.xxx (arround 10% of error, depending of the screen resolution). So the estimation is very bad for such a simple example with squares. The shapes I intend to estimate are way more curvy and complicated… It seems to me that the estimation is resolution dependant, and I'm wondering how to improve the code further so that the error fall beyond 0.1% (or best)? Is it possible?

Thank you in advance dear matlab users.

Bests

## Best Answer