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 1024x1024.
Figure 1: Basic binary shapes generated using the Python PIL library.
For extracting the edges of the shapes, I explored a few edge detection algorithms, eventually settling with the following:
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 formFrom the traced edges, the area it encloses can be calculated using a discretized form of Green’s theorem [6]
where x_i
, y_i
are the pixel coordinates,
and N
is the number of pixels on the
boundary. The results for each algorithm, along with their relative errors,
are shown in Table 1, and the edge-traced images
are shown in Figs. 2-5.
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
A = \pi r^2
and solved for r
. I can now plug in this r
directly into (6)
and finally, multiply it again by \pi r
to get the area in real units. The
calculated areas for the different algorithms are shown in Table
2, and the edge-traced images are shown in Fig. 6.
actual | spot | sobel | prewitt | laplacian | canny |
---|---|---|---|---|---|
4485.92 | 4600.44 | 4630.52 | 4630.55 | 4629.64 | 4634.90 |
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.
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).