亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何計算用 cv2.HoughCircles() 檢測到的圓的面積?

如何計算用 cv2.HoughCircles() 檢測到的圓的面積?

holdtom 2022-12-20 15:03:59
伙計們,我寫了一個程序,我用cv2.HoughCircles()識別圓圈。該代碼也有效。不幸的是,我的項目需要圓圈區域。但我不知道如何計算這個,我在互聯網上搜索也不成功。謝謝你。
查看完整描述

3 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超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 * r2


所以每個檢測到的圓的面積將是:


for i in circles[0,:]:

  area = 3.14159 * i[2] * i[2]


查看完整回答
反對 回復 2022-12-20
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

從 HoughCircles() 你會得到一個“圓圈”列表,其中包含圓的 x、y、r。x, y 是圓心的坐標,r 是半徑。從半徑你可以計算圓的面積:

A = 圓周率 * r^2


查看完整回答
反對 回復 2022-12-20
?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

如果我使用:


for i in circles[0,:]:

area = 3.14159 * i[2] * i[2]

然后我得到以下錯誤:“TypeError:'NoneType'對象不可訂閱”這是我的代碼,我想實時檢測圓圈并計算面積。


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()



查看完整回答
反對 回復 2022-12-20
  • 3 回答
  • 0 關注
  • 235 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號