代码拉取完成,页面将自动刷新
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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。