def get_operator(path, url=False, expand=False):
if url:
req = requests.get(path)
arr = np.asarray(bytearray(req.content), dtype=np.uint8)
shape = cv2.resize(cv2.imdecode(arr, -1), (69, 69))
else:
shape = cv2.resize(cv2.imread('shape.png'), (69, 69))
shape_gray = cv2.cvtColor(shape, cv2.COLOR_BGR2GRAY)
_, shape_binary = cv2.threshold(shape_gray, 127, 255, cv2.THRESH_BINARY)
_, contours, hierarchy = cv2.findContours(shape_binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contour = contours[0]
operator = np.zeros((69, 69))
for point in contour:
operator[point[0][0]][point[0][1]] = 1
if expand:
if point[0][0] > 0:
operator[point[0][0] - 1][point[0][1]] = 1
if point[0][0] < 68:
operator[point[0][0] + 1][point[0][1]] = 1
if point[0][1] > 0:
operator[point[0][0]][point[0][1] - 1] = 1
if point[0][1] < 68:
operator[point[0][0]][point[0][1] + 1] = 1
return operator
评论列表
文章目录