opencv包络圆_opencv学习记录之图像轮廓之三

轮廓拟合

矩形包围轮廓

1,函数cv2.boundingRect()能够绘制轮廓的矩形边界

retval = cv2.boundingRect( array)

retval 表示返回矩形边界左上角顶点的坐标值及矩形边界的宽和高 , 也可以是4个返回值形式

x , y ,w ,h  = cv2.boundingRect( array)

array 是灰度图像或轮廓

然后使用函数cv2.drawContours()来绘制矩形包围框

1 importcv22 importnumpy as np3 o = cv2.imread("cc.bmp")4 cv2.imshow("original", o)5 gray =cv2.cvtColor(o , cv2.COLOR_BGR2GRAY)6 ret , binary = cv2.threshold(gray , 127 , 255,cv2.THRESH_BINARY)7 contours , hierarchy =cv2.findContours(binary , cv2.RETR_LIST ,8 cv2.CHAIN_APPROX_SIMPLE)9 x,y,w,h =cv2.boundingRect(contours[0])10 brcnt = np.array([[[x,y]] ,[[x+w , y]] , [[x+w , y+h]] , [[x,y+h]]] )11 cv2.drawContours(o , [brcnt] , -1 ,(255,255,255) , 2)12 cv2.imshow("result",o)13 cv2.waitKey()14 cv2.destroyAllWindows()

原图

66f5b33fb1eb6e575dc9d8f332a1f825.png

效果图

320be3e70b0fbc9b7f4477b83ba52b64.png

2,使用函数cv2.boundingRect() 即cv2.rectangle()绘制矩形包围框

1 importcv22 importnumpy as np3 o = cv2.imread("cc.bmp")4 gray =cv2.cvtColor(o , cv2.COLOR_BGR2GRAY)5 ret , binary = cv2.threshold(gray , 127 , 255,cv2.THRESH_BINARY)6 contours , hierarchy =cv2.findContours(binary , cv2.RETR_LIST ,7 cv2.CHAIN_APPROX_SIMPLE)8 x,y,w,h =cv2.boundingRect(contours[0])9 cv2.rectangle(o , (x,y) , (x+w , y+h) ,(255,255,255) , 2)10 cv2.imshow("result",o)11 cv2.waitKey()12 cv2.destroyAllWindows()

效果图

c8bdfd2ce69a49645c0a43c753e6661a.png

3。,最小包围矩形框函数cv2.minAreaRect()

该函数能绘制轮廓的最小包围矩形框,函数形式:

retval = cv2.minAreaRect( points )

返回值retval表示返回矩阵特征的信息,结构为(最小外接矩形的中心(x,y) , (宽度, 高度) , 旋转角度)

poin

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值