Ai
1 Star 5 Fork 1

cubone/learnopencv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
example.py 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
kushashwa ravi shrimali 提交于 2018-08-08 22:29 +08:00 . Added Convex Hull Code
import cv2
import numpy as np
import sys
if __name__ == "__main__":
if(len(sys.argv)) < 2:
file_path = "sample.jpg"
else:
file_path = sys.argv[1]
# read image
src = cv2.imread(file_path, 1)
# show source image
cv2.imshow("Source", src)
# convert image to gray scale
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# blur the image
blur = cv2.blur(gray, (3, 3))
# binary thresholding of the image
ret, thresh = cv2.threshold(blur, 200, 255, cv2.THRESH_BINARY)
# find contours
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, \
cv2.CHAIN_APPROX_SIMPLE)
# create hull array for convexHull points
hull = []
# calculate points for each contour
for i in range(len(contours)):
hull.append(cv2.convexHull(contours[i], False))
# create an empty black image
drawing = np.zeros((thresh.shape[0], thresh.shape[1], 3), np.uint8)
# draw contours and hull points
for i in range(len(contours)):
color_contours = (0, 255, 0) # color for contours
color = (255, 255, 255) # color for convex hull
# draw contours
cv2.drawContours(drawing, contours, i, color_contours, 2, 8, hierarchy)
# draw convex hull
cv2.drawContours(drawing, hull, i, color, 2, 8)
cv2.imshow("Output", drawing)
cv2.waitKey(0)
cv2.destroyAllWindows()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cubone/learnopencv.git
git@gitee.com:cubone/learnopencv.git
cubone
learnopencv
learnopencv
master

搜索帮助