我正在尝试使用 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