Activity 4: Measuring Area from Images
For this activity, I generated the basic shapes using the Python Imaging Library (PIL). This let me define the
centroid and pseudo-radius (distance from the centroid to the midpoint of an edge) for each shape analytically,
and therefore, I know precisely what their dimensions and areas will be. The shapes are shown in Fig. 1, all
with size 1024
For extracting the edges of the shapes, I explored a few edge detection algorithms, eventually settling with the following:
- PIL’s
FIND_EDGES
filter: this built-in function works like a charm and can be easily implemented as a one-liner. The documentation doesn’t describe what it does exactly, but after digging through the source code [1] I found that it simply convolves an image with a spot kernel of the form
- Sobel filter: approximates the gradient of an image’s intensity function [2]. Convolves images with two kernels (one for each direction)
- Prewitt filter: also approximates the gradient of image intensity and acts in two directions, similar to the Sobel filter [3]. Its kernel is of the form
- Laplacian filter: calculates the second-order derivatives of the image intensity in one pass [4]. Because of its sensitivity to noise, I first applied a 3x3 Gaussian filter with 0 standard deviation before applying the Laplacian filter. Its kernel is of the form
- Canny filter: a multistage edge-detection algorithm which is highly versatile and efficient [5].
From the traced edges, the area it encloses can be calculated using a discretized form of Green’s theorem [6]
where
Figure 2: Edges extracted from the circle using different algorithms.
Figure 3: Edges extracted from the square using different algorithms.
Figure 4: Edges extracted from the trapezoid using different algorithms.
Figure 5: Edges extracted from the triangle using different algorithms.
Notice how the Sobel & Prewitt filters perform very similarly because they are both gradient-based methods. Due to their directional dependence, we can notice how the edges, particularly on the lower-right portions, are not being detected very well. Upon closer examination, however, the edges are being detected but are very faint (low magnitude). Spot and Canny, on the other hand, produce very clean and accurate edges. Laplacian also detects a clean edge but is somewhat low in magnitude.
Shape | Actual | spot | Sobel | Prewitt | Laplacian | Canny |
---|---|---|---|---|---|---|
circle | 502,655 | 501,584 | 503,187 | 503,187 | 503,198 | 503,182 |
0.21% | 0.11% | 0.11% | 0.11% | 0.10% | ||
square | 640,000 | 639,800 | 641,404 | 641,404 | 641,416 | 641,398 |
0.03% | 0.22% | 0.22% | 0.22% | 0.22% | ||
trapezoid | 426,667 | 426,399 | 428,002 | 428,002 | 428,013 | 427,731 |
0.06% | 0.31% | 0.31% | 0.32% | 0.25% | ||
triangle | 320,000 | 319,102 | 320,705 | 320,705 | 320,714 | 320,304 |
0.28% | 0.22% | 0.22% | 0.22% | 0.09% |
I chose the CS Amphitheater as a location of interest for calculating the area on a map. I obtained its actual area using Google Earth’s Ruler tool, then proceeded to import it into Photoshop. I then applied enough threshold so that the yellow line was clear and isolated from the other white spots. I manually erased any remaining white artifacts until only the circle and the line indicating the radius were left. I then measured the length of the radius in pixels, this time using Photoshop’s Ruler tool. Since the actual radius in meters was also provided by Google Earth’s Ruler tool, I was able to formulate the pixel-to-meter conversion as
Via Green’s theorem, I was able to obtain the Amphitheater’s area in pixels.
However, this cannot be converted directly to real units using (6) because our current units are area, while the
conversion equation only works for units of length. Since the amphitheater
appears fairly circular, I used the formula for the area of a circle
actual | spot | sobel | prewitt | laplacian | canny |
---|---|---|---|---|---|
4,485.92 | 4,600.44 | 4,630.52 | 4,630.55 | 4,629.64 | 4,634.9 |
2.55% | 3.22% | 3.22% | 3.20% | 3.32% |
We can observe that in general, the spot filter works best in edge detection. Upon further investigation, I found out that the spot kernel is actually a variant of the Laplacian kernel and produces exceptional results on arbitrary images.
References
wiredfool, A. Clark, Hugo, A. Murray, A. Karpinsky, C. Gohlke, B. Crowell, D. Schmidt, A. Houghton, and S. Johnson, Pillow: The friendly PIL fork (2016).
I. Sobel, An isotropic 3x3 image gradient operator, Presentation at Stanford A.I. Project 1968 (2014).
J. M. S. Prewitt, Object enhancement and extraction, Picture processing and psychopictorics (1970).
N. Tsankashvili, Comparing edge detection methods (2018).
J. F. Canny, A computational approach to edge detection, IEEE transactions on pattern analysis and machine intelligence 8, 679 (1986).
- M. N. Soriano, Measuring area from images (2019).