



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for problem set 2 of cmsc 426, which involves writing a simple edge detector based on the canny edge detector. The assignment includes image smoothing using a gaussian filter, computing the gradient magnitude and direction, and finding local maxima in the gradient to identify edges. The document also includes test functions and examples.
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Written Exercises (10 points each)
a. What is the gradient at the pixel in bold face (with a value of 6)? b. What is the magnitude of the gradient? c. What is the direction of the gradient?
Programming Exercises
The goal of this assignment is to write a simple edge detector based on the Canny edge detector. Your program will identify pixels in which the magnitude of the gradient is a local maxima when compared to two neighboring points in the direction of the gradient. To compute this you must smooth the image prior to finding the gradient. Then you must compute the gradient magnitude and direction. Using the gradient direction, for each pixel, (x,y), you will compute two points, one in the direction in which the image intensity is increasing most rapidly, the other in the opposite direction, where the intensity is decreasing most rapidly. You will compare the gradient magnitude at (x,y) to the magnitude at these two points. (x,y) is an edge only if its gradient is bigger than at these two points.
Keep in mind that whenever you do image filtering, you should use the option ‘replicate’ for handling boundary pixels. Note that to use the test functions we describe below, you will have to name your functions the same as ours, or modify our test functions. For example, to use the test function test_smooth_image you will have to name your function smooth_image in the first problem.
Hint: It will be a good idea to turn your image into a matrix of floating point numbers using the double function. If not, you will get into trouble, because images are constrained to be non-negative integers below 255. If, for example, the gradient is sometimes negative, it cannot be stored in an image.
You can test your function using the test function that we provide, test_smooth_image. When we execute this test function, we get the following result:
test_smooth_image ans = 0.0053 0.0682 0.3497 0.8090 1.0279 0.8090 0.3497 0.
0.0682 0.8744 4.4855 10.3763 13.1832 10.3763 4.4855 0.
0.3497 4.4855 23.0104 53.2302 67.6296 53.2302 23.0104 4.
0.8090 10.3763 53.2302 123.1381 156.4484 123.1381 53.2302 10.
1.0279 13.1832 67.6296 156.4484 198.7695 156.4484 67.6296 13.
0.8090 10.3763 53.2302 123.1381 156.4484 123.1381 53.2302 10.
0.3497 4.4855 23.0104 53.2302 67.6296 53.2302 23.0104 4.
0.0682 0.8744 4.4855 10.3763 13.1832 10.3763 4.4855 0.
0.0053 0.0682 0.3497 0.8090 1.0279 0.8090 0.3497 0.
Hand in your code and the result of this test.
You can test your function using the test function that we provide, test_gradient_magnitude_direction. When we execute this test function we get the following result:
[R, X, Y] = test_gradient_magnitude_direction R = 1.4142 2.2361 3.1623 4. 2.2361 2.8284 3.6056 4. 3.1623 3.6056 4.2426 5. 4.1231 4.4721 5.0000 5. X = 0.7071 0.8944 0.9487 0. 0.4472 0.7071 0.8321 0. 0.3162 0.5547 0.7071 0. 0.2425 0.4472 0.6000 0. Y = 0.7071 0.4472 0.3162 0. 0.8944 0.7071 0.5547 0. 0.9487 0.8321 0.7071 0. 0.9701 0.8944 0.8000 0.
Notice that with these values, we can find in Matlab:
X.^2+Y.^ ans = 1.0000 1.0000 1.0000 1. 1.0000 1.0000 1.0000 1. 1.0000 1.0000 1.0000 1. 1.0000 1.0000 1.0000 1.
This is because X and Y contain the cosine and sine of the direction of the gradient, and cosine squared plus sine squared equals one. Hand in your code and the result of this test.
A pixel will be an edge if it satisfies two criteria. First, it must be a local maxima in gradient magnitude. That means that its gradient magnitude must be bigger than that at two neighboring locations. These must be locations that are a distance of one pixel away, and that are in the directions at which the gradient is changing most rapidly. For example, suppose pixel (10,10) has a gradient direction that is 45 degrees from the x axis. We would describe this gradient direction as (.7071, .7071). Then we can only have an edge at (10,10) if the magnitude of the gradient there is bigger than the magnitude of the gradient at (10.7071,10.7071), and also greater than the gradient magnitude at (9.2929, 9.2929). Note that these locations do not have integer coordinates, so we must interpolate the gradient magnitude to estimate its value at these locations. Since we haven’t discussed how to do this, we will provide you with a Matlab function that performs this interpolation. This function is called: interpolate_gradients. Look at the comments in this function to see how to use it.
Test your function using the image of the swan in swanbw.jpg. We have provided a second image, swanedges.jpg, which shows the output of our code when we run on this image, using a value of 2 for sigma, and a value of 15 for t. Turn in your code, and an image that shows the edges that your code produces when you run with these same values. You may find it useful to use the Matlab function imwrite.