-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraw2jpg.py
executable file
·161 lines (116 loc) · 5.25 KB
/
raw2jpg.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
import numpy as np
import cv2
import glob
import os
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
raw_shape = (720, 1280)
debug_show = 1
debug_show_slide = 0
pattern_size = (9, 6)
im_h = 640
im_w = 360
def quantization(matin):
# quantized uint8
min_z = float(np.min(matin))
max_z = float(np.max(matin))
return np.flipud(((matin - min_z) / (max_z-min_z) * 255).astype(np.uint8).transpose())
def imread_from_raw_left(raw_file):
# read raw data
raw_shape_left = raw_shape + (2,)
raw = np.fromfile(raw_file, dtype=np.uint8).reshape(raw_shape_left).astype(np.uint16)
# merge channels
gray_short = np.bitwise_or(np.bitwise_and(np.left_shift(raw[:,:,0], 8), 0xff00),
np.right_shift(np.bitwise_and(raw[:,:,1], 0x00f0), 4))
return quantization(gray_short)
def imread_from_raw_right(raw_file):
raw = np.fromfile(raw_file, dtype=np.uint16).reshape(raw_shape)
return quantization(raw)
def merge_binocular_data(indir, outdir):
indir0_pattern = os.path.join(indir, "image0", "*.raw")
if not os.path.exists(outdir):
os.makedirs(outdir)
for filename0 in glob.glob(indir0_pattern):
print(filename0)
basename = os.path.basename(filename0)
filename1 = os.path.join(indir, "image1", basename)
if os.path.exists(filename1):
left = imread_from_raw_left(filename0)
right = imread_from_raw_right(filename1)
merged = np.hstack((left, right))
if debug_show:
mshape = merged.shape
# for easy show
resized = cv2.resize(merged, (int(mshape[0]/2), int(mshape[1]/2)))
cv2.imshow("merged", resized)
cv2.waitKey(debug_show_slide)
outfile = os.path.join(outdir, basename+".png")
cv2.imwrite(outfile, merged)
def save_mono_cheese(indir, outdir, sign):
if sign: #left
indir_pattern = os.listdir(os.path.join(indir, 'image0'))#os.path.join(indir, "image0", "*.raw")
sub_dir = os.path.join(indir, 'image0')
else:
indir_pattern = os.listdir(os.path.join(indir, "image1"))
sub_dir = os.path.join(indir, 'image1')
indir_pattern = [os.path.join(sub_dir, fn) for fn in indir_pattern]
#print(type(indir_pattern))
indir_pattern = sorted(indir_pattern)
sub_dir = 'image0' if sign else 'image1'
savepath = os.path.join(outdir, sub_dir)
if not os.path.exists(savepath):
os.makedirs(savepath)
for filename in indir_pattern:
basename = os.path.basename(filename)
print('basename:', basename)
if sign:
img = imread_from_raw_left(filename)
else:
img = imread_from_raw_right(filename)
img_small = cv2.resize(img, (im_w, im_h))
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(img_small, pattern_size, None)
if ret:
corners2 = cv2.cornerSubPix(img_small, corners, (11, 11), (-1, -1), criteria)
# Draw and display the corners
# Show the image to see if pattern is found ! imshow function.
frame_point = cv2.drawChessboardCorners(img_small.copy(), pattern_size, corners2, ret)
cv2.imshow('frame', frame_point)
if cv2.waitKey(0) & 0xFF == ord('c'):
print('sfdasF:', os.path.join(savepath, basename))
cv2.imwrite(os.path.join(savepath, basename.replace('.raw', '.jpg')), img)
def trans_sample(indir, outdir):
indir0_pattern = os.path.join(indir, "image0", "*.raw")
out_dir0 = os.path.join(outdir, 'image0')
out_dir1 = os.path.join(outdir, 'image1')
if not os.path.exists(out_dir0):
os.makedirs(out_dir0)
if not os.path.exists(out_dir1):
os.makedirs(out_dir1)
for filename0 in glob.glob(indir0_pattern):
print(filename0)
basename = os.path.basename(filename0)
filename1 = os.path.join(indir, "image1", basename)
if os.path.exists(filename1):
left = imread_from_raw_left(filename0)
right = imread_from_raw_right(filename1)
cv2.imshow("merged", cv2.resize(np.hstack((left, right)), (360, 640)))
if cv2.waitKey(0) & 0xFF == ord('c'):
cv2.imwrite(os.path.join(out_dir0, basename + '.jpg'), left)
cv2.imwrite(os.path.join(out_dir1, basename + '.jpg'), right)
def params():
import argparse
parser = argparse.ArgumentParser(description='RAW2JPG')
parser.add_argument('--source_dir', type=str, required=True)
parser.add_argument('--outdir', type=str, required=True)
parser.add_argument('--format', type=str, default='.jpg')
parser.add_argument('--mode', type=str, default='cb', choices=['cb', 'sample'])
args = parser.parse_args()
return args
if __name__ == "__main__":
args = params()
# #merge_binocular_data("./raw", "./merged")
if args.mode == 'cb':
save_mono_cheese(args.source_dir, args.outdir, 1)
save_mono_cheese(args.source_dir, args.outdir, 0)
else:
trans_sample(args.source_dir, args.outdir)