Borrar filtros
Borrar filtros

How to get Canny operator Matrix (just filtering , not edge detection)?

1 visualización (últimos 30 días)
Manuel
Manuel el 16 de Mzo. de 2012
I'm making a project of Matlab (2011) which gives the user the possibility to filter an gray-scale image (with convolution but without looking for edges) . I know for edges there is the [edge] function and I know that for some operators (like sobel) you can use [fspecial] to get the matrix.
I found a matrix (or more than one) corresponding to every operator I need: sobel , roberts , robinson,Laplacian etc... The I apply the filter this way: Result_image = conv2(Original_image,matrix_filter);
But I can't find one for Canny operator..
I read that canny is implemented in varius steps starting from applying Laplacian operator ..but then? What are the following steps? And how can I get the matrix to apply to the image?
I'm looking for the matrix,or a way to get it or at least one example to understand the "multi-step procedure".
Thank you very much
EDIT: I find more info in this page: http://www.cs.ucf.edu/~mikel/Research/Edge_Detection.htm There are several steps to get edges with canny operator. (derivate X , derivate Y , Gradient , Non maximum suppression , Hysteresis). But what are the ones for just filtering?
  1 comentario
David Young
David Young el 20 de Mzo. de 2012
I've just looked at the web page you reference. I have to say that in my view it's not good MATLAB code. I think you'd be much better off going to a textbook that gives a proper explanation, or even to Canny's original, excellent, paper.

Iniciar sesión para comentar.

Respuestas (1)

David Young
David Young el 16 de Mzo. de 2012
Canny uses first derivatives of the Gaussian filter. So you can convolve with fspecial('gauss',...) and then either use the gradient function (once for each axis) or convolve separately with [-1 0 1] and [-1;0;-1] to get the two components of the smoothed gradient. This is the end point of linear filtering in Canny process.
To get the squared magnitude of the gradient you can square and add the components, and to get the gradient direction you can use atan2.
EDIT Example code added.
To get the convolution mask, you use something like this:
sigma = 2; % for example
matrix = fspecial('gaussian', ceil(2.5*sigma)+1, sigma);
where sigma is chosen to give you the amount of smoothing you need for your application. The formula for the size parameter to fspecial is intended to give reasonable accuracy without making the mask unnecessarily large.
Then you do the convolution with
img_smoothed = conv2(img,matrix,'valid');
You may find that convolve2, from the file exchange, is faster than conv2, but it will give the same results.
Then you find the gradients. Use either
[gx, gy] = gradient(img_smoothed);
or
gx = conv2(img_smoothed, 0.5*[-1 0 1], 'valid');
gy = conv2(img_smoothed, 0.5*[-1;0;1], 'valid');
Depending on the application you may need to trim a couple of rows off gx and a couple of columns off gy to make the sizes match. Alternatively you can use the 'same' option instead of 'valid' (or more neatly, the 'reflect' option in convolve2), though then you will get some degree of error in the pixels at the boundaries of the image.
You can easily display gx and gy - just use
imshow(gx, []);
figure;
imshow(gy, []);
It's difficult to answer the rest of your question because it is not clear what is needed. If you look up the Canny process in a textbook, you will see it has a number of stages. I'm not sure which of these is meant to be the "Canny operator". The linear filtering part of the process takes you up to gx and gy, which is what I've given above. If you want to implement the rest of the edge detection process, then you need nonlinear processes, starting with squaring and adding gx and gy to get total gradient, hypot to get the gradient direction, and then non-maximum suppression and hysteresis thresholding. The code to do this is quite elaborate.
  3 comentarios
David Young
David Young el 20 de Mzo. de 2012
You need a few changes to your code. I'm putting them in the answer for easier formatting.
David Young
David Young el 20 de Mzo. de 2012
... and to answer your other question, no, linear filtering and squared magnitude are completely different. But rather than write a textbook here, may I refer you to one of the many excellent image processing texts available.

Iniciar sesión para comentar.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by