在OpenCV 2.4上找不到GaussianBlur模块

时间:2012-06-28 17:04:14

标签: python opencv

我尝试使用Python OpenCV对视频流进行过滤过滤,但是我收到错误:

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

我非常确定高斯模糊应该适用于openCV 2.4,所以我一定做错了。如果我注释掉高斯模糊线,否则代码会按照我的要求执行。

以下是整个事情:

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 

2 个答案:

答案 0 :(得分:6)

OpenCV有两个模块,cv和cv2。

对于cv,image作为cvMat加载,而对于cv2,它作为numpy数组加载。所以所有操作都在cv2模块中的numpy数组上完成。它简化了几件事。

What is different between all these OpenCV Python interfaces?

如此简单:

import cv2
import numpy as np

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

这足以让您获得模糊的结果。

退房:Smoothing Techniques in OpenCV

另外,请检查documentation.

答案 1 :(得分:2)

cv中的高斯模糊 - import cv2.cv as cv

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

但是cv2中的高斯模糊 - import cv2

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