3 回答

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]

TA貢獻1803條經驗 獲得超6個贊
從 HoughCircles() 你會得到一個“圓圈”列表,其中包含圓的 x、y、r。x, y 是圓心的坐標,r 是半徑。從半徑你可以計算圓的面積:
A = 圓周率 * r^2

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)
"Column Number: "
x
"Row Number: "
y
"Radius is: "
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()
添加回答
舉報