-
Notifications
You must be signed in to change notification settings - Fork 3
/
renderer.py
109 lines (102 loc) · 4.06 KB
/
renderer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import common
import cv2
import numpy as np
from collections import namedtuple
def render_glyph(glyph, heigh, bezel=20, thic=26, blur=True, font=cv2.FONT_HERSHEY_SCRIPT_COMPLEX):
"""Returns a picture of a given glyph with a given size"""
scale = cv2.getFontScaleFromHeight(cv2.FONT_HERSHEY_SIMPLEX, heigh)
size, bl = cv2.getTextSize(glyph, cv2.FONT_HERSHEY_SIMPLEX, scale, thic)
size = (size[0]+thic+bezel*2, size[1]+bezel*2)
result = np.zeros(size, dtype=np.uint8)
center = (0+thic//2+bezel, size[1]-bezel)
cv2.putText(result, glyph, center, font, scale, (255,255,255), thic)
if blur:
result = cv2.medianBlur(result, 15)
return result
def get_all_glyphs_refs(chars, heigh=200, bezel=20, thic=20, fonts=[cv2.FONT_HERSHEY_SCRIPT_COMPLEX]):
"""Generates all glyphs picture from a string and given parameters"""
result = dict()
for font in fonts:
for g in chars:
result[g] = render_glyph(g, heigh, bezel, thic, font=font)
return result
def approximate(img, e=.006):
"""Approximates a contourn into smaller area"""
_, cnt, hi = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
#cnt = cnt[0]
aprx = list()
for c in cnt:
#print("{} {}".format(len(c), h))
epsilon = e * cv2.arcLength(c, True)
aprx.append(cv2.approxPolyDP(c, epsilon, True))
sho = np.zeros_like(img, np.uint8) #cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
# cv2.drawContours(sho, cnt, -1, (0,0,255), 2)
# cv2.drawContours(sho, aprx, -1, (0,255,0), 3)
cv2.fillPoly(sho, pts=aprx, color=(255,0,0))
# cv2.imshow("A", sho)
return sho
## hic sunt trash code
#if __name__ == "__main__":
# c = get_all_glyphs_refs("1234567890")
# print("Calculated {} tables".format(len(c)))
#
# infos = get_infos(c)
#
# sub = cv2.imread("ref/gen/con/1.jpg")
# sub = cv2.cvtColor(sub, cv2.COLOR_BGR2GRAY)
# _, sub = cv2.threshold(sub, 0, 255, cv2.THRESH_OTSU+cv2.THRESH_BINARY)
# sub = cv2.morphologyEx(sub, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5)))
#
# from neighbour import clean
#
# sub = clean(sub)
# #sub = sub[:,100:]
#
# sub = cv2.medianBlur(sub, 11)
#
# sift = cv2.SIFT_create()
# kp, des = sift.detectAndCompute(sub, None)
# cv2.imshow("k", cv2.drawKeypoints(sub.copy(), kp, sub.copy()))
#
# max_v, max_img, max_i = 0, None, "nothing"
# FIKT = 1
# MMC = 4
# ip = dict(algorithm=FIKT, trees=5)
# sp = dict(checks=50)
#
#
# flann = cv2.FlannBasedMatcher(ip, sp)
# for (l, m), i in zip(c.items(), infos):
# #cv2.imshow("Base {}".format(l), cv2.drawKeypoints(m, i.kp, m))
# #bf = cv2.BFMatcher()
#
# matches = [m for m, n in flann.knnMatch(des, i.des, k=2) if m.distance < .75 * n.distance]
# if len(matches) > MMC:
# src = np.float32([kp[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
# dst = np.float32([i.kp[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)
#
# M, mask = cv2.findHomography(src, dst, cv2.RANSAC, 20.0)
# if M is not None:
# print("Found an homography for {}".format(l))
# matMask = mask.ravel().tolist()
# h, w = sub.shape
# pts = np.float32([[0,0],[0,h-1],[w-1,h-1],[w-1,0]]).reshape(-1, 1, 2)
# dst_i = cv2.perspectiveTransform(pts, M)
# #m = cv2.polylines(m, [np.int32(dst_i)], True, 255, 3, cv2.LINE_AA)
# dp = dict(matchColor=(0,255,0), singlePointColor=None, matchesMask=matMask, flags=2)
# img3 = cv2.drawMatches(sub, kp, m, i.kp, matches, None, **dp)
# cv2.imshow("Homo {}".format(l), img3)
#
# cv2.imshow(l, cv2.drawMatchesKnn(sub, kp, m, i.kp, [[m] for m in matches], None, flags=2))
# print("matches('{}') := {}".format(l, len(matches)))
# if max_v < len(matches):
# max_v = len(matches)
# max_img = m
# max_i = l
#
#
# #cv2.imshow("Res", sub)
# print("Found {} with ({}) matches".format(max_i, max_v))
#
# cv2.waitKey(0)
# cv2.destroyAllWindows()