0

So far this is what I have..

import cv2
list1=[]
img=cv2.imread("sudoku.jpg")
C,H,W = img.shape[::-1]
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,47,255,cv2.THRESH_BINARY) # filter out background noise
_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for i in range(len(contours)): #for loop necessary to contour multiple boxes 
    perimeter=cv2.arcLength(contours[i],True)
    if perimeter < 240 and perimeter > 150: #got these values by experimenting to find values that contour boxes but not the numbers within
        #cv2.drawContours(img, contours, i, (0,255,0), 2)
        list1.append(i)

#crop_img = img[0:61, 0:61]
for x in list1:
    M=cv2.moments(contours[x])
    cx = int(M["m10"]/M["m00"])
    cy = int(M["m01"]/M["m00"])
    cropped = img[cy-30:cy+30,cx-30:cx+29]
    w,h,c = cropped.shape
    for i in range (10):
        template = cv2.imread("no"+str(i)+".png")
        result = cv2.matchTemplate(cropped, template, cv2.TM_CCOEFF)
        min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(result)
        if(max_val > 0.9):
            grid1.append(i)
        print grid1


cv2.imshow("img",cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()

Everything works fine as far as

cropped = img[cy-30:cy+30,cx-30:cx+29]

and running this returns a cropped image of the top left cell of my Sudoku puzzle. However when I try to apply this to the rest of the puzzle via template matching, I get the following error: Error

I've googled the error but have been unable to resolve the problem. I think it may have to do with the version of Python I am using(2.7). Any help with getting the program to apply template matching and extract all cells would be greatly appreciated as I am pretty new to Opencv and image processing in general

Thanks

1 Answers1

0

From the error, it looks like matchTemplate only accepts gray images (depth == CV_8U || depth == CV_32F also, template has to be gray type == _tmpl.type()). You can try converting both to gray.

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74