Ai
1 Star 0 Fork 0

杨雪锋/opencv-python-5

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
c8-4.py 836 Bytes
一键复制 编辑 原始数据 按行查看 历史
杨雪锋 提交于 2023-04-14 21:51 +08:00 . update ch8
import numpy as np
import cv2 as cv
img = cv.imread('approximate.png')
imggray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret,thresh = cv.threshold(imggray,127,255,0)
contours,hierarchy = cv.findContours(thresh, 1, 2)
cnt = contours[0]
# 轮廓近似
epsilon = 0.1*cv.arcLength(cnt,True)
epsilon1 = 0.01*cv.arcLength(cnt,True)
approx = cv.approxPolyDP(cnt,epsilon,True)
approx1 = cv.approxPolyDP(cnt,epsilon1,True)
# 反转颜色便于显示(可省略)
imggray = cv.bitwise_not(imggray)
img = cv.cvtColor(imggray, cv.COLOR_GRAY2BGR)
img1 = img.copy()
img2 = img.copy()
# 绘制并显示结果
cv.polylines(img1, [approx], True, (0, 255, 0), 3)
cv.polylines(img2, [approx1], True, (0, 255, 0), 3)
cv.imshow('img', np.hstack([img,img1, img2]) )
cv.imwrite('fig8-3.png',np.hstack([img,img1, img2]))
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

搜索帮助