Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Gaussian Filter different between cv2 and skimage?

I've got an image that I apply a Gaussian Blur to using both cv2.GaussianBlur and skimage.gaussian_filter libraries, but I get significantly different results. I'm curious as to why, and what can be done to make skimage look more like cv2. I know skimage.gaussian_filter is a wrapper around scipy.scipy.ndimage.filters.gaussian_filter. To clearly state the question, why are the two functions different and what can be done to make them more similar?

Here is my test image:

Original Image

Here is the cv2 version (appears blurrier):

cv2 image

Here is the skimage/scipy version (appears sharper):

skimage version

Details:

skimage_response = skimage.filters.gaussian_filter(im, 2, multichannel=True, mode='reflect')

cv2_response = cv2.GaussianBlur(im, (33, 33), 2)

So sigma=2 and the size of the filter is big enough that it shouldn't make a difference. Imagemagick covnert -gaussian-blur 0x2 visually agrees with cv2.

Versions: cv2=2.4.10, skimage=0.11.3, scipy=0.13.3

like image 697
waldol1 Avatar asked Mar 28 '16 13:03

waldol1


2 Answers

If anyone is curious about how to make skimage.gaussian_filter() match Matlab's equivalent imgaussfilt() (the reason I found this question), pass the parameter 'truncate=2' to skimage.gaussian_filter(). Both skimage and Matlab calculate the kernel size as a function of sigma. Matlab's default is 2. Skimage's default is 4, resulting in a significantly larger kernel by default.

like image 56
user2348114 Avatar answered Nov 05 '22 20:11

user2348114


For GaussianBlur, you are using a rather large kernel (size=33), which causes a lot of smoothing. Smoothing will depend drastically on you kernel size. With your parameters each new pixel value is "averaged" in a 33*33 pixel "window".

A definition of cv2.GaussianBlur can be found here http://docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html#gsc.tab=0

In contrast, skimage.filters.gaussian seems to work on a smaller kernel. In skimage, the "size" is defined by sigma which is related to kernel size as described here: https://en.wikipedia.org/wiki/Gaussian_filter

Definition can be found here: http://scikit-image.org/docs/dev/api/skimage.filters.html#skimage.filters.gaussian

In order to get corresponding results, you'd have to work with a smaller kernel for OpenCV.

Furthermore, for both libraries, I'd strongly recommend to use up to date library versions.

like image 21
tfv Avatar answered Nov 05 '22 19:11

tfv