Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the gaussian filter algorithm work in OpenCV

I write my own gaussian filter but it is really slow.

OpenCV's Gaussian algorithm is much faster, 20 times than my gaussian filter. I want to rewrite OpenCV's Gaussian algorithm in my project, and I don't want to include opencv in my project.

However,

Can anyone give me the algorithm description, opencv's source code seems too hard to understand?

like image 878
user25749 Avatar asked Jun 01 '09 04:06

user25749


People also ask

How does Gaussian filter work?

A Gaussian Filter is a low pass filter used for reducing noise (high frequency components) and blurring regions of an image. The filter is implemented as an Odd sized Symmetric Kernel (DIP version of a Matrix) which is passed through each pixel of the Region of Interest to get the desired effect.

How does Gaussian blur work OpenCV?

In Gaussian Blur operation, the image is convolved with a Gaussian filter instead of the box filter. The Gaussian filter is a low-pass filter that removes the high-frequency components are reduced. src − A Mat object representing the source (input image) for this operation.

What is Gaussian filter algorithm?

Gaussian Filtering is widely used in the field of image processing. It is used to reduce the noise of an image. In this article we will generate a 2D Gaussian Kernel. The 2D Gaussian Kernel follows the below given Gaussian Distribution.

How does Gaussian blurring work?

In a Gaussian blurGaussian blurIn image processing, a Gaussian blur (also known as Gaussian smoothing) is the result of blurring an image by a Gaussian function (named after mathematician and scientist Carl Friedrich Gauss).https://en.wikipedia.org › wiki › Gaussian_blurGaussian blur - Wikipedia, the pixels nearest the centre of the kernelkernelIn image processing, a kernel, convolution matrix, or mask is a small matrix used for blurring, sharpening, embossing, edge detection, and more. This is accomplished by doing a convolution between the kernel and an image.https://en.wikipedia.org › wiki › Kernel_(image_processing)Kernel (image processing) - Wikipedia are given more weight than those far away from the centre. The rate at which this weight diminishes is determined by a Gaussian function, hence the name Gaussian blur. A Gaussian function maps random variables into a normal distribution or “Bell Curve”.


2 Answers

The Gaussian filter has a property that makes it very easy to speed up: the filter can be applied in both dimensions independently. You define a one-dimensional filter that operates vertically, and another that operates horizontally, and apply them both; this produces the same effect as a single filter applied in two dimensions.

Beyond that, you'll probably need to look at the SIMD instructions e.g. SSE3 available for your processor.

like image 101
Mark Ransom Avatar answered Oct 22 '22 05:10

Mark Ransom


To answer the second part of your question, a Gaussian blur is simply the a 3-d gaussian surface applied as a convolution kernel over the image. Wikipedia has a great reference on the algorithm itself, but basically, you take the values of a Gaussian curve and convert that into a square matrix, and multiply it by every single pixel in your image, e.g.:

Kernel:               
[0 1 2 0 0
1 4 6 4 1      X   Iterate over every single pixel in the image
2 6 10 6 2
1 4 6 4 1
0 1 2 1 0]

(Note that this is just a sample kernel, there are very specific eqns which, depending on your Gaussian variables, you'll get different results)

To answer the performance part of your question, the overall speed of this algorithm would depend on a few things, assuming a constant sized image. Lets say the image is NxM pixels, and the convolution kernel is PxP pixels. You're going to have to do PPN*M operations. The greater P, the more operations you're going to have to do for a given image. You can get crafty with the algorithm you use here, doing very specific row or columnar based math.

Implementation is also very important. If you want to be extremely efficient, you'll probably want to use the most advanced instructions that your architecture offers. If you're using an Intel x86 chip, you'll probably want to look at getting a license for Intel performance primitives (IPP) and calling those instructions directly. IIRC, OpenCV does make use of IPP when its available...

You could also do something very smart and work with all scaled integers if the floating point performance on your given architecture is poor. This would probably speed things up a bit, but I would look at other options first before going down this road.

like image 30
jdt141 Avatar answered Oct 22 '22 05:10

jdt141