Home > Mobile >  Is there a way to add two filter together as one filter in edge detection?
Is there a way to add two filter together as one filter in edge detection?

Time:08-31

When I do an edge detection, I normally first use a Gaussian filter:

 img_blur = scipy.ndimage.gaussian_filter(img,2,truncate = 2.25)

Then a gradient filter:

Ix= scipy.ndimage.convolve(im, dx)
Iy = scipy.ndimage.convolve(im, dy)

Where dx and dy are the two filters in the horizontal and vertical directions. Then I will compute for the edge:

edges = Ix**2   Iy**2

My question is there a way to combine the gaussian filter and the gradient filter into one filter? Let's call this filter f, can I do something like:

f= scipy.scipy.ndimage.gaussian_filter(img,2,truncate = 2.25)   scipy.ndimage.convolve(im, dx) 

? I know this is not a legal expression, but is there a way to correctly implement this filter f or is there any other filter that has the same effect? I am doing horizontal and vertical filters separately, so f just needs to be one direction at a time. This filter need to go through another convolution to get the final result, which means it needs to go through another

result = scipy.ndimage.convolution(img,filter)

to get the final result.

CodePudding user response:

You can't. convolve filter uses result of previous one. To calculate value of convolve, you need blurred values of neighbors. When you apply 2 filter to 1 image, sometimes you can combine them. You apply 2 filter on 2 different images.

Theoretically you can find some implementations of second step in 1 step. In practical I don't saw something like that before. You can write yourself. For effective implementation you need to use more hardware oriented language (ex. C ) and use SIMD instructions for faster implementation.

Sembei in comments ask right questions. Why you want run in 1 filter? Maybe we can suggest other ways to solve your problem.

CodePudding user response:

Two successively applied filters can be combined into a single filter just by convolving their kernels together.

You could, therefore, combine the Gaussian with each of the gradient filters to make two combined Gaussian-gradient filters, but...

You do not want to do this, because a gradient filter is very fast simple, while a Gaussian filter is a more complex operation. A combined Gaussian-gradient filter is also a complex operation. Applying the two (slow) combined filters would be slower that applying a single Gaussian filter followed by two gradients.

In fact, all of the filters we are talking about are separable. The gradient filters operate only in one direction (horizontal or vertical), and the other filters (Gaussian and combination) are most efficiently implemented as two filter applications, one horizontal and one vertical.

So in combining the filters, if you were to take the care to implement your combined filters efficiently, then you would really trading 4 directional filter applications (Gaussian X, Gaussian Y, Gradient X, Gradient Y) for 4 slower directional filter applications (CombinedX X, CombinedX Y, CombinedY X, CombinedY Y). These are slower, because you no longer have any of those quick simple gradient passes.

  • Related