【发布时间】:2017-08-18 14:40:09
【问题描述】:
我无法在图像上绘制圆圈。你能检查一下吗?问题在于绘制圆圈时我无法检索在图像上找到的圆圈的半径和中心。 提前致谢。
代码:(Python)
import cv,cv2
import numpy as np
def Eyes_Detect(frame):
cas = cv2.CascadeClassifier("C:\opencv2.4.10\data\haarcascades\haarcascade_eye.xml")
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
x=1.1
res=cas.detectMultiScale(gray,scaleFactor=x,minNeighbors=2,minSize=(20,50),flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
while len(res)>2:
x+=0.1
res=cas.detectMultiScale(gray,scaleFactor=x,minNeighbors=2,minSize=(20,50),flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
if len(res)<2:
return (False,[])
else:
return (True,res)
def Find_Canny_Edges(frame):
res = cv2.Canny(frame,100,200)
return res
def Find_Circles(frame):
cv.Smooth(frame,frame,cv.CV_GAUSSIAN, 7,7)
storage=cv.CreateMat(frame.width, 1, cv.CV_32FC3)
cv.HoughCircles(frame,storage,cv.CV_HOUGH_GRADIENT,2,100.0,30,150,100,140)
return storage
def Draw_Circles(image,storage):
for circle in storage:
radius = circle[2]
center = (circle[0], circle[1])
cv.Circle(image, center, radius, (0, 0, 255), 3, 8, 0)
return image
# Main
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
ct=1
while True:
ret,frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
result = Eyes_Detect(frame)
if result[0]==True:
result = Find_Canny_Edges(gray)
cv2.imwrite("image.jpg",result)
result = cv.LoadImage('image.jpg',0)
result = Find_Circles(result)
result = Draw_Circles(frame,result)
cv2.imshow("Output",result)
if cv2.waitKey(5) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
【问题讨论】:
-
为什么要将框架写入文件,然后重新加载该框架?这真的是不必要的。你是什么意思你“无法”检索半径和中心?
-
实际上,通过 cv2 模块可以找到精明的边缘。通常这将适用于普通的 numpy 数组。但是在使用 cv Hough Circles 函数时,我们需要以 cvMat 格式发送图像矩阵(我不熟悉的东西)。只需在此行之后打印结果的数据类型“结果 = cv.LoadImage('image.jpg ',0) " .... 你会看到它的数据类型。否则您可以建议一些其他方法来检索坐标 r(radius),x,y ;其中 (x,y) 是中心。 . .
-
我强烈建议不要保存中间图像...特别是使用 JPEG 压缩,因为您将获得压缩伪影以压缩图像以节省空间。压缩图像并再次加载它就像复印复印件一样。我也不明白为什么你用
cv而cv2更容易使用? -
另外,您使用的是什么版本的 OpenCV?在 Python 命令提示符中执行
import cv2、cv2.__version__并告诉我你得到了什么。 -
是的 2.4.10 。我没有在 cv2 中找到任何其他方法来使用 houghCircles 方法检索圆圈数据。我也浏览了它的参数语法(cv2.HoughCirces)。当我打印结果时,它就像一个矩阵,如 4354X1 。 4354 是图像的宽度,我将其作为存储数组(numpy)的大小,空一个来存储方法结果。