def generate_data(img_path):
data_re = []
label_re = []
random_list = []
img = cv2.resize(cv2.imread(img_path, 0), (320, 240))
i = 1
while i < pairs_per_img + 1:
data = []
label = []
y_start = random.randint(32, 80)
y_end = y_start + 128
x_start = random.randint(32, 160)
x_end = x_start + 128
y_1 = y_start
x_1 = x_start
y_2 = y_end
x_2 = x_start
y_3 = y_end
x_3 = x_end
y_4 = y_start
x_4 = x_end
img_patch = img[y_start:y_end, x_start:x_end] # patch 1
y_1_offset = random.randint(-32, 32)
x_1_offset = random.randint(-32, 32)
y_2_offset = random.randint(-32, 32)
x_2_offset = random.randint(-32, 32)
y_3_offset = random.randint(-32, 32)
x_3_offset = random.randint(-32, 32)
y_4_offset = random.randint(-32, 32)
x_4_offset = random.randint(-32, 32)
y_1_p = y_1 + y_1_offset
x_1_p = x_1 + x_1_offset
y_2_p = y_2 + y_2_offset
x_2_p = x_2 + x_2_offset
y_3_p = y_3 + y_3_offset
x_3_p = x_3 + x_3_offset
y_4_p = y_4 + y_4_offset
x_4_p = x_4 + x_4_offset
pts_img_patch = np.array([[y_1,x_1],[y_2,x_2],[y_3,x_3],[y_4,x_4]]).astype(np.float32)
pts_img_patch_perturb = np.array([[y_1_p,x_1_p],[y_2_p,x_2_p],[y_3_p,x_3_p],[y_4_p,x_4_p]]).astype(np.float32)
h,status = cv2.findHomography(pts_img_patch, pts_img_patch_perturb, cv2.RANSAC)
img_perburb = cv2.warpPerspective(img, h, (320, 240))
img_perburb_patch = img_perburb[y_start:y_end, x_start:x_end] # patch 2
if not [y_1,x_1,y_2,x_2,y_3,x_3,y_4,x_4] in random_list:
data.append(img_patch)
data.append(img_perburb_patch) # [2, 128, 128]
random_list.append([y_1,x_1,y_2,x_2,y_3,x_3,y_4,x_4])
h_4pt = np.array([y_1_offset,x_1_offset,y_2_offset,x_2_offset,y_3_offset,x_3_offset,y_4_offset,x_4_offset])
# h_4pt = np.array([y_1_p,x_1_p,y_2_p,x_2_p,y_3_p,x_3_p,y_4_p,x_4_p]) # labels
label.append(h_4pt) # [1, 8]
i += 1
data_re.append(data) # [4, 2, 128, 128]
label_re.append(label) # [4, 1, 8]
return data_re, label_re
评论列表
文章目录