-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaxisANDcorner.py
More file actions
184 lines (141 loc) · 5.52 KB
/
axisANDcorner.py
File metadata and controls
184 lines (141 loc) · 5.52 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
Program to function to detect the axes of the graph and
remove the plot area from the axes for doing OCR.
@author: Pratham Gupta
"""
import cv2
import numpy as np
def detectAxesAndLengthScales(load):
loadcopy = load
down_width = 720
down_height = int(loadcopy.shape[0]/loadcopy.shape[1]*720)
down_points = (down_width, down_height)
image = cv2.resize(load, down_points, interpolation= cv2.INTER_LINEAR)
imagecopy = cv2.resize(loadcopy, down_points, interpolation= cv2.INTER_LINEAR)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
dst = cv2.Canny(gray,400,500)
# Threshold for an optimal value, it may vary depending on the image.
image[dst>0.012*dst.max()]=[255,255,255]
offset = cv2.cvtColor(image-imagecopy, cv2.COLOR_BGR2GRAY)
column_sum=[]
for i in range(down_width):
column_index = i
column_sum.append(np.sum(offset[:, column_index]))
i+=1
lvline_index =column_sum.index(max(column_sum))
loopline2_index=lvline_index
while abs(loopline2_index-lvline_index)<10:
column_sum.pop(loopline2_index)
loopline2_index = column_sum.index(max(column_sum))
columntestline_index = loopline2_index
if columntestline_index < lvline_index:
vline_index = columntestline_index
else:
vline_index=columntestline_index+1
row_sum=[]
for i in range(down_height):
row_index = i
row_sum.append(np.sum(offset[row_index,:]))
i+=1
bhline_index =row_sum.index(max(row_sum))
loopline_index=bhline_index
while abs(loopline_index-bhline_index)<10:
row_sum.pop(loopline_index)
loopline_index = row_sum.index(max(row_sum))
rowtestline_index = loopline_index
if rowtestline_index < bhline_index:
hline_index = rowtestline_index
else:
hline_index=rowtestline_index+1
a = lvline_index
b = hline_index # nz[:,0,1].min()
c = vline_index #nz[:,0,0].max()
d = bhline_index
if b>d:
switch = b
b = d
d = switch
if c>a:
switch = c
c = a
a = switch
offset[b,c:a] =128
offset[b:d,a] =128
offset[b:d,c] =128
offset[d,c:a] =128
imagecopy[b,c:a] =(0,255,0)
imagecopy[d,c:a] =(0,255,0)
imagecopy[b:d,a] =(0,255,0)
imagecopy[b:d,c] =(0,255,0)
graph = imagecopy[b-3:d+3,c-3:a+3]
nongraph = imagecopy
nongraph[b-3:d-3,c+6:a+6] = 255
#The code upto here detects and makes a bounding box around the main graph area,
#returns back the indices of the lines (the x and y axis coordinates), and returns back the graph and nongraph area
graphcopy = graph
graygraph= cv2.cvtColor(graph, cv2.COLOR_BGR2GRAY)
graygraphcopy = cv2.cvtColor(graphcopy,cv2.COLOR_BGR2GRAY)
graygraph = np.float32(graygraph)
dst = cv2.cornerHarris(graygraph,2,3,0.05)
# Look into Shi-Tomasi corner detection
# Threshold for an optimal value, it may vary depending on the image.
graygraph[dst>0.06*dst.max()]=255
corneronly = graygraph-graygraphcopy
corneronly[:-9,9:]=0
(var1,var2) = np.shape(corneronly)
indexhighlow= []
indexlowhigh= []
for i in range(var2-1):
for j in range(var1-1):
if corneronly[j,i] > 128:
if corneronly[j+1,i] < 128:
indexhighlow.append((j,i))
elif corneronly[j,i] < 128:
if corneronly[j+1,i] > 128:
indexlowhigh.append((j+1,i))
second_element_count = {}
for tup in indexlowhigh:
second_element = tup[1]
if second_element in second_element_count:
second_element_count[second_element] += 1
else:
second_element_count[second_element] = 1
max_occurrence = max(second_element_count.values())
max_occurrence_elements = [k for k, v in second_element_count.items() if v == max_occurrence]
maximum = max_occurrence_elements[0]
filtered_array = [tup for tup in indexlowhigh if tup[1] == maximum]
differences = []
for i in range(1, len(filtered_array)):
difference = filtered_array[i][0] - filtered_array[i - 1][0]
differences.append(difference)
def positive(arr):
return[x for x in arr if x>5]
differences=positive(differences)
lengthscaley = np.ceil(np.mean(differences))
for i in range(var2-1):
for j in range(var1-1):
if corneronly[j,i] > 128:
if corneronly[j,i+1] < 128:
indexhighlow.append((j,i))
elif corneronly[j,i] < 128:
if corneronly[j,i+1] > 128:
indexlowhigh.append((j,i+1))
first_element_count = {}
for tup in indexlowhigh:
first_element = tup[0]
if first_element in first_element_count:
first_element_count[first_element] += 1
else:
first_element_count[first_element] = 1
max_occurrence = max(first_element_count.values())
max_occurrence_elements = [k for k, v in first_element_count.items() if v == max_occurrence]
maximum = max_occurrence_elements[0]
filtered_array = [tup for tup in indexlowhigh if tup[0] == maximum]
differences = []
for i in range(1, len(filtered_array)):
difference = filtered_array[i][1] - filtered_array[i - 1][1]
differences.append(difference)
differences=positive(differences)
lengthscalex = np.ceil(np.mean(differences))
return(graph,nongraph,b,d,c,a,lengthscaley,lengthscalex)
#lengthscalex and lengthscaley are the calculated mean differences on the axes, a measure for the distance between two points