1、把图片转化为二值图片:img1 = cv2.imread('0.png',0)_,thresh = cv2.threshold(img1,200,255,0)
2、检测轮廓,并绘制轮廓:img10 = cv2.imread('0.png')image,contours,_ = cv2.findContours(thresh,2,1)for c in contours[1:]: cv2.drawContours(img10,[c],0,(0,0,255),2)
3、绘制外接矩形,要横平竖直那种:for c in contours[1:]: x, y, w, h = cv2.boundingRect(c) # 外接矩形 cv2.rectangle(img10,(x,y),(x+w,y+h),(0,255,0),2)
4、绘制最小的外接矩形:for c in contours[1:]: rect = cv2.minAreaRect(c) box = 荏鱿胫协np.int0(cv2.boxPoints(rect)) cv2.drawContours(img10,[box],0,(255,0,0),2)
5、绘制最小外接圆:for c in contours职邗珩垃[1:]: (x, y), radius = cv2.minEnclosingCircle(c) (x, y, radius) = np.int0((x,y,radius)) cv2.circle(img10, (x,y),radius,(255,255,0),2)
6、用轮廓数据来拟合椭圆:for c in contours[1:]: ellipse = cv2.fitEllipse(c) cv2.ellipse(img10, ellipse,(255,0,255),2)这不是外接椭圆。