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:
and this would be the result:
However, I got this:
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?
NKN definitely got the point, the only thing I want to point out is that you're using the sobel kernel for the y direction, if you want to effectively perform the edge detection on the x axis the kernel is
Sobel_x = [1 0 -1; 2 0 -2; 1 0 -1];
Also note that there are many other better detector, such as the Canny one. I strongly suggest you to have a look at it.
Regarding your second question, I'm not sure what you're asking, but it looks like you want a normalized gradient magnitude matrix. To get it you must first (surprise!) calculate the gradient matrix:
G=sqrt(G_x.^2 + G_y.^2);
Where G_x and G_y has been obtained convolving the image with Sobel_x and Sobel_y in your case. This matrix will contain the gradient magnitude for each pixel of the image on which you're detecting edges.
Then to get things in the interval [0,1] you just normalise the matrix:
G = G/max(G(:));
In this case you'll get the opposite of what you want (closer to 1 mean that the pixel is likely to be part of an edge), so you may just simply do G = 1-G.
EDIT:
Also note that to get a decent looking binary result you want to threshold the gradient magnitude matrix.
For example I tried a threshold of 0.15 on the G before doing the 1-G.
G = G>0.15;
G = 1-G
imshow(G)
and the result with sobel is:
I'm sure you can do better, this is just an almost random threshold value to show you the result.
Higher the threshold, finer the features you're classifying as edges.