Ipinapakita ang mga post na may etiketa na edge detection. Ipakita ang lahat ng mga post
Ipinapakita ang mga post na may etiketa na edge detection. Ipakita ang lahat ng mga post

Huwebes, Disyembre 7, 2017

Fourier for Today: Fourier Transform Model of Image Formation

Fourier transform is a wonderful linear transform which converts a signal into its inverse dimension. For a signal with dimensions of space, FT converts it to a signal of spatial frequency. For a 2D image, the FT is given in the equation below.  A powerful implementation of FT is the fast fourier transform algorithm which is fft2() in matlab and something similar in other packages. This function however has different properties and we should be careful in using them.


For our 186 subject, it is a required skill to be familiar with and to know by heart the fourier transform of different patterns. There are a few get away tips to easily familiarize oneself on FT. first is that the FT of a pattern is in inverse dimension of those of the pattern. From here, we can say that when the pattern is long then the FT is short or when the pattern is short the FT will be long. The FT of a small aperture will be wide. Second is that when the pattern is rotated, the FT will be in the same rotation. Last is that the product of two patterns will have a FT of the sum of the individual FT of the patterns. It is also important to note that the FT of an FT of a pattern is the pattern itself, so a pattern and its FT is somehow like a pair. So now, we look at FT of some basic patterns. 

Figure 1 shows the FT of a circle. 1(a) is the circle. This pattern could be thought of as a circular aperture. Figure 1(b) is the output of the FFT algorithm, which is not yet center shifted. This should always be noted in using FFT algorithms from packages. The function fftshift() can fix it. Figure 1(c) is the FFT of the circular aperture, which is an Airy pattern. Note that the pattern is medium sized, producing a medium size FT. Figure 1 (d) is the FT of the Airy pattern in figure 1 (c) showing that the FT of the FT of the circle is a circle. It should be noted also that the FT of a function is complex. Figure 2 shows the real and imaginary parts of the FT. 

Figure 1. The FT of a circular aperture. (a) the circular aperture. (b) The pattern produced by the FFT algorithm. (c) The FT of the pattern. (d) the inverse fourier transform of the FT. 

Figure 2. The complex FT of letter A. (a) the pattern A. (b) The FT of the pattern. (c) The FT of the FT of the pattern. (d) The real part of the FT of the pattern. (e) The complex part of the FT of the pattern. 

Figure 3. a) A sinusoid along x (corrugated roof) b) The FFT of the sinusoid which is a two dirac deltas and a direct delta at the center 0. 


Figure 3 shows the FFT of a sinusoid along x, which is a two dirac delta with peaks corresponding to the frequency of the sinusoidal signal. For the fourier series, any signal can be expressed as a linear superposition of weighted sines and cosines of different frequencies. The fourier transform shows the distribution and the relative strengths of sinusoids in the signal. In this case, the frequency as which the sinusoid was made is at the peak of the dirac delta, which is the sinusoid with the highest strength. The spread of the dirac delta shows the other available signals. A constant signal is like a sinusoid with a very low frequency, which has an FFT of dirac deltas very near the center 0. So for a constant signal, the FFT is at the center. A double slit however, as seen in figure 4, can be thought of as a many dirac delta signals along the y axis centered at x = 0. It’s FFT will be a sinusoid along x-axis. Another signal that can be thought of as a superposition of many sinusoids is a square pattern. The FFT is a sinusoidal signal forming a cross. Since the square is wide along x and along y, the FFT is thin and long along x and y. 

Figure 4. The double slit and its FFT which is a sinusoid along the x-axis but confined only along y = 0.

Figure 5. Square and its FFT, which are sinusoids along x and y forming a cross

A Gaussian bell curve will always have a Gaussian as its FFT. A small pattern will have a big FFT. This is shown in figure 6. 

Figure 6. The FFT pair which is a small and big 2D Gaussian bell curve

Simulation of an imaging device

Convolution is used in imaging such that the resulting convolution of 2 patterns looks like both the 2 patterns. The result is like the smearing of one pattern to the other. For an object, the resulting image taken by an imaging device is the convolution of the object and the transfer function of the imaging device.

It is also essential to note that the FFT of a convolution is the product of the individual FFT of two patterns. With this in mind, to determine the FFT of a difficult pattern, the pattern could be broken down to easy patterns with known FFT and obtain the product of the FFTs.

In the next part, we will simulate an imaging device. For a digital camera for example, the finite lens radius limits the gathered rays reflected from the object, making the reconstruction imperfect.

We have an object which is the letters VIP in Arial font. The imaging device is shown by a white centered circle. In figure 7, radius of the imaging device was increased from left to right. The increase in the radius of the imaging device reblurred the reconstructed image. The greater the radius, the better the quality of the produced image. This can be observed also in a video bellow where the radius of the aperture increases. 

Figure 7. The convolution of the letters VIP and a circle with varying radius. a) r = 0.1 b) r = 0.3 c) r = 0.5 d) r = 0.7 e) r = 0.9


Template matching using correlation

Correlation is another tool used to look into the degree of similarity of two patterns. The high the correlation at a certain pixel implies that the patterns are identical at that point. This can used for template matching or pattern recognition. For the activity, we used a template with the text: “THE RAIN IN SPAIN STAYS MAINLY IN THE PLAIN” in Arial 12. The letter “A” typed also in Arial 12 was correlated with the pattern. The center of the bright spots show the center of the location of the letter “A”.

Figure 8. Template matching using the correlation of a letter to a pattern. a) The pattern used. b) the letter A was detected in the pattern. c) The brightest spots in the correlation shows the centers of the location of the occurrence of A in the pattern in (a). 

Another template matching application if the edge detection in an image using an edge pattern. The edge patterns used are 3x3 matrices such that the total sum of the elements is zero. Figure 9 shows the edge patterns used and its convolution with the VIP image. As can be seen the white pixels in the convolution represents the areas at which the image and the edge pattern matches. For the horizontal pattern (a) the horizontal lines were not seen while the vertical lines were not seen when using the vertical pattern (b). The slant and curve edges for (a) and (b) looks pixelized and was badly connected. A clean detection was done by the spot (c). For the diagonal patterns in (d) and (e), the verticals and the horizontal lines were not detected.

Figure 9. The edge patterns used (upper panel) and its convolution with the image VIP (lower panel) the edge patterns are: a) horizontal b) vertical c) spot d) diagonal with a negative slope e) diagonal with a positive slope. 


For this activity, I would give myself 10/10 for the activity. In general, this activity is not that fun, however, among all the activities, I enjoyed looking at the edge detection results in images and the template matching using correlation. I did it for the exploration part. However, I did not get to finish the template matching.

At first I thought that the correlation would be a nice technique to be used for handwriting character recognition such that a program can recognize each letter from an image of text. However, changing the font and the size of the template letter would not make it detectable for the program anymore. Good thing however, I did center shifting using the centroid of the letters before doing the correlations so that shifting the location of the template letter from the center would still make it detectable since. In figure 10, I correlated all the letters in the text with the text image. My plan is to correlate each letter to the image and the peak locations would mean that the letter occurred in that location. The program will then reconstruct the text in the image using the letters and the peak locations basing on the index. The spacing between the words would be neglected. The problem however is for letters with high correlations. A nice example would be the letter I, where it detected 14 peak locations instead of 6.


Figure 10. The correlations (shown in the lower panels) of the image in figure 8 a) with the letters (shown in the upper panels). 

For the edge detection, I planned on using only the spot edge pattern since the results in the VIP image is clean, however, when using it alone, the resulting edges are not continuous and smooth. It also detected some undesired edge which created noise in the image. So for this, I added each of the resulting convolutions of the image with the edge patterns (horizontal, vertical, spot, diagonal with a negative slope, and diagonal with a positive slope) and applied a threshold to clean the image. I compared the technique to the edge detection package in Matlab using the Prewitt algorithm. The results are shown in figures 11 and 12. The edge detection method is shown in column b while the Prewitt algorithm is shown in column c.

Figure 11. The application of edge detection method to the images in column a) using convolution with the edge patterns (column b) compared with the Prewitt algorithm (column c)
Figure 12. Another application of the edge detection method to the images in column a) using convolution with the edge patterns (column b) compared with the Prewitt algorithm (column c)

For the images in figure 11, the results of the convolution is better compared with the Prewitt algorithm since most of the essential details were overlooked by the Prewitt algorithm such as the jaws of the kid, the outline of its face, etc. The result of the convolution however is messier as can be observed in the uppermost panel. 

For the images in figure 12, the Prewitt algorithm produced cleaner and better results. Some edges, especially in the middle panel, were omitted by the convolution method while it was perfectly outlined by the Prewitt. 

To improve this method, one can think of a way to give weights to the effect of convolving different patterns in  the image. The identification of the threshold for each image could also be automatically known using the histogram of the intensity.

I would like to thank Elijah Justin Medina for all the creative ideas which either he shared or was inspired by him. I cannot accurately remember some, but there sure are a lot of things he contributed into the making of this activity.

Beyond the edge of the world there’s a space where emptiness and substance neatly overlap, where past and future form a continuous, endless loop. And, hovering about, there are signs no one has ever read, chords no one has ever heard.Haruki Murakami, Kafka on the Shore

Linggo, Setyembre 3, 2017

Edge of a Circle: Area Estimation in Images using Green's Theorem

Technological advancements of today has wide applications on a lot of aspects of our life. One of them is through area estimation using only image processing. This would be helpful is estimating land areas where it only requires an image and a conversion factor of the pixel size to actual length scales. One example would be area estimation of regions in the Mars taken by Curiosity. In this activity, we were tasked to use Green’s Theorem as a method of area estimation. I also used morphological operation by getting the sum of the pixels inside a known shape and compared it with the result of Green’s Theorem and the known area of the shape based on its radius or side. 

Green’s Theorem obtains the area A of the regions R using the contour of the edges of the area obtained in a clockwise direction.
 In discrete form,

For obtaining the contour of each shape, I used the edge function in Matlab with Canny as the method. I compared the result of the Green’s Theorem to the sum of the white pixels in the image and to the result of using the formula for the area of each shape. I obtained the center of the image from the mean of the x axis and mean of the y axis of the edged of the shape. Then, I used the function sort to sort the theta in increasing order. This function returns the old indices arranged now in the way it was sorted. The indices was used to rearrange the x and y coordinates.

 For the activity, I used a circle, square, triangle, and a parallelogram as test shapes. Varying the pixels sizes of the image, I used 10x10, 50x50, 100x100, and 500x500. 

In figure 1, increasing the pixel size increases the accuracy of the Green’s Theorem method. This is affected by the edge detection done since looking at the circle in the 50x50 px image, the circle is not completely round looking at the sides. At the extreme case, in figure 1 a) for the 10x10 px image, the image is now a square, which opens a lot of inaccuracy at the corners considering that the radius of the circle remained constant. The table below summarizes the percent difference of the area obtained using the Green’s Theorem, compared with the area obtained using an empirical formula (formula) and the area obtained from the sum of the white pixels (sum). Same thing was done for the square in figure 2, triangle in figure 3, and parallelogram in figure 4. 
Resolution of Circle (px)
Formula (%)
Sum (%)
10x10
9.9237
31.25
50x50
1.4975
1.2821
100x100
0.5524
0.8558
500x500
0.1269
0.1277

Figure 1. The circles used with varying pixel size. a) 10x10 px image. b) 50x50 px image. c) 100x100 px image. d) 500x500 px image. The radius of the circle used was 0.25 the length of the whole square image. 

The summary of the area estimation done using a sqaure is shown in the table below.
Resolution of Square (px)
Formula (%)
Sum (%)
10x10
31.25
31.25
50x50
1.7361
1.7361
100x100
0.66
0.66
500x500
0.1064
0.1064

Figure 2. The squares used with varying pixel size. a) 10x10 px image. b) 50x50 px image. c) 100x100 px image. d) 500x500 px image. The size of the square used was 0.5 of the length of the whole image.

The summary of the area estimation done using a triangle is shown in the table below.
Resolution of Triangle (px)
Formula (%)
Sum (%)
10x10
12.5
12.5
50x50
14.876
3.4722
100x100
7.2917
1.12
500x500
1.4723
0.1448

Figure 3. The triangles used with varying pixel size. a) 10x10 px image. b) 50x50 px image. c) 100x100 px image. d) 500x500 px image. The base and height of the triangle was 0.5 of the length of the image. 

The summary of the area estimation done using a parallelogram is shown in the table below.
Resolution of Parallelogram (px)
Formula (%)
Sum (%)
10x10
30.314
30.314
50x50
10.2941
1.3158
100x100
4.8423
0.5342
500x500
0.9264
0.0879


Figure 4. The parallelogram used with varying pixel size. a) 10x10 px image. b) 50x50 px image. c) 100x100 px image. d) 500x500 px image. 

Overall, the accuracy of the area estimation increases as the resolution increase for all the shapes. The least accurate for high resolutions in the triangle. which could be due to the angle of inclination of the sides. The formula also has a comparable difference in accuracy with the sum. This could be due to the rendering done in the images. 

I used the Green's theorem for estimating the area of the QMC circle. From google maps, the screen shot of the QMC circle in figure 5 has a lot of colors, which would give errors in the edge detection technique used. So, before running the image to the code, I created a mask using GIMP to show only the region of the QMC. The mask is depicted by the black pixels as shown in figure 6. Further processing shown in figure 7 converts the image to binary, with the region of interest shown in white pixels. 

Figure 5. The screenshot of the QMC circle. The conversion factor for the area was 8826 pixels per 10000 square meters. The area of the QMC circle is 0.27 square km based on Wikipedia. 

Figure 6. The screenshot of the QMC with a mask to highlight only the region of interest.

Figure 7. The screenshot of the QMC in binary, with the region of interest depicted in white pixels. 

Green’s Theorem gives an area of 0.2932 square km which deviates by 8.5926% from the result from Wikipedia. Comparing with the sum of the white pixels, the deviation of the method is 0.0587%. From these number, I say that the method is pretty good as long as the resolution is high enough to yield good results.

Overall, this activity is okay for me. I didn’t enjoy repeating the same method with different shapes. However, the part where I have to manipulate the screen shot of Google maps bothers me most since I feel like it required a lot of human intervention while I thought this method would be for automation of area estimation. I don’t like the part where I have to trace the perimeter of the desired object and color and recolor them into a binary image since I feel like there is an image processing method to do that. 

For the next part, I tried doing a different method by isolating a part of the image with a certain color instead of binarizing the image. The problem here is to find a pixels values which is at the middle of the range of values in the image. Compared to thresholding, the binarization should be done with a range of pixel values be assigned to white and the rest to be black. I tried searching on matlab examples, and I found about about k-means segmentation. This method segments the image based on its color using the function kmeans, which separates groups into clusters. In this case, the rgb image is converted into L*a*b* color space which identifies the colors into its luminosity layer L, chromaticity-layer a* or the red-green axis, and the chromaticity-layer b* or the location of the color along the blue-yellow axis (this was also our lesson in 187!!). For the k-means clustering, the object locations was obtained from the a*b* space and clusters using the Euclidean distance matrix.

I used a screenshot of the top view of Mayon Volcano. In Google maps, the considered range of the area of Mayon was depicted in a certain shade of green which could be located in figure 8. I tried to obtain the area of this region, first by isolating it using k-means clustering. The region was successfully separated from the other elements of the image. This results makes me super happy as the segmentation done by the program was so clean, I can even use one of the clusters indicating water ways if ever I want to. I feel energized to do more. The cluster which obtained the region of interest, ROI, which is the Mayon Volcano, was however not that good. It can be seen from figure 9 c) that there are small green regions outside of the ROI and cracks inside the ROI. I thought that repairing this region is okay since I acquired enough energy from the successful segmentation, to search even more. I’m also particularly grateful since while searching for this method, I learned about another method of segmentation, which is the watershed segmentation, that could probably be useful for my research. For more info, you may check on my research blog.

Figure 8. Screenshot of the top view of Mayon Volcano. The region of interest is the big circular region in a greenish hue. 

Figure 9. The image successfully segmented into different components using k-means clustering along the a*b* space. The region of interest in in the third cluster in c).

Separating and reparing the region of interest is harder than I thought since the shape is not a regular circle. I tried using the imfindcircles function but it can’t detect the irregular shape.  I also thought of using the function regionprops, but the cracks would still be detected as edges. This is when I thought of comparing the area option in regionprops to the Green’s Theorem method done. Anyway, after further searching, I decided to ignore the cracks or the waterways in the image and separate first the biggest area in Figure 9 c), which is the largest connected component in figure 11 b). The theoretical area used was obtained from the area feature in Google maps, figure 10, which shows the area of a selected region. I slightly cleaned the results obtained from the edge detection technique as shown in figure 11 c), by removing the points where the radius is less than 400 pixels, to remove the lines inside. This critical radius was obtained from the shortest among half the difference of opposite points along the edge. The summary of the results comparing the area obtained from Google Pro, Green's theorem, morphological operation and region props is shown in the table below. 

Google Pro (sq km)
Green’s Theorem
Sum of White Pixels
RegionProps Area
Area (sq km)
Difference (%)
Area (sq km)
Difference (%)
Area
Difference (%)
221.54
214.58
3.14
210.52
4.97
213.65
3.56


Figure 10. Area of a selected region in Google Maps can be obtained my right clicking, selecting measure distance, and selecting a closed area by selecting points around the curve. 

Looking at the summary of the results of different methods, the Green's Theorem method is pretty good. Though, a lot of modification was done in the image before the method, we still successfully did everything using image processing without human intervention using GIMP. For the whole activity, I give myself 11/10 since I completed the whole activity and added an additional process which is the image segmentation. Though the image segmentation still needs a lot of improvements, I think it is enough since the accuracy of the area estimation technique is high.

Figure 11. The Region of Interest. a) The cluster 3 obtained from figure 7 as shown in figure 8. b) The grayscale image to be used to isolate the biggest connected component which is the region of interest. c) The edges of the region of interests shows edges inside of the larger circle. 


The hand is the cutting edge of the mind -Jacob Bronowski