Description
- First thing we’re going to do is learn how to create a sample image that will be useful for this lab. We will do this by using the Matlab command ‘checkerboard’.
- To give you an idea on how this command works, from the matlab command prompt, type the following:
- imshow(checkerboard(20));
- It should create the image shown below: (see fig1.png)
- The 20 in the command is the number of pixels that each box on the checkerboard will represent.
- Let’s create an image from this matrix that we can use later. In order to do that, we need to use the ‘imwrite’ command.
- imwrite(checkerboard(20), ‘checkerboard.jpg’, ‘JPEG’);
- To verify the above worked correctly, let’s read in the newly made image and display it:
- myImage = imread(‘checkerboard.jpg’);
- imshow(myImage);
- Now that we have a nice image to use for our edge detection algorithm, let’s begin to implement it. First thing we want to do is create our x and y gradient filters:
- Sx = [1 0 -1; 2 0 -2; 1 0 -1];
- Sy = [1 2 1; 0 0 0; -1 -2 -1];
- Convolve each of the above filters with the checkerboard image. Before we are able to use the convolve function we must convert our image into a double. We do that by using the ‘double’ command and passing it image variable name, in this case ‘myImage’.
sobelXImage = conv2(double(myImage), Sx, 'same');
sobelYImage = conv2(double(myImage), Sy, 'same');
- Let’s go ahead and take a peek at what our images currently look like:
figure('Name', 'X gradient');
imshow(uint8(sobelXImage));
figure('Name', 'Y gradient');
imshow(uint8(sobelYImage));
- Do the gradient images look like you expect them too? Why or why not?
- What is missing from the above question and how would you fix it?
- Now that we understand what the gradients are doing and are able to display them properly, let’s continue on with our edge detection. We now need to square each of the X and Y portions, and then take the square root of them to determine the magnitude of our gradients.
E(u,v) = sqrt( (Dx(u,v))2 + (Dy(u,v))2 )
- Enter the appropriate matlab command for the above formula and display the image. You should see something like fig.png
- Each of the edges are shown in white.
Part B
- Download a sample image from the net. Repeat the above steps and display the original image, the X gradient, and the Y gradient on one form/figure (Note: Don’t forget to convert it to black and white first) .
- After we sum the squares and take the square root we have the magnitude of our edge strength. We may not want to look at every single edge detected. We can limit which edges we want to see by applying a threshold. We can do this by using the following matlab commands:
threshold = 50;
edgeDetectedImage = (uint8(edgeDetectedImage) > threshold) * 255;
- Create 4 additional figures each with their own respective image on it (using threshold {50, 100, 150, 200} ). Make sure the titles of the figures reflect their contents.
- Turn in the final matlab script that you used to accomplish part B. It should display 5 figures
