Ai
1 Star 0 Fork 0

杨雪锋/opencv-python-5

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
c8-6.py 668 Bytes
一键复制 编辑 原始数据 按行查看 历史
杨雪锋 提交于 2023-04-14 21:51 +08:00 . update ch8
import numpy as np
import cv2 as cv
img = cv.imread('lightning.png')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret,thresh = cv.threshold(gray,127,255,0)
contours,hierarchy = cv.findContours(thresh,1,2)
cnt = contours[0]
# 反转颜色便于显示(可省略)
gray = cv.bitwise_not(gray)
img = cv.cvtColor(gray, cv.COLOR_GRAY2BGR)
# 直边外接矩形绘制
x,y,w,h = cv.boundingRect(cnt)
cv.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
# 旋转矩形绘制
rect = cv.minAreaRect(cnt)
box = cv.boxPoints(rect)
box = np.int0(box)
cv.drawContours(img,[box],0,(0,0,255),2)
cv.imshow('rectangle',img )
cv.imwrite('fig8-6.png',img)
cv.waitKey(0)
cv.destroyAllWindows()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/hopeasy/opencv-python-5.git
git@gitee.com:hopeasy/opencv-python-5.git
hopeasy
opencv-python-5
opencv-python-5
master

搜索帮助