简体   繁体   English

Python Opencv使用霍夫圆变换从二进制图像中检测圆

[英]Python Opencv Detecting Circles from the Binary Image by Using Hough Circle Transform

I am trying trying to split video which is threshold applied into frames then trying to find the circles in the image. 我正在尝试将阈值应用的视频分割为帧,然后尝试在图像中找到圆圈。 But I searched that converting binary image to grayscale is not possible. 但是我搜索到不可能将二进制图像转换为灰度图像。 I searched about Hough circle and the method only can take grayscale images. 我搜索了霍夫圆,该方法只能拍摄灰度图像。 Hough Lines can work on binary images but hough circles could not. 霍夫线可以在二进制图像上工作,但霍夫圆不能。 Is there any advice to use thresholded images in hough circle method? 有什么建议在霍夫圆法中使用阈值图像吗? Please help me. 请帮我。

ps: I am adding the code and image that purpose is to find the circles in the thresholded image. ps:我要添加代码和图像,目的是在阈值图像中找到圆圈。

while videoCapture.isOpened(): #Begins to detect the captures in video by frames

    ret, image = videoCapture.read()
    print("image capture opened")

    if ret == True:

        #rgb = cv2.cvtColor(image, cv2.COLOR_HLS2RGB)
        gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        bgr = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) #converting video color to gray

        print("gray scaled image\n")
        frameCounter = frameCounter + 1
        circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param1=71, param2=70, minRadius=0, maxRadius=0)
        if circles is not None:

            print("Hough Circle on each frame")
            circles = np.uint16(np.around(circles))
            for i in circles[0, :]:
                cv2.circle(bgr, (i[0],i[1]), i[2], (0, 255, 0), 2) #Outer circle in the image
                cv2.circle(bgr, (i[0],i[1]), 2, (0, 0, 255), 3) #inner circle center
                print("inner outer circle draw")

            cv2.imwrite(outputDir + "/%d.jpg" % (frameCounter), bgr) #Saving frame to the output directory
        else :
            print('Circle could not find')
            cv2.imwrite(outputDir + "/%d.jpg" % (frameCounter), bgr)  # Saving frame to the output directory

        print("image saved to directory")

        videoOutput.write(bgr)

        if(frameCounter > (frameLength-1)):
            endTime = time.time()
            videoCapture.release()
            videoOutput.release()
            print("Converting video took %d seconds." % (endTime-startTime))
            break
    else:
       break

**二进制图像(已应用阈值)**

Try using convertTo rather than cvtColor. 尝试使用convertTo而不是cvtColor。 This example works: 此示例有效:

cv::Mat image = imread("binary.bmp");
cv::Mat outImage;
image.convertTo(outImage, CV_8U);
imwrite("grayscale.bmp", outImage);

PS: you'll still have to play with param1 and param2 parameters of HoughCircles depending on how "circular" are the circles you want to detect, in your case it's really not a perfect circle. PS:您仍然必须使用HoughCircles的param1和param2参数,具体取决于您要检测的圆的“圆形”程度,在您的情况下,这实际上并不是一个完美的圆。 It would be much easyer to start detecting coins in an image to practice. 开始检测图像中的硬币进行练习会容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM