forked from pratistha-gupta/Data-Extraction-From-Graphs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.py
More file actions
138 lines (121 loc) · 4.05 KB
/
1.py
File metadata and controls
138 lines (121 loc) · 4.05 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
import cv2
import numpy as np
import csv
import os
arr = os.listdir("graphs") #list the directory files
print("Please select the file you want to print x,y co-ordinates")
for i, filename in enumerate(arr):
print(str(i+1)+". "+filename)
file_index=int(input())
selected_file_name= arr[file_index-1]
print(arr[file_index-1]+" selected")
clicks = [['x', 'y']]
originC = []
XC = []
YC = []
origin = False
plots = False
axisX = False
axisY = False
def draw_circle(event,x,y,flags,param):
#BGR
if event == cv2.EVENT_LBUTTONDOWN:
if origin == True:
cv2.circle(img,(x,y),5,(0,255,0),2)
originC.extend((x, y))
print("origin:",originC)
elif axisX == True:
cv2.circle(img,(x,y),5,(255,0,0),2)
XC.extend((x, y))
print("X:",XC)
print("Enter the length of x-axis")
xl = float(input())
print("Enter the zero offset for x axis")
global x_zero_offset
x_zero_offset = float(input())
global x_PixLength
x_PixLength = float(euclidean(originC[0], originC[1], XC[0], XC[1]))
global x_pixToUnitRatio
x_pixToUnitRatio = float(x_PixLength)/float(xl)
global cosine
cosine = (XC[0]-originC[0])/x_PixLength
elif axisY == True:
cv2.circle(img,(x,y),5,(255,0,0),2)
YC.extend((x, y))
print("Y:",YC)
print("Enter the length of y-axis")
yl = float(input())
print("Enter the zero offset for y axis")
global y_zero_offset
y_zero_offset = float(input())
global y_PixLength
y_PixLength = float(euclidean(originC[0], originC[1], YC[0], YC[1]))
global y_pixToUnitRatio
y_pixToUnitRatio = float(y_PixLength)/float(yl)
elif plots == True:
cv2.circle(img,(x,y),5,(0,0,255),2)
clicks.append(coordinate_calculator(x, y))
#Distance formula
def euclidean(x1, y1, x2, y2):
return np.sqrt((x2-x1)*2 + (y2-y1)*2)
#Equation of Line y=mx+c
def x_axis(x):
return ((XC[1]-originC[1])/abs(XC[0]-originC[0]))*abs(x-originC[0]) + originC[1]
#Equation of Line x=my+c
def y_axis(y):
return ((YC[0]-originC[0])/abs(YC[1]-originC[1]))*abs(y-originC[1]) + originC[0]
#Convert pixels to unt
def x_pixToUnit(pixels):
return float(pixels)/float(x_pixToUnitRatio)
def y_pixToUnit(pixels):
return float(pixels)/float(y_pixToUnitRatio)
#calculate the co-ordinates
def coordinate_calculator(x, y):
eu_x = euclidean(x, y, y_axis(y), y)
y_unit = y_pixToUnit(euclidean(x, y, x, x_axis(x))*cosine)+y_zero_offset
x_unit = x_pixToUnit(euclidean(x, y, y_axis(y), y)*cosine)+x_zero_offset
return [x_unit, y_unit]
#print("Enter image name with extension")
img_file = selected_file_name #read image file
img = cv2.imread("graphs\\"+img_file)
cv2.namedWindow('image') #Show the imgage in a window named 'image'
cv2.setMouseCallback('image',draw_circle) #handle mouse click events and call draw_Circle function
#infinite loop to imshow the image and keep capturing the key press
while(1):
cv2.imshow('image',img)
k = cv2.waitKey(1) & 0xFF
if k == ord("o"):
origin = True
plots = False
axisX = False
axisY = False
elif k == ord("x"):
origin = False
plots = False
axisX = True
axisY = False
elif k == ord("y"):
origin = False
plots = False
axisX = False
axisY = True
elif k == ord("p"):
origin = False
plots = True
axisX = False
axisY = False
elif k == ord("s"):
cv2.imwrite("plots\\"+img_file+"_plot.png", img)
print("Plot image saved!")
elif k == ord("f"):
break
cv2.destroyAllWindows()
print("Enter the file name to which you need to save data")
F_name = input()
#Export the points in a csv
with open("plotDataFiles\\"+F_name+'.csv', 'w') as csvFile:
print(clicks)
writer = csv.writer(csvFile)
writer.writerows(clicks)
csvFile.close()
print("done!")