【发布时间】:2016-12-14 03:04:43
【问题描述】:
我正在尝试检测蓝色圆圈并且它是中心。然后在检测到的圆圈上画一个圆圈,在它的中心画一个非常小的圆圈。但我得到了一些错误。 (我使用 OpenCV 3.1.0,Python 2.7 Anaconda 64 位,PyCharm 作为 IDE)(请帮助我使用 python 代码) 我运行以下代码:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
if cap.isOpened():
while(True):
frame, _ = cap.read()
# blurring the frame that's captured
frame_gau_blur = cv2.GaussianBlur(frame, (3, 3), 0)
# converting BGR to HSV
hsv = cv2.cvtColor(frame_gau_blur, cv2.COLOR_BGR2HSV)
# the range of blue color in HSV
lower_blue = np.array([110, 50, 50])
higher_blue = np.array([130, 255, 255])
# getting the range of blue color in frame
blue_range = cv2.inRange(hsv, lower_blue, higher_blue)
# getting the V channel which is the gray channel
blue_s_gray = blue_range[::2]
# applying HoughCircles
circles = cv2.HoughCircles(blue_s_gray, cv2.HOUGH_GRADIENT, 1, 10, 100, 30, 5, 50)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# drawing on detected circle and its center
cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2)
cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('circles', frame)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
else:
print "Can't find camera"
我运行代码时遇到的错误是:
OpenCV 错误:在 cv::cvtColor、文件 C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv 中断言失败(深度 == CV_8U || 深度 == CV_16U || 深度 == CV_32F) \modules\imgproc\src\color.cpp,第 7935 行 回溯(最近一次通话最后): 文件“C:/Users/Meliodas/PycharmProjects/OpenCV_By_Examples/code_tester.py”,第 11 行,在 hsv = cv2.cvtColor(frame_gau_blur, cv2.COLOR_BGR2HSV) cv2.error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp:7935: 错误: (-215) depth == CV_8U ||深度 == CV_16U ||函数 cv::cvtColor 中的深度 == CV_32F 非常感谢您的帮助!
【问题讨论】:
-
frame_gau_blur 的类型是什么?使用 frame_gau_blur.dtype
-
如何查看“frame_gau_blur”的数据类型?非常抱歉,我对 numpy 和 python 以及计算机视觉也很陌生。 @AmitayNachmani
-
打印 frame_gau_blur.dtype
-
frame_gau_blur.dtype @AmitayNachmani 的数据类型为“float 64”
-
您可以看到您得到的异常是您需要以下之一:“depth == CV_8U || depth == CV_16U || depth == CV_32F”因此尝试执行 hsv = cv2 .cvtColor(frame_gau_blur.astype(np.float32, cv2.COLOR_BGR2HSV)
标签: python python-2.7 opencv computer-vision opencv3.1