Activity #4 – Length and Area Estimation in Images

Digitization has not only helped in generating synthetic images and digital reconstruction of old graphs and images, but also became essential in the estimation of object size using 2-D images. This has led to great improvements in research and development because not all objects are measurable with the cheap equipment. Measuring the size of microscopic specimen like bacteria and cancer cells, area of large buildings and structures, and even a land mass from the moon or Mars can be easily done using image manipulation. The thing is, one should be able to capture the image of the said objects, and be able to understand the underlying concepts behind area estimation. So, in this activity, we’ll estimate areas of simple shapes using only its image in pixel coordinates and an image processing software. Then, we’ll extend this to estimation of land area of known structures from their images in Google Maps.

Before anything else, I just want to clear out the aim of this activity. In the wondrous world of physics, we do not only simply solve physics problems using math, but also prove and validate theorems and hypotheses. My aim in this blog post is to prove a known theorem, which is called Green’s theorem. One could say that I can just count the number of pixels enclosed by a shape and that would be a much more reliable and easier area estimation. The thing is, I want to show the validity of Green’s theorem when it comes to area estimation, so in my results, I would compare pixel counting and area estimation via Green’s theorem. Besides, physics would be more enjoyable if we are to explore more things in life.

The principle behind area estimation through image processing via Green’s theorem is contour integration. Consider a closed region whose area is to be estimated: Green’s theorem states that the double integral (differential area) of the region is related to the contour integral encompassing the said region. For two functions F1 and F2  which are continuous along the boundary of the region and have continuous partial derivatives everywhere in the region, Green’s theorem reads as [1]:

Green's Formula 2

If we choose the functions F1 = 0, F2 = x and F1 = -y, F2 = 0 for the path along the boundary of the region, then this equation becomes:

Green's Formula 1

The discrete form of this equation for Nb pixels in the image is then given by [1]:

Green's Formula

Essentially, this equation is the main area computing formula in the computational estimation of image area. The pixels encompassing the region of an image can then be used in computing the area of an image.

The first part of this activity is to verify Green’s theorem for finding areas of regular shapes. From Activity #3 (Scilab Basics), I generated 3 regular shapes (Circle, Square and Triangle apertures) to test Green’s theorem. It is essential that the image to be processed are single channel images. Meaning, the images must be black and white, consisting only of pixel values 1 and 0. The images involved are shown below.

Shapes
Figure 1. Single channel images of a) circle, b) square and c) triangle aperture images. Pixel dimensions of each image is 1000 x 1000 pixels.

The next step is to use edge-finding techniques to determine the pixel coordinates of the contour. In Scilab, there is an edge() function from the SIVP toolbox. This function finds the edges for a single channel images. It’s pretty convenient for us. I was also surprised when I found out that these functions comes with different variations. Here are the list of edge finding techniques (from Sourceforge (SIVP)):

  • Sobel – detects edges using the Sobel gradient estimator. Named after Irwin Sobel and Gary Feldman, the Sobel-Feldman operator is based on convolving the image with a small, separable, and integer-valued filter in horizontal and vertical direction, and is inexpensive in terms of computations, although its approximation is relatively crude (from Wikipedia).
  • Prewitt – detects edges using the Prewitt gradient estimator. Named after Judith M. S. Prewitt, the Prewitt operator is similar to that of Sobel-Feldman operator, since both are based on convolution of images to small, separable, and integer-valued filter in horizontal and vertical directions (from Wikipedia).
  • fftderiv – detects edges using the Fast Fourier Transform (FFT) gradient method.
  • log – detects edges using the Laplacian of Gaussian method.
  • Canny – detects edges using Canny method. Named after John F. Canny, the Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images (from Wikipedia).

There are other edge detection techniques such as Roberts and fuzzy logic method, but only these five listed are the ones that I have used in this activity. Moving on, the Scilab code that I used for this activity is shown below.

Code
Code for Green’s theorem.

Let me run through the code. An edge-detection technique (in this case, it’s Sobel) is first applied to the single channel image. The pixel coordinates are then obtained using the find() function. The centroid of the image is first calculated by obtaining the mean of the edge coordinates. The coordinates are then converted to polar coordinates by finding r and θ using atan(). The coordinates are then arranged into increasing angle (counter-clockwise direction) using gsort(). Green’s theorem (discrete form) is then applied to the coordinates. The analytical area is then computed by counting the number of white pixels (value = 255) in the image. The computed and analytical area are compared.

The results of the five edge-detection techniques for the circle is shown below.

Figure 2. Images of circle after (a) Sobel, (b) Prewitt, (c) log, (d) fftderiv, and (e) Canny edge-detection.
Figure 2. Images of circle after (a) Sobel, (b) Prewitt, (c) log, (d) fftderiv, and (e) Canny edge-detection.

These five edge-detection techniques were also done for the square and triangle images.

Squares
Figure 3. Images of square after (a) Sobel, (b) Prewitt, (c) log, (d) fftderiv, and (e) Canny edge-detection.
Triangles
Figure 4. Images of triangle after (a) Sobel, (b) Prewitt, (c) log, (d) fftderiv, and (e) Canny edge-detection.

On these results, the Sobel technique seems to produce very thin edges. As seen in Fig. 3a, the four corners of the square are the only visible part in the image. The thickest image would be on the log technique. The Canny and fftderiv techniques have the finest image results. Moving on, the areas computed for each image are then compared with analytical results, and summarized in Table 1.

Table 1. Summary of computed areas and their deviation from analytical area.

Table 1

From Table 1, we observe high deviations resulting from the Sobel technique. Both Prewitt and fftderiv seem to have similar area results, while the log technique shows lowest error in all of the results. The area of the square in the Sobel method showed high deviation, which would deem Sobel unreliable for computation of square area. The Canny method also showed minimal error. Nevertheless, consistency is observed for each technique, and all are viable since most error values are less than 5%. Part 1 gives good verification for Green’s theorem. One could also infer that the lowest error from all the techniques vary from shape to shape.

Now, to further justify Green’s theorem’s accuracy in determining area, I will determine the areas of irregularly shaped objects via Green’s theorem. In this activity, we are assigned to pick a specific place in Google Maps, and estimate its area using Green’s theorem. Well, I chose Farmers Market in Cubao as the destination. Below are the top-view images of Farmers Market obtain from Google Maps and Google Earth.

Figure 5. (a) Google Earth and (b) Google Maps image of Farmers Market in Cubao.
Figure 5. (a) Google Earth and (b) Google Maps image of Farmers Market in Cubao.

The first thing to do is to convert the image into a single channel image (black and white). So, I first used paint to eliminate all annotations and labels, then filled the region outside the area black and white inside it. Using Scilab’s rgb2gray() function, the image is finally converted into a single channel image, shown below.

Figure 6. Black and white image of Farmers Market.
Figure 6. Black and white image of Farmers Market.

The code for Green’s theorem is then used for this image. The five methods were also used in this image. To convert pixel units to meters, the scaling in the Google Maps (lower right portion in Figure 5) was used. The image is loaded into MS Paint, and using the knowledge from Activity #2 (Digital Scanning), I was able to obtain pixel-to-meter ratio. To obtain its actual area, I used the distance and area measurement technique in Google Maps, where you have to trace along the sides to cover the whole area. Google Maps will automatically give the approximate distance covered and area, which is 13394.24 sq. meters. Of course this is another area estimate, and that the estimate will be only as good as the user’s tracing of the boundaries. But for now, this will suffice for a reliable comparative value. Figure 7 provides the images after edge-detection techniques and  Table 2 summarizes the areas obtained for each method used.

Figure 7.
Figure 7. Images of Farmer’s Market after (a) Sobel, (b) Prewitt, (c) log, (d) fftderiv, and (e) Canny edge-detection.

 Table 2. Summary of computed area values for Farmers Market and corresponding % error.

Table 2

Surprisingly, in this table, all methods seems to agree with each other. very low percentage errors were obtained for each method. Although it seems inconsistent with the first table, the log values has the highest deviation. These errors are combinations of several factors. First is the error associated with the technique, as shown in Table 1. Second, the actual area obtained from Google Maps might have affected the error. Nevertheless, Green’s theorem is still viable in the procedure since the deviation is low.

Since I am not contented with Google Maps area estimation, I would try to go for a bigger location, with a reliable political boundary. It’s time to go bigger!

“Bigger is Better.”

So, with that said, I would now measure the land area of the home of my favorite Las Vegas, the renowned state of Nevada. The shape of Nevada is pretty irregular compared to rectangular states like Colorado and Wyoming, as shown below.

Figure 7. Google Maps image of Nevada.
Figure 8. Google Maps image of Nevada.

Again, similar to Farmers Market area computation, the image is converted to single channel image.

Figure 8. Single channel image of Nevada.
Figure 9. Single channel image of Nevada.

Let’s now see the results. Figure 10 presents the images of the edge-detected Nevada, and Table 3 summarizes the areas obtained.

Nevadas
Figure 9. Images of Nevada after (a) Sobel, (b) Prewitt, (c) log, (d) fftderiv, and (e) Canny edge-detection.

Table 3. Summary of computed area values for Nevada and corresponding % error.

Table 3

The land area was obtained from Wikipedia. Again, we observe similar images of edge-detected Nevada, with Sobel model with the thinnest edge. From Table 3, we observe relatively high deviation from both Sobel and fftderiv method, while log method again shows the lowest deviation. The error would have come from the conversion of the image to black-and-white, and error from the method itself. Also, the image must be more defined and reliable to be able to reach the actual area. Nevertheless, it was successful since the deviations were lower than 5%.

We’re not done yet! The next part of this activity is to introduce another method of estimating image length and area. This time, I’ll be introducing a new image processing software, which is called ImageJ. ImageJ was developed by the National Institute of Health, US primarily for measuring microscopic images [1]. It is free and Java-based. In this part, I’ll show how to measure the size of an object digitally using a physically obtained measurement.

First, a scanned image of an object is uploaded to ImageJ. In this case, I used my driver’s license card as the object.

Figure 11. Scanned image of my beautiful driver's license card.
Figure 11. Scanned image of my driver’s license card.

The next step is to draw a straight line using ImageJ along one side of the object. After that, the set scale function in Analyze tab of ImageJ shows the pixel-to-actual distance ratio. The known distance is the physically measured distance of the side of the card using a ruler. Shown below is my data.

Figure 12. Picture scaling using actual distance and image distance. Pixel ratio is set to 1.0.
Figure 12. Picture scaling using actual distance and image distance. Pixel ratio is set to 1.0.

Now, using ImageJ’s free-hand and shape generating tool, I measured the area of the card by making a rectangle shape similar in size with the card. Then, by using ImageJ’s Measure function, the program automatically gives me the area of the rectangle in the units given by Figure 12. The area that I obtained from ImageJ is 46.477 sq. cm, while the actual measurement, by using a ruler, is 46.17 sq. cm. The deviation is about 0.66%, which is actually good. Now this error may arise from the fact that the card has rounded corners, in which it would give a slight decrease in actual area. Of course, the least significant value or uncertainty of the ruler may have contributed to the deviation.

Since I am not satisfied with this results, I resort to checking the method by finding a regular shape. I used ImageJ to measure the area of my picture in the card and compared it with the actual measurement. I obtained an area of 4.357 sq. cm from ImageJ, and an area of 4.32 sq. cm from measurement by ruler. The deviation is about 0.85%, which is actually very good. Although there was some slight deviation, this can be improved by using a more precise ruler. But yeah, those are not easy to find.

And so that ends this tedious activity. Green’s theorem is proved to be a very viable method of estimating image area. Although there are many techniques such as Sobel, Canny, and log, one can compare these methods so quickly since these are all installed in the SIVP toolbox of Scilab. Many of my classmates would say Canny is the best, or even Prewitt, but I think log beats them all. But I’m only saying this because of my results and my way of applying Green’s theorem, which can vary from code to code, shape to shape. So yeah, this activity really needs more samples. Also, ImageJ’s length calibration program enabled the measurement of many objects using a reference scale. In this case, I may have undermined the use of ImageJ, but its power is very vital in microscopic studies. Now that I have established in this blog that Green’s theorem is a valid area estimation method, the question now is: where can I use this? Well, I would say that Green’s theorem could be a valid substitute for pixel counting, if pixel counting is not optimized for, let’s say, estimating the area of a really, really large image. In the method presented, one can just detect all the pixels in the edges, and perform the integrations.

Since I am able to estimate the area of regular shapes such as the circle, square and triangle with low deviation, my estimation of Farmers Market and Nevada’s land area from Google Maps are almost the same with reference value, and I have shown the power of ImageJ using my beautiful driver’s license, I would give myself a 10/10. Well, I was able to differentiate the different method and elaborate on their accuracy, then I could give myself an 12/10. Haha!

Reference:

[1] Soriano, M. “A4 – Length and Area Estimation in Images,” 2014.

“Potato is love. Potato is life.”

One thought on “Activity #4 – Length and Area Estimation in Images

Leave a comment