forked from geekpradd/Visual-Sudoku-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgProc.py
58 lines (46 loc) · 1.53 KB
/
imgProc.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
import cv2
import numpy as np
#import subprocess
img = cv2.imread('sud6.jpg')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(imgray, (11, 11), 0)
th = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
cv2.THRESH_BINARY,5,2)
kernel = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], np.uint8)
erosion = cv2.erode(th, kernel, iterations = 1)
# cv2.imshow('image', erosion)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
contours, hierarchy = cv2.findContours(erosion, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
maxA = cv2.contourArea(contours[0], True)
max_i = 0
for i in range(1, len(contours)) :
area = cv2.contourArea(contours[i], True)
if area > maxA :
maxA = area
max_i = i
mask = np.zeros(imgray.shape,np.uint8)
cv2.drawContours(mask, contours, max_i, 255, -1)
pixelpoints = np.nonzero(mask)
X = pixelpoints[1]
Y = pixelpoints[0]
SUM = X + Y
DIFF = X - Y
a1 = np.argmax(SUM)
a2 = np.argmin(SUM)
a3 = np.argmax(DIFF)
a4 = np.argmin(DIFF)
pts1 = np.float32([[X[a2], Y[a2]], [X[a3], Y[a3]], [X[a1], Y[a1]], [X[a4], Y[a4]]])
pts2 = np.float32([[0,0],[306,0],[306,306],[0,306]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(imgray,M,(306,306))
# cv2.drawContours(img, contours, max_i, (0,255,0), 3)
# cv2.namedWindow('image', cv2.WINDOW_NORMAL)
# cv2.resizeWindow('image', 600, 600)
cv2.imshow('image', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
# digits = np.zeroes(9, 9)
# for i in range(0, 273, 34):
# for j in range(0, 273, 34):
# digits[i][j] = digit_recognizer(dst[i+3:i+32, j+3:j+32])