慕神8447489
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_houghcircles/py_houghcircles.htmlimport cv2import numpy as npimg = 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]
拉丁的传说
如果我使用: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 pythonimport cv2 # openCVimport numpy as npfirst_device = py.TlFactory.GetInstance().CreateFirstDevice()icam = py.InstantCamera(first_device)icam.Open()# set parametersicam.PixelFormat = "RGB8"# if only a part of image sensor is used an offset is required or centering'''icam.Width = 640icam.Height = 480icam.CenterX = Falseicam.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 ExposureAutoicam.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 foundif 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'): breakfor i in circles[0, :]: area = 3.14159 * i[2] * i[2] print(area)icam.StopGrabbing()icam.Close()cv2.destroyAllWindows()