-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathapp5_stitching.py
257 lines (222 loc) · 8.68 KB
/
app5_stitching.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
from glob import glob
import numpy as np
import pandas as pd
from skimage import io
import dash
from dash.exceptions import PreventUpdate
from dash.dependencies import Input, Output, State
import dash_html_components as html
import dash_core_components as dcc
import dash_table
import dash_canvas
from dash_canvas.components import image_upload_zone
from dash_canvas.utils.io_utils import (image_string_to_PILImage,
array_to_data_url)
from dash_canvas.utils.registration import register_tiles
from dash_canvas.utils.parse_json import parse_jsonstring_line
def title():
return "Image stitching"
def description():
return """Automatic or semi-supervized stitching of images acquired
with some overlap"""
def tile_images(list_of_images, n_rows, n_cols):
dtype = list_of_images[0].dtype
if len(list_of_images) < n_rows * n_cols:
white = np.zeros(list_of_images[0].shape, dtype=dtype)
n_missing = n_rows * n_cols - len(list_of_images)
list_of_images += [white, ] * n_missing
return np.vstack([np.hstack(list_of_images[i_row*n_cols:
i_row*n_cols + n_cols])
for i_row in range(n_rows)])
def untile_images(image_string, n_rows, n_cols):
big_im = np.asarray(image_string_to_PILImage(image_string))
tiles = [np.split(im, n_cols, axis=1) for im in np.split(big_im, n_rows)]
return np.array(tiles)
def instructions():
return html.Div(children=[
html.H5(children='How to use this stitching app'),
dcc.Markdown("""
- Choose the number of rows and columns of the mosaic,
- Upload images.
- Try automatic stitching by pressing
the "Run stitching" button.
- If automatic stitching did not work,
try adjusting the overlap parameter.
If shifts between different images are very diifferent,
draw lines to match points of interest in pairs of
images, then press "Estimate translation" to compute an
estimate of the shifts, then press "Run stitching".
""")
])
app = dash.Dash(__name__)
server = app.server
app.config.suppress_callback_exceptions = False
height, width = 200, 500
canvas_width = 800
canvas_height = round(height * canvas_width / width)
scale = canvas_width / width
list_columns = ['length', 'width', 'height']
columns = [{"name": i, "id": i} for i in list_columns]
layout = html.Div([
html.Div([
dcc.Tabs(
id='stitching-tabs',
value='canvas-tab',
children=[
dcc.Tab(
label='Image tiles',
value='canvas-tab',
children=[
dash_canvas.DashCanvas(
id='canvas-stitch',
width=canvas_width,
height=canvas_height,
scale=scale,
lineWidth=2,
lineColor='red',
tool="line",
image_content=array_to_data_url(
np.zeros((width, width), dtype=np.uint8)),
goButtonTitle='Estimate translation',
),
html.Button('Upload demo data', id='demo'),
image_upload_zone('upload-stitch', multiple=True,
width=45),
html.Div(id='sh_x', hidden=True),
]
),
dcc.Tab(
label='Stitched Image',
value='result-tab',
children=[
html.Img(id='stitching-result',
src=array_to_data_url(
np.zeros((height, width), dtype=np.uint8)),
width=canvas_width)
]
)
]
)
], className="eight columns"),
html.Div([
html.Label('Number of rows'),
dcc.Input(
id='nrows-stitch',
type='number',
value=2,
name='number of rows',
),
html.Label('Number of columns'),
dcc.Input(
id='ncolumns-stitch',
type='number',
value=4,
name='number of columns',
),
html.Label('Fraction of overlap (in [0-1] range)'),
dcc.Input(
id='overlap-stitch',
type='float',
value=0.15,
),
html.Label('Measured shifts between images'),
dash_table.DataTable(
id='table-stitch',
columns=columns,
editable=True,
),
html.Br(),
html.Button('Run stitching', id='button-stitch',
style={'color':'red'}),
html.Br(),
instructions()
], className="three columns"),
])
def callbacks(app):
@app.callback(Output('table-stitch', 'data'),
[Input('canvas-stitch', 'json_data')])
def estimate_translation(string):
props = parse_jsonstring_line(string)
df = pd.DataFrame(props, columns=list_columns)
return df.to_dict("records")
@app.callback(Output('sh_x', 'children'),
[Input('upload-stitch', 'contents'),
Input('upload-stitch', 'filename'),
Input('demo', 'n_clicks')],
[State('nrows-stitch', 'value'),
State('ncolumns-stitch', 'value')])
def upload_content(list_image_string, list_filenames, click,
n_rows, n_cols):
#if list_image_string is None:
# raise PreventUpdate
if list_image_string is not None:
print('update canvas upload')
order = np.argsort(list_filenames)
image_list = [np.asarray(image_string_to_PILImage(
list_image_string[i])) for i in order]
res = tile_images(image_list, n_rows, n_cols)
return array_to_data_url(res)
elif click:
filelist = glob('./assets/tile*.jpg')
filelist.sort()
print(filelist)
image_list = [io.imread(filename) for filename in filelist]
res = tile_images(image_list, n_rows, n_cols)
return array_to_data_url(res)
else:
raise PreventUpdate
#return None
@app.callback(Output('stitching-tabs', 'value'),
[Input('button-stitch', 'n_clicks')])
def change_focus(click):
print('changing focus')
if click:
return 'result-tab'
return 'canvas-tab'
@app.callback(Output('stitching-result', 'src'),
[Input('button-stitch', 'n_clicks')],
[State('nrows-stitch', 'value'),
State('ncolumns-stitch', 'value'),
State('overlap-stitch', 'value'),
State('table-stitch', 'data'),
State('sh_x', 'children')])
def modify_content(n_cl, n_rows, n_cols, overlap, estimate, image_string):
tiles = untile_images(image_string, n_rows, n_cols)
if estimate is not None and len(estimate) > 0:
overlap = []
for line in estimate:
overlap.append(1.1 * line['length'] / tiles.shape[3])
canvas = register_tiles(tiles, n_rows, n_cols,
overlaps=overlap,
pad=100)
return array_to_data_url(canvas)
@app.callback(Output('canvas-stitch', 'image_content'),
[Input('sh_x', 'children')])
def update_canvas_image(im):
print('update image content')
return im
@app.callback(Output('canvas-stitch', 'height'),
[Input('sh_x', 'children')],
[State('canvas-stitch', 'width'),
State('canvas-stitch', 'height')])
def update_canvas_upload_shape(image_string, w, h):
if image_string is None:
raise PreventUpdate
if image_string is not None:
im = image_string_to_PILImage(image_string)
im_h, im_w = im.height, im.width
return round(w / im_w * im_h)
else:
return canvas_height
@app.callback(Output('canvas-stitch', 'scale'),
[Input('sh_x', 'children')])
def update_canvas_upload_scale(image_string):
if image_string is None:
raise PreventUpdate
if image_string is not None:
# very dirty hack, this should be made more robust using regexp
im = image_string_to_PILImage(image_string)
im_h, im_w = im.height, im.width
return canvas_width / im_w
else:
return scale