-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobtain_targets.py
67 lines (51 loc) · 1.94 KB
/
obtain_targets.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
import cv2
import numpy as np
import matplotlib.pyplot as plt
import yaml
import os
##############################################################################################
# This obtains the target images from the input images already filtered with rmv_repeated.py #
'''
Input
- input_folder
- homography_calib.yaml -> Here is the calibration matrix obtained with calibrator.py
Ouptut:
- output_folder
'''
##############################################################################################
TARGET_H, TARGET_W = 500, 500
M = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
input_folder = r'Dataset\all_input'
output_folder = r'Dataset\all_target'
def load_yalm(file):
global M
with open(file, 'r') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
M = data['M']
M = np.array(M).reshape(3,3)
f.close()
def ipm_from_opencv(image):
global M
warped = cv2.warpPerspective(image, M, (TARGET_W, TARGET_H), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=0)
return warped
load_yalm('homography_calib.yaml')
# Loop through all the images in the input folder
for image_file in os.listdir(input_folder):
# Check if the file is an image file
if not image_file.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp')):
continue
# Load the image
image_path = os.path.join(input_folder, image_file)
image = cv2.imread(image_path)
# Apply the transformation to the image
transformed_image = ipm_from_opencv(image)
# Save the transformed image to the output folder with the same name as the input image
output_path = os.path.join(output_folder, image_file)
cv2.imwrite(output_path, transformed_image)
# image = cv2.cvtColor(cv2.imread('Dataset/data5/data5_0.jpg'), cv2.COLOR_BGR2RGB)
# warped2 = ipm_from_opencv(image)
# #show cv2
# cv2.imshow('warped2',warped2)
# cv2.imshow('image',image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()