<jupyter_start><jupyter_text>Próba match template<jupyter_code>import cv2
import numpy as np

img = cv2.imread('img/aa.png')

size = [9, 6]

rows,cols,ch = img.shape

pts1 = np.float32([[384-5,64-5],[270-5,1128+5],[1898+5,108-5],[1980+5,1120+5]])
pts2 = np.float32([[0,0],[0,600*size[1]],[600*size[0],0],[600*size[0],600*size[1]]])

M = cv2.getPerspectiveTransform(pts1,pts2)

dst = cv2.warpPerspective(img,M,(600*size[0],600*size[1]))

dst = cv2.resize(dst, (0,0), None, fx=0.1, fy=0.1)
ps.draw(dst)

print(dst.shape)
correct = np.array([
    [30,50,43,39,52,6,7,13,19],
    [36,44,32,45,47,2,0,21,17],
    [37,38,41,40,48,24,5,4,10],
    [42,31,51,46,35,25,28,29,20],
    [49,53,33,34,26,12,3,15,8],
    [1,9,27,14,16,11,18,23,22]
])
import copy
base = copy.deepcopy(dst)
base = adjust_gamma(base)

templates = []
for el in results:
    loc = el[0][0].copy()

    loc = cv2.warpAffine(loc, cv2.getRotationMatrix2D((500,500), 270, 1.0), (1000, 1000))
    loc = adjust_gamma(loc)
    templates.append(loc)
    
own_try = np.zeros(correct.shape)

for i, template in enumerate(templates):
    method = cv2.TM_CCOEFF_NORMED
    template = cv2.resize(template, (0,0), None, fx=0.1, fy=0.1)
    template = template[20:80, 20:80]

    c, w, h = template.shape[::-1]

    res = cv2.matchTemplate(base,template, method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc

    bottom_right = (top_left[0] + w, top_left[1] + h)
    
    cy = top_left[0] + (h)//2
    cx = top_left[1] + (w)//2

    a = base.shape[0]//6
    b = base.shape[1]//9

    a, b = cy//a, cx//b

    if own_try[b,a] == 0:
        own_try[b,a] = i
        #cv2.rectangle(base, top_left, bottom_right, (255,255,0), 1)
own_try
print('Błąd: {:.3f} (poprawnych/wszystkich)'.format(np.count_nonzero(np.subtract(own_try,correct)==0)/(size[0]*size[1])))
def adjust_gamma(image, gamma=2.5):

    invGamma = 1.0 / gamma
    table = np.array([((i / 255.0) ** invGamma) * 255
      for i in np.arange(0, 256)]).astype("uint8")

    return cv2.LUT(image, table)
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8))

ps.draw(base)
d = results[31][0][0].copy()
for el in results[31][1][0]:
    cv2.circle(d, (el[1], el[0]), 1, (255,255,0))
    cv2.circle(d, (el[1], el[0]+30), 1, (0,128,256))

ps.draw(d)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21000413393964448, 'fraction_numerical': 0.12525837122778008, 'mean_word_length': 3.9896907216494846, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '665333', 'license': 'MIT', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1110, 'n_words': 177, 'path': 'TamplateMatching.ipynb', 'repo_name': 'bartoszptak/PuzzleSolver', 'size': '2419'}