Created
April 2, 2020 12:55
-
-
Save xylcbd/c8de463bd1d8d1520ad1b1509bd00d0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
# from: https://github.com/argman/EAST/blob/ab97939783901b7e22ff55e151964e159d1627b9/icdar.py | |
import cv2 | |
import numpy as np | |
import copy | |
def shrink_poly(poly, r): | |
''' | |
fit a poly inside the origin poly, maybe bugs here... | |
used for generate the score map | |
:param poly: the text poly | |
:param r: r in the paper | |
:return: the shrinked poly | |
''' | |
# shrink ratio | |
R = 0.3 | |
# find the longer pair | |
if np.linalg.norm(poly[0] - poly[1]) + np.linalg.norm(poly[2] - poly[3]) > \ | |
np.linalg.norm(poly[0] - poly[3]) + np.linalg.norm(poly[1] - poly[2]): | |
# first move (p0, p1), (p2, p3), then (p0, p3), (p1, p2) | |
## p0, p1 | |
theta = np.arctan2((poly[1][1] - poly[0][1]), (poly[1][0] - poly[0][0])) | |
poly[0][0] += R * r[0] * np.cos(theta) | |
poly[0][1] += R * r[0] * np.sin(theta) | |
poly[1][0] -= R * r[1] * np.cos(theta) | |
poly[1][1] -= R * r[1] * np.sin(theta) | |
## p2, p3 | |
theta = np.arctan2((poly[2][1] - poly[3][1]), (poly[2][0] - poly[3][0])) | |
poly[3][0] += R * r[3] * np.cos(theta) | |
poly[3][1] += R * r[3] * np.sin(theta) | |
poly[2][0] -= R * r[2] * np.cos(theta) | |
poly[2][1] -= R * r[2] * np.sin(theta) | |
## p0, p3 | |
theta = np.arctan2((poly[3][0] - poly[0][0]), (poly[3][1] - poly[0][1])) | |
poly[0][0] += R * r[0] * np.sin(theta) | |
poly[0][1] += R * r[0] * np.cos(theta) | |
poly[3][0] -= R * r[3] * np.sin(theta) | |
poly[3][1] -= R * r[3] * np.cos(theta) | |
## p1, p2 | |
theta = np.arctan2((poly[2][0] - poly[1][0]), (poly[2][1] - poly[1][1])) | |
poly[1][0] += R * r[1] * np.sin(theta) | |
poly[1][1] += R * r[1] * np.cos(theta) | |
poly[2][0] -= R * r[2] * np.sin(theta) | |
poly[2][1] -= R * r[2] * np.cos(theta) | |
else: | |
## p0, p3 | |
# print poly | |
theta = np.arctan2((poly[3][0] - poly[0][0]), (poly[3][1] - poly[0][1])) | |
poly[0][0] += R * r[0] * np.sin(theta) | |
poly[0][1] += R * r[0] * np.cos(theta) | |
poly[3][0] -= R * r[3] * np.sin(theta) | |
poly[3][1] -= R * r[3] * np.cos(theta) | |
## p1, p2 | |
theta = np.arctan2((poly[2][0] - poly[1][0]), (poly[2][1] - poly[1][1])) | |
poly[1][0] += R * r[1] * np.sin(theta) | |
poly[1][1] += R * r[1] * np.cos(theta) | |
poly[2][0] -= R * r[2] * np.sin(theta) | |
poly[2][1] -= R * r[2] * np.cos(theta) | |
## p0, p1 | |
theta = np.arctan2((poly[1][1] - poly[0][1]), (poly[1][0] - poly[0][0])) | |
poly[0][0] += R * r[0] * np.cos(theta) | |
poly[0][1] += R * r[0] * np.sin(theta) | |
poly[1][0] -= R * r[1] * np.cos(theta) | |
poly[1][1] -= R * r[1] * np.sin(theta) | |
## p2, p3 | |
theta = np.arctan2((poly[2][1] - poly[3][1]), (poly[2][0] - poly[3][0])) | |
poly[3][0] += R * r[3] * np.cos(theta) | |
poly[3][1] += R * r[3] * np.sin(theta) | |
poly[2][0] -= R * r[2] * np.cos(theta) | |
poly[2][1] -= R * r[2] * np.sin(theta) | |
return poly | |
def render(image, poly, color): | |
cv2.polylines(image, np.int32([poly]), True, color) | |
return image | |
width = 800 | |
height = 800 | |
poly = np.array([[200, 200], [490, 180], [440, 370], [210, 420]]) | |
image = np.ones((height, width, 3), np.uint8) | |
image = render(image, poly, (255, 0, 0)) | |
# res_poly = shrink_poly(copy.deepcopy(poly), [-300 for _ in range(4)]) | |
res_poly = shrink_poly(copy.deepcopy(poly), [-300, 0, -300, 0]) | |
cv2.imshow('shrinked poly', render(image, res_poly, (0, 255, 0))) | |
cv2.waitKey(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment