Wednesday 19 June 2013

Activity 5 - Area Estimation for Images with Defined Edges

For this activity we were to use Scilab in order to measure area via implementation of Green's Theorem. Recall that Green's Theorem allows an area integral to be related to a line integral. Given a region R bounded by a closed curve, composed of  Nb points arranged counterclockwise, we can estimate the area of the bounded surface based on the curve coordinates, stated as: 


we will be using binary images for the most part of the activity and later on move on to colored images. 

Recalling that from Activity 2, we were tasked with creating with creating binary images of a square and a circular aperture. In this activity, I will be reusing them to test the accuracy of the available edge detection methods available in Scilab. 

Shown in Figure 1 are two 300x300 binary image containing a 150x150 square and a circular aperture with radius = 45pixels. In applying the edge() function, there are three possible methods available; sobel, prewitt, and canny. As we can see from Figure 2, sobel and prewitt were able to detect the corner and the contour of the square, respectively, while the canny method was not able to detect anything. As such I will be using prewitt and sobel for the remainder of the activity.

Figure 1. 300x300 image with (Left) 150x150 square aperture; (Right) Circular aperture with radius = 45 pixels

Figure 2. Application of Edge detection methods: (Top and Bottom Left) Prewitt; (Top and Bottom Middle) Sobel: (Top and Bottom Right) Canny

Using find(), the edge coordinates along the x-axis and the y-axis were determined from the processed image. With the coordinates known, the center was shifted to the origin, as is the requirement needed for the equation to work.

[y,x] = find(E); //determine edge coordinates

//shift center to origin
x_unsort = x - mean(x);
y_unsort = y - mean(y);

What's left to do is to assured that the coordinates are arranged according to their position along the boundary of the object. The coordinates were thus represented in cylindrical space and sorting was implemented based on angle. For the ange conversion the atand() function was used, which mapped the coordinates along [-180,180]. For the sorting, gsort() function was used which returns a sorted array along with the original indices, which was used to sort the corresponding [x,y] coordinates.

//sort coordinates in counter-clockwise order
theta_unsort = atand(y_unsort,x_unsort);
[theta_sort,order] = gsort(theta_unsort);

L = length(x);

//sort x and y coordinates according to theta
x_sort = []; y_sort = [];
for i = 1: L
    x_sort = [x_sort, x_unsort(order(i))];
    y_sort = [y_sort, y_unsort(order(i))];
end

Area calculation was then applied based on Green's theorem.

//area calculations
Area = 0;
for i = 1L
    if i == l then
        Area = Area + (x_sort(i)*y_sort(1) - y_sort(i)*x_sort(1));
    else
        Area = Area + (x_sort(i)*y_sort(i+1) - y_sort(i)*x_sort(i+1));
    end
end

Area = abs(0.5*Area)

The abs() function was used in the equation because the codes gives a negative value of Area. But this is not the only way to estimate the area of the apertures, another method known as Pixel counting can be used. In this case, I will be using pixel counting to count the number of 'white' pixels, the sum of which should be equal to the actual area of the object of interest. Pixel count method can also be used as a ground truth value in case the theoretical value does not match with the real value.

//set a groundtruth value
[void, pixelw] = size(find(A > 0));
gtruth = pixelw;

Table 1 shows the result after implementing the pixel count method and Green's Theorem based on the three mentioned edge detection technique.

Table 1. Area estimation results and percent error

For both the square and circle area estimation, it can be seen that the canny operator performed worst with a percent error of 100% which ensues due to the fact that it was not able to detect any part of the aperture boundary. This comes as a surprise since the canny edge method is supposed to be the standard edge detection method. Prewitt on the other hand exhibits the best result with the closest possible value to the pixel count method, with an error of 3 pixels for both aperture wrt pixel counting. Relating it to the theoretical value however shows that, like the pixel count value, it is farther away from the theoretical value compared to the Sobel operator. The error  that occurs is due to the synthesis of the circular aperture, it is not a perfect circle, what the code does is it approximates the boundary of the circle as close as possible as there are no fractional pixels. As shown in Figure 3, the boundary of the circle is not smooth and is actually pixelized, from this it is safe to assume that the Pixel counting and Prewitt-based application of Green's theorem has a more correct result compared to the Sobel-based approach.

Figure 3. Close up view of circular aperture boundary

Increasing the array size to 1000x1000, different area estimations were made for both square and circular apertures. In Figure 4 and 5, it can be seen that the for very small area estimation, both pixel counting and the Prewitt-based approach shows better results compared to the Sobel-based approach. But as the area of the apertures became larger, the error comparison between the three became insignificant, as shown in Tables 2 and 3. It is to note that for the circular aperture the Sobel-based approach was able to estimate the theoretical area of the circle more accurately than the two mentioned appraoches. However, the Prewitt-based approach has to closer approximation to the pixel counting method, which is used for the ground truth value. Also, reduction of the expected area was prominent throughout the synthesis of the circle and even in the smaller squares, which would have attributed to the error. Therefore, error should have been based from pixel counting rather than algebraic calculations.

Figure 4. Error maping for different Circular aperture area

Figure 5. Error mapping for different Square aperture areas

Table 2. Error calculation for circular aperture

Table 3. Error calculation for square aperture

Moving on to colored images, I took an image of the Pyramid of Giza from google maps by utilizing the Snipping tool. The area cover of the pyramid was first isolated using paint, shown in Figure 6 and the pixel to real world measurement was taken. For the image, every 119pixels corresponds to either 200ft or 50m. The image was then read using imread() and converted to grayscale using rgb2gray() function. Once this was done, all three edge detection techniques were used and area calculation via Green's theorem used. As shown in table 4, the Canny-approach was able to detect the edge of the pyramid outline, and the three edge detection method results varied minimally compared to each other. But the base length measurement of the pyramid varied based on the source, the one used in this activity was 693 ft, which can range from 693 - 765 ft. Using manual measurement of the pyramids base, it was estimated to have a base with a dimension of 435x436 pixels. Converting this to real world measurement, it can be seen that the Canny operator performed best followed by Prewitt and Sobel. Based from this, it can be said that the Canny operator is not applicable on binary images or arrays.

Figure 6. Pyramid base area islation; (Left) original image; (Middle) Distinguishing the edge of the pyramid; (Right) Isolation of the estimated Pyramid area cover

Table 4. Pyramid area and Error calculation based from source [1]

Table 5. Pyramid area and Error calculation based from manual estimation

In this activity, I believe I have performed beyond the requirements. Aside from meeting the set requirements I have also found error caused by the synthesis of the circle, made a comparison of the edge detection methods based on different area measurements and even discovered that the Canny operator does not work on purely binary values. I therefore would like to give myself a grade of 12.

Acknowledgements:
I would like to thank Alix for suggesting rgb2gray when gray_imread and im2gray would not work or function.

References:
[1]http://www.earthmatrix.com/great/pyramid.htm
[2]maps.google.com





No comments:

Post a Comment