Last active
August 21, 2019 19:45
-
-
Save snuke/fd208905eb2de0cd48df3b15205fd9a3 to your computer and use it in GitHub Desktop.
detect icon rotation
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 -*- | |
import cv2 | |
import numpy as np | |
def fix(name): | |
src = cv2.imread(name) | |
h,w = src.shape[:2] | |
r = min(h,w)/2 - 1 | |
center = np.array([w/2,h/2]) | |
mask = np.zeros((w,h), dtype=np.uint8) | |
cv2.ellipse(mask,((w/2,h/2),(r*2,r*2),0),(255,255,255),-1) | |
best = [-1,0] | |
for a in range(-90,90): | |
trans = cv2.getRotationMatrix2D((w/2,h/2), a, 1) | |
now = cv2.warpAffine(src, trans, (w,h)) | |
now = cv2.Canny(now,50,150,apertureSize = 3) | |
now = now & mask | |
tot = now.sum() | |
cnt = (now & np.roll(now,1,0)).sum() | |
per = 0 | |
if tot > 0: | |
per = cnt/tot | |
best = max(best, [per, a]) | |
trans = cv2.getRotationMatrix2D((w/2,h/2), best[1], 1) | |
result = cv2.warpAffine(src, trans, (w,h)) | |
# result = cv2.Canny(result,50,150,apertureSize = 3) | |
# result &= mask | |
cv2.imwrite('result.jpg', result) | |
if __name__ == '__main__': | |
fix('1.jpg') |
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 -*- | |
import cv2 | |
import numpy as np | |
def fix(name): | |
src = cv2.imread(name) | |
h,w = src.shape[:2] | |
r = min(h,w)/2 - 1 | |
center = np.array([w/2,h/2]) | |
# mask = np.zeros((w,h), dtype=np.uint8) | |
mask = np.zeros(src.shape, dtype=np.uint8) | |
cv2.ellipse(mask,((w/2,h/2),(r*2,r*2),0),(255,255,255),-1) | |
best = [-1,0] | |
for a in range(-90,90): | |
trans = cv2.getRotationMatrix2D((w/2,h/2), a, 1) | |
now = cv2.warpAffine(src, trans, (w,h)) | |
# now = cv2.Canny(now,50,150,apertureSize = 3) | |
now = now & mask | |
tot = now.sum() | |
# cnt = (now & np.roll(now,1,0)).sum() | |
cnt = (now & np.flip(now,1)).sum() | |
per = 0 | |
if tot > 0: | |
per = cnt/tot | |
best = max(best, [per, a]) | |
trans = cv2.getRotationMatrix2D((w/2,h/2), best[1], 1) | |
result = cv2.warpAffine(src, trans, (w,h)) | |
# result = cv2.Canny(result,50,150,apertureSize = 3) | |
# result &= mask | |
cv2.imwrite('result.jpg', result) | |
if __name__ == '__main__': | |
fix('1.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment