【问题标题】:How can I calculate the area of a circle which I detected with cv2.HoughCircles()?如何计算用 cv2.HoughCircles() 检测到的圆的面积?
【发布时间】:2020-06-02 12:04:48
【问题描述】:

伙计们,

我编写了一个程序,我用 cv2.HoughCircles() 识别圆圈。该代码也有效。不幸的是,我的项目需要圆圈的区域。但我不知道如何计算,我在互联网上搜索不成功。

谢谢。

【问题讨论】:

    标签: python opencv image-processing


    【解决方案1】:

    https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_houghcircles/py_houghcircles.html

    import cv2
    import numpy as np
    
    img = cv2.imread('opencv_logo.png',0)
    img = cv2.medianBlur(img,5)
    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
    
    circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
                                param1=50,param2=30,minRadius=0,maxRadius=0)
    
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        # draw the outer circle
        cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
        # draw the center of the circle
        cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
    
    cv2.imshow('detected circles',cimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    i[0] 是 x 位置
    i[1] 是 y 位置
    i[2] 是半径

    面积计算公式为pi * r²

    所以每个检测到的圆圈的面积是:

    for i in circles[0,:]:
      area = 3.14159 * i[2] * i[2]
    

    【讨论】:

      【解决方案2】:

      从 HoughCircles() 可以得到一个包含圆的 x、y、r 的“圆”列表。 x, y 是中心的坐标,r 是半径。 根据半径,您可以计算圆的面积:

      A = PI * r^2

      【讨论】:

        【解决方案3】:

        谢谢你们,

        @Tin Nguyen

        如果我使用:

        for i in circles[0,:]:
        area = 3.14159 * i[2] * i[2]
        

        然后我收到以下错误:“TypeError: 'NoneType' object is not subscriptable” 这是我的代码,我想实时检测圆圈并计算面积。

        import pypylon.pylon as py  # wrapper to control Basler camera with python
        import cv2  # openCV
        import numpy as np
        
        first_device = py.TlFactory.GetInstance().CreateFirstDevice()
        icam = py.InstantCamera(first_device)
        icam.Open()
        # set parameters
        icam.PixelFormat = "RGB8"
        # if only a part of image sensor is used an offset is required or centering
        '''icam.Width = 640
        icam.Height = 480
        icam.CenterX = False
        icam.CenterY = False'''
        # Demonstration of setting parameters - properties can be found on Pylon Viewer
        # Auto property values are 'Off', 'Once', 'Continuous'
        icam.GainAuto = 'Off'
        icam.ExposureAuto = 'Continuous'
        icam.BalanceWhiteAuto = 'Off'
        icam.Gain = 0  # minimum gain value
        # icam.ExposureTime       = 50000 # exposure time or use ExposureAuto
        icam.StartGrabbing(py.GrabStrategy_LatestImages)
        while True:
           res = icam.RetrieveResult(1000)  # 1000 = time constant for time-out
           frame = cv2.cvtColor(res.Array, cv2.COLOR_RGB2BGR)
        
           output = frame.copy()
           gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        
           gray = cv2.GaussianBlur(gray,(5,5),0)
           gray = cv2.medianBlur(gray, 5)
        
           gray = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
                                     cv2.THRESH_BINARY,11,3.5)
        
           kernel = np.ones((2,3),np.uint8)
           gray = cv2.erode(gray,kernel)
        
           gray = cv2.dilate(gray, kernel)
        
           # detect circles in the image
           circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 200, param1=30, param2=45, 
                               minRadius=0, maxRadius=0)
        # print circles
        
        # ensure at least some circles were found
        if circles is not None:
            # convert the (x, y) coordinates and radius of the circles to integers
            circles = np.round(circles[0, :]).astype("int")
        
            # loop over the (x, y) coordinates and radius of the circles
            for (x, y, r) in circles:
                # draw the circle in the output image, then draw a rectangle in the image
                # corresponding to the center of the circle
                cv2.circle(output, (x, y), r, (0, 255, 0), 4)
                cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
                # time.sleep(0.5)
                print
                "Column Number: "
                print
                x
                print
                "Row Number: "
                print
                y
                print
                "Radius is: "
                print
                r
        
                # Display the resulting frame
                cv2.imshow('gray', gray)
        cv2.imshow('frame', output)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        
        for i in circles[0, :]:
            area = 3.14159 * i[2] * i[2]
            print(area)
        
        icam.StopGrabbing()
        icam.Close()
        cv2.destroyAllWindows()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-16
          • 2017-07-12
          • 1970-01-01
          相关资源
          最近更新 更多