def generate_data_test(img_path):
data = []
img = cv2.resize(cv2.imread(img_path, 0), (640, 480))
y_start = random.randint(64, 160)
y_end = y_start + 256
x_start = random.randint(64, 320)
x_end = x_start + 256
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(-64, 64)
x_1_offset = random.randint(-64, 64)
y_2_offset = random.randint(-64, 64)
x_2_offset = random.randint(-64, 64)
y_3_offset = random.randint(-64, 64)
x_3_offset = random.randint(-64, 64)
y_4_offset = random.randint(-64, 64)
x_4_offset = random.randint(-64, 64)
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, (640, 480))
img_perburb_patch = img_perburb[y_start:y_end, x_start:x_end] # patch 2
data.append(img_patch)
data.append(img_perburb_patch)
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])
h1 = np.array([y_1,x_1,y_2,x_2,y_3,x_3,y_4,x_4])
return data, h_4pt, h1, img, img_perburb
评论列表
文章目录