python - 使用 OpenCV 检测彩色圆圈及其中心

标签 python python-2.7 opencv computer-vision opencv3.1

我正在尝试检测蓝色圆圈,它位于中心。然后在检测到的圆上画一个圆,在它的中心画一个非常小的圆。但是我遇到了一些错误。 (我正在使用 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 Error: Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F) in cv::cvtColor, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp, line 7935 Traceback (most recent call last): File "C:/Users/Meliodas/PycharmProjects/OpenCV_By_Examples/code_tester.py", line 11, in 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: error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cv::cvtColor Thanks a lot in advance for your help!

最佳答案

我已经解决了我的问题,在网上查找错误的含义(我得到的错误)后,我能够找到解决它们的方法,因此我能够解决它们。如果您运行下面给出的以下代码,您应该能够很好地检测到蓝色圆圈。非常感谢那些试图帮助我解决问题的人。

代码如下:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
if cap.isOpened():
    while(True):
        ret, 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)
        res_blue = cv2.bitwise_and(frame_gau_blur,frame_gau_blur, mask=blue_range)
        blue_s_gray = cv2.cvtColor(res_blue, cv2.COLOR_BGR2GRAY)
        canny_edge = cv2.Canny(blue_s_gray, 50, 240)
        # applying HoughCircles
        circles = cv2.HoughCircles(canny_edge, cv2.HOUGH_GRADIENT, dp=1, minDist=10, param1=10, param2=20, minRadius=100, maxRadius=120)
        cir_cen = []
        if circles != None:
            # 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)
                cir_cen.append((i[0],i[1]))
        print cir_cen
        cv2.imshow('circles', frame)
        cv2.imshow('gray', blue_s_gray)
        cv2.imshow('canny', canny_edge)
        k = cv2.waitKey(5) & 0xFF
        if k == 27:
            break
    cv2.destroyAllWindows()
else:
    print 'no cam'

关于python - 使用 OpenCV 检测彩色圆圈及其中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38827505/

相关文章:

python - 使用 psycopg2 将多行插入 postgreSQL 时出错

python - NumPy - frombuffer 和 fromstring 有什么区别?

python - 如何用python连接远程mysql服务器

image-processing - 在视频输入库或 OpenCV 中禁用自动对焦

python - python开发的Jedi/rope/emacs配置

python - groupby cumsum 排序数据框

networking - Scapy 原始数据操作

python - 将 Scipy minimize (scipy.optimize.minimize) 与大型等式约束矩阵结合使用

python - 和有什么区别!和 !!在 yaml 中?

Java Opencv connectedComponentsWithStats