[SciPy-User] Gaussian Filter

Brian Thorne hardbyte at gmail.com
Fri Sep 4 10:33:09 EDT 2009


Similar question, but now a bit harder. I have this code (pieced together
from a few files) that does a gaussian filter on a single image in both
OpenCV and in SciPy.It is now at a point where I cannot tell them apart with
a visual inspection, but a imshow(image1 - image2) begs to differ. Is it
going to be possible to get the exact same output?


from opencv import cv
from opencv import adaptors
from __future__ import division
import numpy as np
from numpy import array, uint8
from scipy import signal, ndimage

@scipyFromOpenCV
def gaussianBlur(np_image):
    """Blur an image with scipy"""
    sigma = opencvFilt2sigma(43.0)

    result = ndimage.filters.gaussian_filter(np_image,
                            sigma=(sigma, sigma, 0),
                            order=0,
                            mode='reflect'
                            )
    return result

def gaussianBlur(image, filterSize=43, sigma=opencvFilt2sigma(43)):
    """Blur an image with a particular strength filter.
    Default is 43, 139 gives a very strong blur, but takes a while
    """

    # Carry out the filter operation
    cv.cvSmooth(image, image, cv.CV_GAUSSIAN, filterSize, 0, sigma)
    return image

def opencvFilt2sigma(size):
    """OpenCV defaults to making sigma up with this formula.
    Learning OpenCV: computer vision with the OpenCV library
    By Gary Bradski, Adrian Kaehler pg 112"""
    return (( size*0.5 ) - 1)*0.30 + 0.80

class scipyFromOpenCV(object):
    """This decorator can be used to wrap a function that takes
    and returns a numpy array into one that takes and retuns an
    opencv CvMat.
    """
    def __init__(self, f):
        self.f = f

    def __call__(self, image):
        # Convert CvMat to ndarray
        np_image = adaptors.Ipl2NumPy(image)

        # Call the original function
        np_image_filtered = self.f(np_image)

        # Convert back to CvMat
        return adaptors.NumPy2Ipl(np_image_filtered)

cheers,

Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20090905/f019be1a/attachment.html>


More information about the SciPy-User mailing list