GaussianBlur module not found on OpenCV 2.4


Question

I'm trying to do a gussian filter on a video stream with Python OpenCV but I get the error:

blur = cv.GaussianBlur(dst, (5, 5), 0)
AttributeError: 'module' object has no attribute 'GaussianBlur'

I'm pretty sure gaussian blur should work with openCV 2.4 so I must be doing something wrong. The code otherwise does what I want it to if I comment out the gaussian blur line.

Here's the whole thing:

import sys
from math import sin, cos, sqrt, pi
import cv2.cv as cv
import urllib2

if __name__ == '__main__':
    try: fn = sys.argv[1]
    except: fn = 0

def nothing(*args):
    pass

cv.NamedWindow("Source", 1)
cv.NamedWindow("Hough", 1)
cv.CreateTrackbar("rho","Hough",1,10, nothing)
cv.CreateTrackbar("thresh","Hough",1,1000, nothing)
cv.CreateTrackbar("cThresh1","Hough",0,500, nothing)
cv.CreateTrackbar("cThresh2","Hough",0,500, nothing)

while True:
    url = 'http://192.168.5.1:8080/shot.jpg'
    filedata = urllib2.urlopen(url).read()
    imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
    cv.SetData(imagefiledata, filedata, len(filedata))
    src = cv.DecodeImageM(imagefiledata, cv.CV_LOAD_IMAGE_GRAYSCALE)

#Get Image  
    dst = cv.CreateImage(cv.GetSize(src), 8, 1)
    color_dst = cv.CreateImage(cv.GetSize(src), 8, 3)
    storage = cv.CreateMemStorage(0)
    lines = 0

#blur
    blur = cv.GaussianBlur(dst, (5, 5), 0)

#Canny    
cThresh1 = cv.GetTrackbarPos('cThresh1', 'Hough')
cThresh2 = cv.GetTrackbarPos('cThresh2', 'Hough')
cv.Canny(src, dst, cThresh1, cThresh2, 5)
cv.CvtColor(blur, color_dst, cv.CV_GRAY2BGR)

#Hough     
    rho = cv.GetTrackbarPos('rho', 'Hough')
    thresh = cv.GetTrackbarPos('thresh', 'Hough')
    lines = cv.HoughLines2(dst, storage, cv.CV_HOUGH_STANDARD, rho, pi / 180, thresh, 0, 0)

    for (rho, theta) in lines[:100]:
            a = cos(theta)
            b = sin(theta)
            x0 = a * rho 
            y0 = b * rho
            pt1 = (cv.Round(x0 + 1000*(-b)), cv.Round(y0 + 1000*(a)))
            pt2 = (cv.Round(x0 - 1000*(-b)), cv.Round(y0 - 1000*(a)))
            cv.Line(color_dst, pt1, pt2, cv.RGB(255, 0, 0), 3, 8)

#Display Video  
    cv.ShowImage("Source", src)
    cv.ShowImage("Hough", color_dst)
    if cv.WaitKey(10) == 27:
        break 
1
3
6/28/2012 5:04:14 PM

Accepted Answer

OpenCV has two modules , cv and cv2.

For cv, image is loaded as cvMat while for cv2, it is loaded as numpy array. So all operations are done on numpy arrays in cv2 module. It simplifies several things.

What is different between all these OpenCV Python interfaces?

So simply:

import cv2
import numpy as np

img = cv2.imread('image.jpg')
gaussian_blur = cv2.GaussianBlur(img,(5,5),0)

This is sufficient for you to get the blurred result.

Check out : Smoothing Techniques in OpenCV

Also, check the documentation.

6
5/23/2017 11:45:33 AM

Gaussian Blur in cv -- import cv2.cv as cv

cv.Smooth(src, dst, cv.CV_GAUSSIAN, 5, 5)

but Gaussian Blur in cv2 -- import cv2

dst = cv2.GaussianBlur(src,(5,5),0)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon