Ai
1 Star 0 Fork 1

Leo/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
contours_hull.py 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
import cv2
import numpy as np
OPENCV_MAJOR_VERSION = int(cv2.__version__.split('.')[0])
img = cv2.pyrDown(cv2.imread("../images/hammer.jpg"))
ret, thresh = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
127, 255, cv2.THRESH_BINARY)
if OPENCV_MAJOR_VERSION >= 4:
# OpenCV 4 or a later version is being used.
contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
else:
# OpenCV 3 or an earlier version is being used.
# cv2.findContours has an extra return value.
# The extra return value is the thresholded image, which (in
# OpenCV 3.1 or an earlier version) may have been modified, but
# we can ignore it.
_, contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
black = np.zeros_like(img)
for cnt in contours:
epsilon = 0.01 * cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
hull = cv2.convexHull(cnt)
cv2.drawContours(black, [cnt], -1, (0, 255, 0), 2)
cv2.drawContours(black, [approx], -1, (255, 255, 0), 2)
cv2.drawContours(black, [hull], -1, (0, 0, 255), 2)
cv2.imshow("hull", black)
cv2.waitKey()
cv2.destroyAllWindows()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/akwkevin/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition.git
git@gitee.com:akwkevin/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition.git
akwkevin
Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition
Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition
master

搜索帮助