How to detect edges in a image, and create a mask (matlab)

Dedrawde picture Dedrawde · Dec 26, 2013 · Viewed 8.4k times · Source

I am trying to do a task, but there are 2 problems. The first one, I must take an image, and then detect the edges on the surface.

This is the original image:

the original image

and this would be the result:

the result

However, I got this:

actual result

I am using a very simple code:

filter=[1 2 1;0 0 0;-1 -2 -1];
image=imread('boat.jpg');
image_edge=filter2(filter,image);
imshow(image_edge);

As you can see, it is very easy but I don't have the same image. Is my filter wrong?

The second problem is the next: I don't know how to create a matrix of miximg coefficients, mask(x,y). This matrix has to be created based on the edges (value of 1 in flat areas and gradually decrease to 0 to edges). What command should I use?

Answer

NKN picture NKN · Dec 26, 2013

is your filter extracting both vertical and horizontal edges? Because the filter2 uses the cov2 function and to extract both vertical and horizontal edges you need to use cov2 twice in your code. check this page for conv2 example on vertical and horizontal edges.

You can also try edge command in MATLAB though it works over intensity images:

I = imread('boat.jpg');
BW1 = edge(I,'prewitt');  % or any other filters supported
imshow(BW1);

check this page for different kinds of edge filters.