-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineReader.py
More file actions
158 lines (131 loc) · 4.58 KB
/
Copy pathLineReader.py
File metadata and controls
158 lines (131 loc) · 4.58 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###################################################
# by Maciej Kamiński Politechnika Wrocławska
# Under GPL 3 and MIT licence
#
__author__ = 'Maciej Kamiński Politechnika Wrocławska'
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import argparse
import numpy as np
parser=argparse.ArgumentParser(description="Helps determine point coordinates in scanned chart")
parser.add_argument('-xfs','--x_figure_size', type=int, help='Window width in inches',default=12)
parser.add_argument('-yfs','--y_figure_size', type=int, help='Window height in inches',default=8)
parser.add_argument('maxx', type=int, help='Max X point - (0,X) coordinated')
parser.add_argument('maxy', type=int, help='Max Y point - (Y,0) coordinated')
parser.add_argument('input', type=str, help='Input image filename')
parser.add_argument('output', type=str, help='Output CSV filename')
args=parser.parse_args()
def onclick(event):
global point_zero
global point_xmax
global point_ymax
global points
if not point_zero:
event.canvas.figure.suptitle("Select X max Point", fontsize=14, fontweight='bold')
point_zero=(event.xdata, event.ydata)
event.canvas.draw()
elif not point_xmax:
event.canvas.figure.suptitle("Select Y max Point", fontsize=14, fontweight='bold')
point_xmax=(event.xdata, event.ydata)
event.canvas.draw()
elif not point_ymax:
event.canvas.figure.suptitle("Select First Point", fontsize=14, fontweight='bold')
point_ymax=(event.xdata, event.ydata)
event.canvas.draw()
else:
event.canvas.figure.suptitle("Select Next Point", fontsize=14, fontweight='bold')
points.append((event.xdata, event.ydata))
event.canvas.draw()
with open(args.output,"a") as file:
zero=complex(*point_zero)
xmax=complex(*point_xmax)
ymax=complex(*point_ymax)
point=complex(*points[-1])
x=xmax-zero
y=ymax-zero
p=point-zero
xversor=x/args.maxx
yversor=y/args.maxy
a=np.array([
[xversor.real,yversor.real],
[xversor.imag,yversor.imag]
])
b=np.array([p.real,p.imag])
h=np.linalg.solve(a,b)
print(h)
print("{:.2f}, {:.2f}".format(round(h[0],2),round(h[1],2)),file=file)
def onZoom(event):
# get the current x and y limits
base_scale=1.8
global ax
cur_xlim = ax.get_xlim()
cur_ylim = ax.get_ylim()
# get middle
xdata = event.xdata # get event x location
ydata = event.ydata # get event y location
x_l=xdata-cur_xlim[0]
x_r=cur_xlim[1]-xdata
y_d=ydata-cur_ylim[0]
y_u=cur_ylim[1]-ydata
if event.button == 'up':
# deal with zoom in
scale_factor = 1/base_scale
elif event.button == 'down':
# deal with zoom out
scale_factor = base_scale
else:
# deal with something that should never happen
scale_factor = 1
print(event.button)
# set new limits
x_l*=scale_factor
x_r*=scale_factor
y_d*=scale_factor
y_u*=scale_factor
new_min_x_limit=xdata - x_l
new_max_x_limit=xdata + x_r
new_min_y_limit=ydata - y_d
new_max_y_limit=ydata + y_u
if x_l+x_r>1 or y_u+y_d>1:
new_min_x_limit=0
new_max_x_limit=1
new_min_y_limit=0
new_max_y_limit=1
if new_min_x_limit<=0:
new_max_x_limit+=(0-new_min_x_limit)
new_min_x_limit=0
if new_max_x_limit>=1:
new_min_x_limit-=(new_max_x_limit-1)
new_max_x_limit=1
if new_min_y_limit<=0:
new_max_y_limit+=(0-new_min_y_limit)
new_min_y_limit=0
if new_max_y_limit>=1:
new_min_y_limit-=(new_max_y_limit-1)
new_max_y_limit=1
ax.set_xlim([new_min_x_limit,new_max_x_limit])
ax.set_ylim([new_min_y_limit,new_max_y_limit])
plt.draw() # force re-draw
def main():
global point_zero
global point_xmax
global point_ymax
global points
global ax
point_zero=None
point_xmax=None
point_ymax=None
points=[]
image=mpimg.imread(args.input)
fig, ax = plt.subplots(figsize=(args.x_figure_size,args.y_figure_size))
fig.suptitle("Select zero point", fontsize=14, fontweight='bold')
fig.canvas.mpl_connect('button_press_event', onclick)
fig.canvas.mpl_connect('scroll_event',onZoom)
plt.imshow(image,zorder=0,extent=[0.0,1.0,0.0,1.0],aspect='auto')
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
plt.tight_layout(pad=0)
plt.show()
main()