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