4

我正在尝试使用 Python OpenCV 对视频流进行 gussian 过滤器,但出现错误:

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 
4

2 回答 2

6

OpenCV 有两个模块,cv 和 cv2。

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

所有这些 OpenCV Python 接口之间有什么不同?

很简单:

import cv2
import numpy as np

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

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

查看:OpenCV 中的平滑技术

另外,检查文档。

于 2012-06-28T18:29:52.290 回答
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)
于 2013-02-07T12:39:54.183 回答