質問編集履歴

1

全体のソースコードを追加しました。

2021/10/14 06:32

投稿

Kota1
Kota1

スコア15

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
 
4
4
 
5
- 画像を入力としていて、「画像の名前.txt」というファイルに実行結果を保存したいです。
5
+ 画像を入力としていて、「画像の名前.txt」というファイルに実行結果を保存したいです。保存先はカレントディレクトリです。
6
+
7
+ 複数の画像を入力としたいため、.txtファイルの区別をしたいです。
6
8
 
7
9
 
8
10
 
@@ -14,6 +16,434 @@
14
16
 
15
17
 
16
18
 
19
+ ####全体のソースコード
20
+
21
+ ```Python
22
+
23
+ """Utils for monoDepth.
24
+
25
+ """
26
+
27
+ import sys
28
+
29
+ import re
30
+
31
+ import numpy as np
32
+
33
+ import cv2
34
+
35
+ import torch
36
+
37
+
38
+
39
+
40
+
41
+ def read_pfm(path):
42
+
43
+ """Read pfm file.
44
+
45
+
46
+
47
+ Args:
48
+
49
+ path (str): path to file
50
+
51
+
52
+
53
+ Returns:
54
+
55
+ tuple: (data, scale)
56
+
57
+ """
58
+
59
+ with open(path, "rb") as file:
60
+
61
+
62
+
63
+ color = None
64
+
65
+ width = None
66
+
67
+ height = None
68
+
69
+ scale = None
70
+
71
+ endian = None
72
+
73
+
74
+
75
+ header = file.readline().rstrip()
76
+
77
+ if header.decode("ascii") == "PF":
78
+
79
+ color = True
80
+
81
+ elif header.decode("ascii") == "Pf":
82
+
83
+ color = False
84
+
85
+ else:
86
+
87
+ raise Exception("Not a PFM file: " + path)
88
+
89
+
90
+
91
+ dim_match = re.match(r"^(\d+)\s(\d+)\s$", file.readline().decode("ascii"))
92
+
93
+ if dim_match:
94
+
95
+ width, height = list(map(int, dim_match.groups()))
96
+
97
+ else:
98
+
99
+ raise Exception("Malformed PFM header.")
100
+
101
+
102
+
103
+ scale = float(file.readline().decode("ascii").rstrip())
104
+
105
+ if scale < 0:
106
+
107
+ # little-endian
108
+
109
+ endian = "<"
110
+
111
+ scale = -scale
112
+
113
+ else:
114
+
115
+ # big-endian
116
+
117
+ endian = ">"
118
+
119
+
120
+
121
+ data = np.fromfile(file, endian + "f")
122
+
123
+ shape = (height, width, 3) if color else (height, width)
124
+
125
+
126
+
127
+ data = np.reshape(data, shape)
128
+
129
+ data = np.flipud(data)
130
+
131
+
132
+
133
+ return data, scale
134
+
135
+
136
+
137
+
138
+
139
+ def write_pfm(path, image, scale=1):
140
+
141
+ """Write pfm file.
142
+
143
+
144
+
145
+ Args:
146
+
147
+ path (str): pathto file
148
+
149
+ image (array): data
150
+
151
+ scale (int, optional): Scale. Defaults to 1.
152
+
153
+ """
154
+
155
+
156
+
157
+ with open(path, "wb") as file:
158
+
159
+ color = None
160
+
161
+
162
+
163
+ if image.dtype.name != "float32":
164
+
165
+ raise Exception("Image dtype must be float32.")
166
+
167
+
168
+
169
+ image = np.flipud(image)
170
+
171
+
172
+
173
+ if len(image.shape) == 3 and image.shape[2] == 3: # color image
174
+
175
+ color = True
176
+
177
+ elif (
178
+
179
+ len(image.shape) == 2 or len(image.shape) == 3 and image.shape[2] == 1
180
+
181
+ ): # greyscale
182
+
183
+ color = False
184
+
185
+ else:
186
+
187
+ raise Exception("Image must have H x W x 3, H x W x 1 or H x W dimensions.")
188
+
189
+
190
+
191
+ file.write("PF\n" if color else "Pf\n".encode())
192
+
193
+ file.write("%d %d\n".encode() % (image.shape[1], image.shape[0]))
194
+
195
+
196
+
197
+ endian = image.dtype.byteorder
198
+
199
+
200
+
201
+ if endian == "<" or endian == "=" and sys.byteorder == "little":
202
+
203
+ scale = -scale
204
+
205
+
206
+
207
+ file.write("%f\n".encode() % scale)
208
+
209
+
210
+
211
+ image.tofile(file)
212
+
213
+
214
+
215
+
216
+
217
+ def read_image(path):
218
+
219
+ """Read image and output RGB image (0-1).
220
+
221
+
222
+
223
+ Args:
224
+
225
+ path (str): path to file
226
+
227
+
228
+
229
+ Returns:
230
+
231
+ array: RGB image (0-1)
232
+
233
+ """
234
+
235
+ img = cv2.imread(path)
236
+
237
+
238
+
239
+ if img.ndim == 2:
240
+
241
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
242
+
243
+
244
+
245
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) / 255.0
246
+
247
+
248
+
249
+ return img
250
+
251
+
252
+
253
+
254
+
255
+ def resize_image(img):
256
+
257
+ """Resize image and make it fit for network.
258
+
259
+
260
+
261
+ Args:
262
+
263
+ img (array): image
264
+
265
+
266
+
267
+ Returns:
268
+
269
+ tensor: data ready for network
270
+
271
+ """
272
+
273
+ height_orig = img.shape[0]
274
+
275
+ width_orig = img.shape[1]
276
+
277
+
278
+
279
+ if width_orig > height_orig:
280
+
281
+ scale = width_orig / 384
282
+
283
+ else:
284
+
285
+ scale = height_orig / 384
286
+
287
+
288
+
289
+ height = (np.ceil(height_orig / scale / 32) * 32).astype(int)
290
+
291
+ width = (np.ceil(width_orig / scale / 32) * 32).astype(int)
292
+
293
+
294
+
295
+ img_resized = cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA)
296
+
297
+
298
+
299
+ img_resized = (
300
+
301
+ torch.from_numpy(np.transpose(img_resized, (2, 0, 1))).contiguous().float()
302
+
303
+ )
304
+
305
+ img_resized = img_resized.unsqueeze(0)
306
+
307
+
308
+
309
+ return img_resized
310
+
311
+
312
+
313
+
314
+
315
+ def resize_depth(depth, width, height):
316
+
317
+ """Resize depth map and bring to CPU (numpy).
318
+
319
+
320
+
321
+ Args:
322
+
323
+ depth (tensor): depth
324
+
325
+ width (int): image width
326
+
327
+ height (int): image height
328
+
329
+
330
+
331
+ Returns:
332
+
333
+ array: processed depth
334
+
335
+ """
336
+
337
+ depth = torch.squeeze(depth[0, :, :, :]).to("cpu")
338
+
339
+
340
+
341
+ depth_resized = cv2.resize(
342
+
343
+ depth.numpy(), (width, height), interpolation=cv2.INTER_CUBIC
344
+
345
+ )
346
+
347
+
348
+
349
+ return depth_resized
350
+
351
+
352
+
353
+ def write_depth(path, depth, bits=1 , colored=False):
354
+
355
+ """Write depth map to pfm and png file.
356
+
357
+
358
+
359
+ Args:
360
+
361
+ path (str): filepath without extension
362
+
363
+ depth (array): depth
364
+
365
+ """
366
+
367
+ # write_pfm(path + ".pfm", depth.astype(np.float32))
368
+
369
+ if colored == True:
370
+
371
+ bits = 1
372
+
373
+
374
+
375
+ depth_min = depth.min()
376
+
377
+ depth_max = depth.max()
378
+
379
+
380
+
381
+ max_val = (2**(8*bits))-1 #8bit演算で計算するときの最大値
382
+
383
+ # if depth_max>max_val:
384
+
385
+ # print('Warning: Depth being clipped')
386
+
387
+ #
388
+
389
+ # if depth_max - depth_min > np.finfo("float").eps:
390
+
391
+ # out = depth
392
+
393
+ # out [depth > max_val] = max_val
394
+
395
+ # else:
396
+
397
+ # out = 0
398
+
399
+
400
+
401
+ #最大値最小値
402
+
403
+ print(str(depth_min)+","+str(depth_max))
404
+
405
+ with open('min,max.txt', 'w') as f:
406
+
407
+ print(str(depth_min)+","+str(depth_max), file=f)
408
+
409
+ cv2.imwrite(path+'.txt')
410
+
411
+
412
+
413
+ if depth_max - depth_min > np.finfo("float").eps:
414
+
415
+ out = max_val * (depth - depth_min) / (depth_max - depth_min)
416
+
417
+ else:
418
+
419
+ out = 0
420
+
421
+
422
+
423
+ if bits == 1 or colored:
424
+
425
+ out = out.astype("uint8")
426
+
427
+ if colored:
428
+
429
+ out = cv2.applyColorMap(out,cv2.COLORMAP_INFERNO)
430
+
431
+ cv2.imwrite(path+'.png', out)
432
+
433
+ elif bits == 2:
434
+
435
+ cv2.imwrite(path+'.png', out.astype("uint16"))
436
+
437
+
438
+
439
+ return
440
+
441
+
442
+
443
+
444
+
445
+ ```
446
+
17
447
  ### 該当のソースコード
18
448
 
19
449