So i want to recognize the numbers of a sudoko by doing a template matching.
For some of the photos, the template matching goes well, but for some images it goes wrong, what should I do to improve the precision? I read that i should improve my templates but i don't know what to do anymore.
I attach function where i do matching and also my github repo for the rest of the project.
input:
Thanks!
def get_results(img,lines_horizontal,lines_vertical): #k=0 for i in range(len(lines_horizontal)-1): for j in range(len(lines_vertical)-1): y_min = lines_vertical[j][0][0] y_max = lines_verticalj + 1[0] x_min = lines_horizontal[i]0 x_max = lines_horizontali + 11 patch = img_crop[x_min:x_max, y_min:y_max].copy()
# w, h = template[4].shape[::-1]
img_gray = cv.cvtColor(patch, cv.COLOR_BGR2GRAY)
threshold = 0.9
# Store the coordinates of matched area in a numpy array
# Draw a rectangle around the matched region.
# Show the final image with the matched area.
#cv.imshow('Detected',patch)
patch_2=img_crop[x_min+17:x_max-12,y_min+12:y_max-18].copy()
# cv.imshow("patch", patch_2)
#k+=1
image = cv.cvtColor(patch_2,cv.COLOR_BGR2GRAY)
image_m_blur = cv.medianBlur(image,5)
image_g_blur = cv.GaussianBlur(image_m_blur, (0, 0), 5)
image_sharpened = cv.addWeighted(image_m_blur, 1.2, image_g_blur, -0.8, 0)
_, thresh = cv.threshold(image_sharpened, 20, 255, cv.THRESH_BINARY)
kernel = np.ones((5, 5), np.uint8)
thresh = cv.erode(thresh, kernel)
img_gray=cv.cvtColor(patch,cv.COLOR_BGR2GRAY)
img_grayt=cv.normalize(img_gray,None, alpha=0, beta=255, norm_type=cv.NORM_MINMAX)
img_grayt=img_grayt[2:-2,8:-8]
#cv.imshow("patch", thresh)
print(np.mean(thresh))
#show_image("patch",img_grayt)
if(np.mean(thresh)>253):
f.write("o")
g.write("o")
else:
f.write("x")
ok=0
while ok!=1:
for k in range(0,9):
res = cv.matchTemplate(img_grayt,template[k],cv.TM_CCOEFF_NORMED)
show_image("template",template[k])
show_image("patch",img_grayt)
loc = np.where( res >= threshold)
if len(loc[0]>1):
g.write(str(k+1))
ok=1
break
threshold-=0.03
cv.waitKey(0)
cv.destroyAllWindows()
f.write("\n")
g.write("\n")