opencv绘制矩形,最小矩形,最小闭圆的轮廓

import cv2
import numpy as np

img = cv2.imread("../chapter3/hammer.jpg", cv2.IMREAD_UNCHANGED)

ret, thresh = cv2.threshold(cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY)

contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print(len(contours))
for c in contours:
    #绘制矩形框
    x, y, w, h = cv2.boundingRect(c)
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)

    rect = cv2.minAreaRect(c)#绘制最小矩形框
    box = cv2.boxPoints(rect) #找矩形的四个顶点
    box = np.int0(box)
    cv2.drawContours(img, [box], 0, (0, 0, 255), 3)

    #最小闭圆
    (x, y), radius = cv2.minEnclosingCircle(c)
    x, y, radius = [int(x) for i in [x, y, radius]]
    img = cv2.circle(img, (x, y), radius, (0, 255, 0), 2)

cv2.imshow("contours", cv2.resize(img, dsize=(300, 300)))
cv2.waitKey()

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值