For the second part of my blog on area estimation, we will obtain the area of geometric figures from their dimensions (since we have the formulae to calculate the areas of these figures), pixel counting, and the previously discussed Green’s Theorem. For the latter method, we will also try different built-in edge-detection methods in Scilab.

Images of different geometric figures with known area formulas are generated using the code below. All images has dimensions of 1000 px 1000 px. Note that all images has white pixels (the ‘1’s) inside the region and black pixels (the ‘0’s) outside the region.

//create images of geometric figures //circle x = 1000; ny = 1000;//defines the number of elements along x and y x = linspace(-1,1,nx); //range y = linspace(-1,1,ny); [X,Y] = ndgrid(x,y);//creates 2D arrays of x and y coords A = zeros(nx,ny); r = sqrt(X.^2 + Y.^2); R = 0.5; A(find(r <= R))=1; imshow(A); clf; imwrite(A, 'circle.bmp'); //saved in My Documents //ellipse nx = 1000; ny = 1000;//defines the number of elements along x and y x = linspace(-1,1,nx); //range y = linspace(-1,1,ny); [X,Y] = ndgrid(x,y); //creates 2D arrays of x and y coords B = zeros(nx,ny); a = 1; b = 0.5; R_e = ((X./a).^2)+((Y./b).^2); B(find(R_e <= 1)) = 1; imshow(B); clf; imwrite(B, 'ellipse.bmp'); //rectangle nx = 1000; ny = 1000;//defines the number of elements along x and y x = linspace(-1,1,nx);//range y = linspace(-1,1,ny); [X,Y] = ndgrid(x,y);//creates 2D arrays of x and y coords C = zeros(nx,ny); l = 0.5; w = 0.2; C(find(abs(X) <= l & abs(Y) <= w))=1; imshow(C); clf; imwrite(C, 'rectangle.bmp'); //square nx = 1000; ny = 1000;//defines the number of elements along x and y x = linspace(-1,1,nx);//range y = linspace(-1,1,ny); [X,Y] = ndgrid(x,y);//creates 2D arrays of x and y coords D = zeros(nx,ny); L = 0.5; D(find(abs(X) <= L & abs(Y) <= L))=1; imshow(D); clf; imwrite(D, 'square.bmp'); //diamond nx = 1000; ny = 1000;//defines the number of elements along x and y x = linspace(-1,1,nx);//range y = linspace(-1,1,ny); [X,Y] = ndgrid(x,y);//creates 2D arrays of x and y coords E = zeros(nx,ny); s = 0.5; R_d = abs(X)+abs(Y); E(find(R_d <= s))=1; imshow(E); clf; imwrite(E, 'diamond.bmp'); //triangle nx = 1000; ny = 1000;//defines the number of elements along x and y x = linspace(-1,1,nx);//range y = linspace(-1,1,ny); [X,Y] = ndgrid(x,y);//creates 2D arrays of x and y coords F = zeros(nx,ny); m = 1.5; F(find(Y <= sqrt(3)*X + (0.25*m*sqrt(3)) & Y <= -sqrt(3)*X + (0.25*m*sqrt(3)) & Y >= -0.25*m*sqrt(3)))=1; imshow(F); clf; imwrite(F, 'triangle.bmp');

The generated images from the above code are shown below:

The code below shows the implementation of the pixel counting method and Green’s theorem method with areas expressed in terms of the ‘physical’ units by using a scale factor scale.

//import image and get coordinates of the edges of a figure gim = imread('J:\AP 186-187\186 - activity 4\triangle.bmp'); //from imwrite ed = bool2s(edge(gim,'prewitt')); //convert string 'T' and 'F' to 0 and 1 [x,y]=find(ed==1); //orientation of coordinate axes does not matter since we only need the area [m,n] =size(x); //m rows, n columns x0 = sum(x)/n; //mean value is origin y0 = sum(y)/n; X = x-x0; Y = y-y0; theta= atan(Y,X); //corresponding angular position of coordinates in radians [sorted_theta ,coord_index]=gsort(theta, 'g', 'i'); //get index of corresponding coordinates sorted by increasing angular position //plot the pixel location of edges with respect to new origin a = []; b = []; for i = 1:n a(i) = X(coord_index(i)); b(i) = Y(coord_index(i)); end plot(a,b); //Find area by counting pixels scale = (2/1000)^2 //there are 1000 px from -1 to 1 units A = find(gim > 0); //count the pixels inside the region [p,q]=size(A); //p=1 disp(q*scale); //Find area using Green's theorem coord_index(n+1)=coord_index(1); //closed boundary s = []; for i = 1:n s = s + X(coord_index(i))*Y(coord_index(i+1))-Y(coord_index(i))*X(coord_index(i+1)); end disp(0.5*s*scale);

The table below summarizes the results obtained using different methods of obtaining the area of different geometric figures. Note that when using the Green’s theorem, different built-in edge detection methods in Scilab (‘sobel’, ‘prewitt’, and ‘canny’) were used to distinguish the figure from the background. The percentages enclosed in parentheses are percent deviations from the analytical area.

Figure | Analytical Area | Pixel counting | Green’s (Sobel) | Green’s (Prewitt) | Green’s (Canny) |

Circle | 0.7853982 | 0.783856(0.2%) | 0.784044(0.17%) | 0.783868(0.19%) | 0.783844(0.2%) |

Ellipse | 1.5707963 | 1.567712(0.2%) | 1.568028(0.18%) | 1.567724(0.2%) | 1.5677(0.2%) |

Rectangle | 0.4 | 0.4(0%) | 0.402796(0.7%) | 0.400012(0.003%) | 0.399988(0.003%) |

Square | 1.0 | 1.0(0%) | 1.003996(0.4%) | 1.000012(0.001%) | 0.999988(0.001%) |

Diamond | 0.5 | 0.498(0.4%) | 0.498012(0.4%) | 0.498012(0.4%) | 0.497988(0.4%) |

Triangle | 0.9742786 | 0.971032(0.33%) | 0.972532(0.18%) | 0.971044(0.33%) | 0.972512(0.18%) |

It is expected that the pixel counting method is most accurate with squares and rectangles, (provided that they are not rotated) since their sides are parallel to the arrangement of the pixels. From these results, pixel counting is enough for first estimation of areas. Depending on the edge detection method, Green’s theorem method generally increases the accuracy compared to first estimations using pixel counting. I am not yet familiar on the mechanisms of these different edge detection methods, but maybe I can discuss it in a future blog after some reading.It also seems that the pixel dimension of the images matter to the accuracy of area calculations. For demonstration, I will use 100 px 100 px images of the same figures above, and calculate their areas. The table below shows the results from pixel counting and Green’s theorem (using the Prewitt method).

>

>

Figure | Pixel counting | Green’s (Prewitt) |

Circle | 0.7522222(4.22%) | 0.7488164(4.66%) |

Ellipse | 1.5023671(4.36%) | 1.4966908(4.72%) |

Rectangle | 0.3888889(2.78%) | 0.3818116(4.55%) |

Square | 0.9666667(3.33%) | 0.9570773(4.29%) |

Diamond | 0.4794686(4.11%) | 0.4756280(4.87%) |

Triangle | 0.9324879(4.29%) | 0.9284662(4.70%) |

It can be noticed, by comparing the previous tables, that the accuracy in area estimation dropped for images with smaller pixel dimensions. However, if I were to take screenshots, the pixel dimensions of the image is relatively small which could be a problem.

As a last note, I found this part really tedious, especially in the creation of the tables, since I have to manually check each entry. For me, this is a 10/10, since I managed to know some of the factors that can affect the accuracy of area estimation. As you can notice, I made these basic tables using HTML, which I learned from this site: https://wpbtips.wordpress.com/2013/04/09/table-coding-for-wordpress-com-users/. This ends the second part of my blog on area estimation in images. We learn something new everyday!