-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslRelocate_Images_toDataset.py
208 lines (168 loc) · 9.91 KB
/
translRelocate_Images_toDataset.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
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 26 14:54:02 2020
@author: anasa
"""
#importing modules
import matplotlib.pyplot as plt
import numpy as np
import cv2
import random
import os
def download_calibration_file(serial_number) :
if os.name == 'nt' :
hidden_path = 'C:/Users/anasa/Desktop/ZED_Detection/'#'D:/Downloads/'
calibration_file = hidden_path + 'SN' + str(serial_number) + '.conf'
else :
hidden_path = 'C:/Users/anasa/Desktop/ZED_Detection/'
calibration_file = hidden_path + 'SN' + str(serial_number) + '.conf'
if os.path.isfile(calibration_file) == False:
url = 'http://calib.stereolabs.com/?SN='
filename = wget.download(url=url+str(serial_number), out=calibration_file)
if os.path.isfile(calibration_file) == False:
print('Invalid Calibration File')
return ""
return calibration_file
def init_calibration(calibration_file, image_size) :
cameraMarix_left = cameraMatrix_right = map_left_y = map_left_x = map_right_y = map_right_x = np.array([])
config = configparser.ConfigParser()
config.read(calibration_file)
check_data = True
resolution_str = ''
if image_size.width == 2208 :
resolution_str = '2K'
elif image_size.width == 1920 :
resolution_str = 'FHD'
elif image_size.width == 1280 :
resolution_str = 'HD'
elif image_size.width == 672 :
resolution_str = 'VGA'
else:
resolution_str = 'HD'
check_data = False
T_ = np.array([-float(config['STEREO']['Baseline'] if 'Baseline' in config['STEREO'] else 0),
float(config['STEREO']['TY_'+resolution_str] if 'TY_'+resolution_str in config['STEREO'] else 0),
float(config['STEREO']['TZ_'+resolution_str] if 'TZ_'+resolution_str in config['STEREO'] else 0)])
left_cam_cx = float(config['LEFT_CAM_'+resolution_str]['cx'] if 'cx' in config['LEFT_CAM_'+resolution_str] else 0)
left_cam_cy = float(config['LEFT_CAM_'+resolution_str]['cy'] if 'cy' in config['LEFT_CAM_'+resolution_str] else 0)
left_cam_fx = float(config['LEFT_CAM_'+resolution_str]['fx'] if 'fx' in config['LEFT_CAM_'+resolution_str] else 0)
left_cam_fy = float(config['LEFT_CAM_'+resolution_str]['fy'] if 'fy' in config['LEFT_CAM_'+resolution_str] else 0)
left_cam_k1 = float(config['LEFT_CAM_'+resolution_str]['k1'] if 'k1' in config['LEFT_CAM_'+resolution_str] else 0)
left_cam_k2 = float(config['LEFT_CAM_'+resolution_str]['k2'] if 'k2' in config['LEFT_CAM_'+resolution_str] else 0)
left_cam_p1 = float(config['LEFT_CAM_'+resolution_str]['p1'] if 'p1' in config['LEFT_CAM_'+resolution_str] else 0)
left_cam_p2 = float(config['LEFT_CAM_'+resolution_str]['p2'] if 'p2' in config['LEFT_CAM_'+resolution_str] else 0)
left_cam_p3 = float(config['LEFT_CAM_'+resolution_str]['p3'] if 'p3' in config['LEFT_CAM_'+resolution_str] else 0)
left_cam_k3 = float(config['LEFT_CAM_'+resolution_str]['k3'] if 'k3' in config['LEFT_CAM_'+resolution_str] else 0)
right_cam_cx = float(config['RIGHT_CAM_'+resolution_str]['cx'] if 'cx' in config['RIGHT_CAM_'+resolution_str] else 0)
right_cam_cy = float(config['RIGHT_CAM_'+resolution_str]['cy'] if 'cy' in config['RIGHT_CAM_'+resolution_str] else 0)
right_cam_fx = float(config['RIGHT_CAM_'+resolution_str]['fx'] if 'fx' in config['RIGHT_CAM_'+resolution_str] else 0)
right_cam_fy = float(config['RIGHT_CAM_'+resolution_str]['fy'] if 'fy' in config['RIGHT_CAM_'+resolution_str] else 0)
right_cam_k1 = float(config['RIGHT_CAM_'+resolution_str]['k1'] if 'k1' in config['RIGHT_CAM_'+resolution_str] else 0)
right_cam_k2 = float(config['RIGHT_CAM_'+resolution_str]['k2'] if 'k2' in config['RIGHT_CAM_'+resolution_str] else 0)
right_cam_p1 = float(config['RIGHT_CAM_'+resolution_str]['p1'] if 'p1' in config['RIGHT_CAM_'+resolution_str] else 0)
right_cam_p2 = float(config['RIGHT_CAM_'+resolution_str]['p2'] if 'p2' in config['RIGHT_CAM_'+resolution_str] else 0)
right_cam_p3 = float(config['RIGHT_CAM_'+resolution_str]['p3'] if 'p3' in config['RIGHT_CAM_'+resolution_str] else 0)
right_cam_k3 = float(config['RIGHT_CAM_'+resolution_str]['k3'] if 'k3' in config['RIGHT_CAM_'+resolution_str] else 0)
R_zed = np.array([float(config['STEREO']['RX_'+resolution_str] if 'RX_' + resolution_str in config['STEREO'] else 0),
float(config['STEREO']['CV_'+resolution_str] if 'CV_' + resolution_str in config['STEREO'] else 0),
float(config['STEREO']['RZ_'+resolution_str] if 'RZ_' + resolution_str in config['STEREO'] else 0)])
R, _ = cv2.Rodrigues(R_zed)
cameraMatrix_left = np.array([[left_cam_fx, 0, left_cam_cx],
[0, left_cam_fy, left_cam_cy],
[0, 0, 1]])
cameraMatrix_right = np.array([[right_cam_fx, 0, right_cam_cx],
[0, right_cam_fy, right_cam_cy],
[0, 0, 1]])
distCoeffs_left = np.array([[left_cam_k1], [left_cam_k2], [left_cam_p1], [left_cam_p2], [left_cam_k3]])
distCoeffs_right = np.array([[right_cam_k1], [right_cam_k2], [right_cam_p1], [right_cam_p2], [right_cam_k3]])
T = np.array([[T_[0]], [T_[1]], [T_[2]]])
R1 = R2 = P1 = P2 = np.array([])
R1, R2, P1, P2 = cv2.stereoRectify(cameraMatrix1=cameraMatrix_left,
cameraMatrix2=cameraMatrix_right,
distCoeffs1=distCoeffs_left,
distCoeffs2=distCoeffs_right,
R=R, T=T,
flags=cv2.CALIB_ZERO_DISPARITY,
alpha=0,
imageSize=(image_size.width, image_size.height),
newImageSize=(image_size.width, image_size.height))[0:4]
map_left_x, map_left_y = cv2.initUndistortRectifyMap(cameraMatrix_left, distCoeffs_left, R1, P1, (image_size.width, image_size.height), cv2.CV_32FC1)
map_right_x, map_right_y = cv2.initUndistortRectifyMap(cameraMatrix_right, distCoeffs_right, R2, P2, (image_size.width, image_size.height), cv2.CV_32FC1)
cameraMatrix_left = P1
cameraMatrix_right = P2
return cameraMatrix_left, cameraMatrix_right, map_left_x, map_left_y, map_right_x, map_right_y
class Resolution:
width = 1920
height =1080
cap = cv2.VideoCapture(0)
if cap.isOpened() == 0:
exit(-1)
image_size = Resolution()
image_size.width = 1920
image_size.height = 1080
# Set the video resolution to HD720
cap.set(cv2.CAP_PROP_FRAME_WIDTH, image_size.width*2)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, image_size.height)
serial_number = 12970 # int(sys.argv[1])
calibration_file = download_calibration_file(serial_number)
if calibration_file == "":
exit(1)
print("Calibration file found. Loading...")
camera_matrix_left, camera_matrix_right, map_left_x, map_left_y, map_right_x, map_right_y = init_calibration(calibration_file, image_size)
red_lower=np.array([0,0,75],np.uint8)
def translateImg(fimage, fname):
image = cv2.imread(fimage)
height, width = image.shape[:2]
offset_height, offset_width = random.randint(-100,200),random.randint(-100,200) #random.randint(-(height-200)/2,(height-200)/2), random.randint(-(width-200)/2,(width-200)/2)
T = np.float32([[1, 0, offset_width], [0, 1, offset_height]])
image = cv2.resize(image, (int(800*0.7),int(800*0.7)))
img = cv2.warpAffine(image, T, (width, height)) # We use warpAffine to transform
# complete_image = masked_image + crop_background
cv2.imwrite('D:/test_images/' +"tstIm_0" +str(fname) + ".jpg", img)
#plt.imshow(complete_image)
def ret_path(img_dir):
img_paths = []
for filename in os.listdir(img_dir):
if os.path.splitext(filename)[1].lower() in ['.png', '.jpg', '.jpeg']:
img_paths.append(os.path.join(img_dir, filename))
return img_paths
def cropIm2size(fimage, fname):
image_size = Resolution()
image_size.width = 1920
image_size.height = 1080
# Set the video resolution to HD720
cap.set(cv2.CAP_PROP_FRAME_WIDTH, image_size.width*2)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, image_size.height)
serial_number = 12970 # int(sys.argv[1])
camera_matrix_left, camera_matrix_right, map_left_x, map_left_y, map_right_x, map_right_y = init_calibration(calibration_file, image_size)
red_lower=np.array([0,0,75],np.uint8)
red_upper=np.array([250,255,255],np.uint8)
height, width = fimage.shape[:2]
print (height, width)
crop_img = fimage[0:1080,500:1580] #fimage[0:1080,500:1570]
cv2.imwrite('D:/test_images/' +"tstIm_0" +str(fname) + ".jpg", crop_img)
retval, frame = cap.read()
frm=frm+1
start_t = time.time()
# Extract left and right images from side-by-side
left_right_image = np.split(frame, 2, axis=1)
#left_rect = cv2.remap(left_right_image[0], map_left_x, map_left_y, interpolation=cv2.INTER_LINEAR)
right_rect = cv2.remap(left_right_image[1], map_right_x, map_right_y, interpolation=cv2.INTER_LINEAR)
img=right_rect
img=img[0:1080,500:1570] #375:1815 Y=787 X=787
f_dir = 'C:/Users/anasa/Pictures/Camera Roll' #'C:/Users/anasa/Desktop/Datasets/papers'
fpath = ret_path(f_dir)
fname = 1
for image_path in fpath:
fimage=cv2.imread(image_path)
cropIm2size(fimage, fname)
fname+=1
# for i in range(4):
# translateImg(image, fname)
# fname+=1
# paper_dir = 'C:/Users/anasa/Desktop/Datasets/ForTrain/train/2_cartonCup'
# paper_path = ret_path(paper_dir)
# bot_dir = 'C:/Users/anasa/Desktop/Datasets/ForTrain/train/3_bottles'
# bot_path = ret_path(bot_dir)
# nyl_dir = 'C:/Users/anasa/Desktop/Datasets/ForTrain/train/4_nylon'
# nyl_path = ret_path(nyl_dir)