Guest User

Untitled

a guest
Oct 15th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. import sys
  2.  
  3. import cv2
  4. import numpy as np
  5.  
  6.  
  7. def main(fn):
  8. SZ = 21
  9. OUTPUT_ARRAY = [[0]*SZ for i in range(SZ)]
  10. img = cv2.imread(fn)
  11. gr = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  12. gr = cv2.threshold(gr, 190, 255, cv2.THRESH_BINARY_INV)[1]
  13.  
  14. kernel = np.ones((2, 2), dtype=np.uint8)
  15. gr = cv2.morphologyEx(gr, cv2.MORPH_OPEN, kernel)
  16. gr = cv2.morphologyEx(gr, cv2.MORPH_CLOSE, kernel)
  17.  
  18. (__im2, cnts, __hierarchy) = cv2.findContours(gr, cv2.RETR_EXTERNAL,
  19. cv2.CHAIN_APPROX_SIMPLE)
  20.  
  21. # get the biggest shape
  22. cnts.sort(key=cv2.contourArea)
  23. cnt = cnts[-1]
  24. cv2.drawContours(img, [cnt], -1, (0, 255, 255), 4)
  25.  
  26. # find the enclosing square
  27. x, y, w, h = cv2.boundingRect(cnt)
  28. cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
  29.  
  30. mask = np.zeros(gr.shape, dtype=np.uint8)
  31.  
  32. # now, we know that the field is 31x31
  33. w0 = w / SZ
  34. h0 = h / SZ
  35. for i in range(SZ):
  36. for j in range(SZ):
  37. cv2.rectangle(
  38. mask,
  39. (int(x + i * w0 + 10), int(y + j * w0 + 12)),
  40. (int(x + i * w0 + w0 - 14), int(y + j * h0 + h0 - 12)),
  41. 255, -1,
  42. )
  43.  
  44. gr = cv2.bitwise_and(gr, mask)
  45.  
  46. KNOWN_NUMBERS = [
  47. # number, x-coord, y-coord
  48. (1, 9, 2), (1, 10, 16), (1, 14, 12),
  49.  
  50. (2, 9, 5), (2, 2, 6), (2, 2, 7), (2, 12, 17), (2, 9, 15),
  51.  
  52. (3, 10, 1), (3, 10, 11), (3, 10, 14), (3, 10, 18), (3, 18, 5),
  53. (3, 2, 4), (3, 16, 9),
  54.  
  55. (4, 10, 2), (4, 1, 7), (4, 1, 8),
  56.  
  57. (5, 11, 2), (5, 18, 9), (5, 1, 11), (5, 3, 16),
  58. (6, 13, 1), (6, 9, 16),
  59.  
  60. (7, 13, 2), (7, 1, 10), (7, 3, 4), (7, 11, 0),
  61.  
  62. (8, 9, 1), (8, 6, 4), (8, 20, 11), (8, 4, 6),
  63.  
  64. (9, 6, 3), (9, 6, 12),
  65. ]
  66.  
  67. #serious_mark(gr, KNOWN_NUMBERS, 1, x, y, w0, h0)
  68.  
  69. TEMPLATES = []
  70. for (n, xx, yy) in KNOWN_NUMBERS:
  71. TEMPLATES.append((n, get_sq_function(gr, xx, yy, x, y, w0, h0)))
  72.  
  73. # visualize_search(gr, TEMPLATES, x, y, w0, h0, 10, 16)
  74.  
  75. out = cv2.cvtColor(gr, cv2.COLOR_GRAY2RGB)
  76. for i in range(SZ):
  77. for j in range(SZ):
  78. sq = get_sq_function(gr, i, j, x, y, w0, h0)
  79. if sq is None:
  80. continue
  81.  
  82. results = []
  83. for (number, templ) in TEMPLATES:
  84. result = cv2.matchTemplate(sq, templ, cv2.TM_CCOEFF)
  85. (_, score, _, _) = cv2.minMaxLoc(result)
  86. if score > 1:
  87. results.append((score, number))
  88. if results:
  89. results.sort()
  90. mark_square(out, i, j, x, y, w0, h0, results[-1][1])
  91. OUTPUT_ARRAY[j][i] = results[-1][1]
  92.  
  93. cv2.imwrite("out.jpg", out)
  94. return OUTPUT_ARRAY
  95.  
  96.  
  97. def serious_mark(img, nums, hl, x, y, w0, h0):
  98. for (n, xx, yy) in nums:
  99. if n != hl:
  100. continue
  101. mark_square(img, xx, yy, x, y, w0, h0, n)
  102. cv2.imwrite("out.jpg", img)
  103. sys.exit(0)
  104.  
  105.  
  106. def visualize_search(img, TEMPLATES, x, y, w0, h0, xx, yy):
  107. out = np.zeros((300, 2000), dtype=np.uint8)
  108. sq = get_sq_function(img, xx, yy, x, y, w0, h0)
  109.  
  110. X = 20
  111. results = []
  112. for (number, templ) in TEMPLATES:
  113. result = cv2.matchTemplate(sq, templ, cv2.TM_CCOEFF)
  114. out[20:20 + sq.shape[0], X:X + sq.shape[1]] = sq
  115. out[150:150 + templ.shape[0], X:X + templ.shape[1]] = templ
  116. (_, score, _, _) = cv2.minMaxLoc(result)
  117. results.append((score, number))
  118. X += sq.shape[1] + 5
  119.  
  120. best = max([i[0] for i in results])
  121. X = 20
  122. for (val, num) in results:
  123. cv2.putText(out, "%0.2f" % (val / best, ),
  124. (X, 120),
  125. cv2.FONT_HERSHEY_SIMPLEX,
  126. 0.4, 255, 1, cv2.LINE_AA)
  127. X += sq.shape[1] + 5
  128.  
  129. cv2.imwrite("out.jpg", out)
  130. sys.exit(0)
  131.  
  132.  
  133. def find_fit_square(img, x, y, x0, y0, w0, h0):
  134. sub_img = extract_square(img, x, y, x0, y0, w0, h0)
  135. (__im2, cnts, __hierarchy) = cv2.findContours(sub_img, cv2.RETR_EXTERNAL,
  136. cv2.CHAIN_APPROX_SIMPLE)
  137.  
  138. if not cnts:
  139. return None
  140.  
  141. min_x = min_y = 100000
  142. max_x = max_y = -100000
  143. for i in cnts:
  144. c = i.reshape((i.shape[0] * i.shape[-1]))
  145. min_x = min((min_x, min(c[0::2])))
  146. max_x = max((max_x, max(c[0::2])))
  147. min_y = min((min_y, min(c[1::2])))
  148. max_y = max((max_y, max(c[1::2])))
  149.  
  150. sub_sub = sub_img[min_y:max_y, min_x:max_y]
  151. res = cv2.resize(sub_sub, (int(w0), int(h0)), interpolation=cv2.INTER_CUBIC)
  152. return res
  153.  
  154.  
  155. def extract_square(img, x, y, x0, y0, w0, h0):
  156. x_base = int(x0 + x * w0)
  157. y_base = int(y0 + y * h0)
  158. h0 = int(h0)
  159. w0 = int(w0)
  160. return img[y_base + 12:y_base + h0 - 12, x_base + 10:x_base + w0 - 14]
  161.  
  162.  
  163. get_sq_function = find_fit_square
  164.  
  165.  
  166. def mark_square(img, x, y, x0, y0, w0, h0, text='X'):
  167. COLOR = 255
  168. if 3 == len(img.shape):
  169. COLOR = (0, 0, 255)
  170. cv2.putText(img, str(text),
  171. (int(x0 + x * w0 + 0.3 * w0), int(y0 + y * h0 + 0.7 * h0)),
  172. cv2.FONT_HERSHEY_SIMPLEX,
  173. 1.4, COLOR, 2, cv2.LINE_AA)
  174. cv2.putText(img, "%s" % (x, ),
  175. (int(x0 + x * w0 + 3), int(y0 + y * h0 + 0.6 * h0)),
  176. cv2.FONT_HERSHEY_SIMPLEX,
  177. 0.4, COLOR, 1, cv2.LINE_AA)
  178. cv2.putText(img, "%s" % (y, ),
  179. (int(x0 + x * w0 + 3), int(y0 + y * h0 + 0.8 * h0)),
  180. cv2.FONT_HERSHEY_SIMPLEX,
  181. 0.4, COLOR, 1, cv2.LINE_AA)
  182.  
  183.  
  184. if "__main__" == __name__:
  185. ar = main("hlavolam.jpg")
  186. print(ar)
Add Comment
Please, Sign In to add comment