-
Notifications
You must be signed in to change notification settings - Fork 1
/
getcellval.txt
137 lines (113 loc) · 4.82 KB
/
getcellval.txt
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
"""
**************************************************************************
* IMAGE PROCESSING (e-Yantra 2016)
* ================================
* This software is intended to teach image processing concepts
*
* MODULE: Task1C
* Filename: getCellVal.py
* Version: 1.0.0
* Date: October 13, 2016
*
* Author: Jayant Solanki, e-Yantra Project, Department of Computer Science
* and Engineering, Indian Institute of Technology Bombay.
*
* Software released under Creative Commons CC BY-NC-SA
*
* For legal information refer to:
* http://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
*
*
* This software is made available on an “AS IS WHERE IS BASIS”.
* Licensee/end user indemnifies and will keep e-Yantra indemnified from
* any and all claim(s) that emanate from the use of the Software or
* breach of the terms of this agreement.
*
* e-Yantra - An MHRD project under National Mission on Education using
* ICT(NMEICT)
*
**************************************************************************
"""
# detectCellVal detects the numbers/operatorsm,
# perform respective expression evaluation
# and stores them into the grid_map
# detectCellVal(img,grid_map)
# Find the number/operators, perform the calculations and store the result into the grid_map
# Return the resultant grid_map
import cv2
import numpy as np
# team_id- eYRC#4227-CC
def detectCellVal(img_rgb,grid_map):
# your code here
grid_line_x = 7
grid_line_y = 7
grid_map = [[0 for j in range(0, grid_line_x-1)]for i in range(0, grid_line_y-1)]
# grid_map is a 2D list to store the values that are read from given image
def assign_grid_map(num_template, num):
'''
assign_grid_map() is a function to store values in the matrix
:param num_template: is the image template which is to be compared in the given problem image
:param num: is the value which corresponds to the template passed
'''
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
result = cv2.matchTemplate(img_gray, num_template, cv2.TM_CCOEFF_NORMED)
if num == 8 or num == 3:
threshold = 0.57
else:
threshold = 0.52
locations = np.where(result >= threshold)
# locations is for storing the locations where templates are matched
if num == 10:
h = '-'
# template[10] corresponds to '-'
elif num == 11:
h = '+'
# template[11] corresponds to '+'
else:
h = num
# template[i] corresponds to a value i except for i=10 and i=11
for points in zip(*locations[::-1]):
if points[0] % 100 != 0 or points[1] % 100 != 0:
'''
points[0] stores the x coordinate
points[1] stores the y coordinate
if either of x or y coordinate of the point is not a multiple of 100
then the point is not required
so we skip this case by using 'continue'
'''
continue
else:
x = int(points[0]/100)
y = int(points[1]/100)
grid_map[y][x] = h
'''
if both the x and y coordinates are multiple of 100
then the template matching is successful and the value is assigned to the corresponding matrix element
here x is the column number and y is the row number
'''
# loop to solve the matrix grid_map thus formed
for i in range(0, 6, 1):
temp = grid_map[i][0]
for j in range(1, 6, 2):
if j == 5:
grid_map[i][5] = temp
elif grid_map[i][j] == '-':
temp -= grid_map[i][j+1]
elif grid_map[i][j] == '+':
temp += grid_map[i][j + 1]
template = [cv2.imread('digits/0.jpg', 0), cv2.imread('digits/1.jpg', 0), cv2.imread('digits/2.jpg', 0),
cv2.imread('digits/3.jpg', 0), cv2.imread('digits/4.jpg', 0), cv2.imread('digits/5.jpg', 0),
cv2.imread('digits/6.jpg', 0), cv2.imread('digits/7.jpg', 0), cv2.imread('digits/8.jpg', 0),
cv2.imread('digits/9.jpg', 0), cv2.imread('digits/minus.jpg', 0), cv2.imread('digits/plus.jpg', 0)]
# template is list which stores the given templates from the digits folder
for i in range(0, len(template)):
assign_grid_map(template[i], i)
'''
every template is passed to the function assign_grid_map()
the number in template[i] corresponds to a value of i
for example template[1] stores 1.jpg and hence corresponds to value 1
but template[10] corresponds to '-'
and template[11] corresponds to '+'
'''
return grid_map