Datasets:
Tasks:
Image Segmentation
Languages:
English
Size:
10K<n<100K
ArXiv:
Tags:
object-centric learning
License:
Create preprocess.py
Browse files- preprocess.py +59 -0
preprocess.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def crop_and_resize(image_path, bbox_path, crop_path, resize_path):
|
| 6 |
+
img = Image.open(image_path)
|
| 7 |
+
width, height = img.size
|
| 8 |
+
|
| 9 |
+
with open(bbox_path, 'r') as f:
|
| 10 |
+
_, x_center, y_center, _, _ = map(float, f.readline().split())
|
| 11 |
+
x_center, y_center = int(x_center * width), int(y_center * height)
|
| 12 |
+
|
| 13 |
+
x1, x2 = max(0, x_center - 128), min(width, x_center + 128)
|
| 14 |
+
y1, y2 = max(0, y_center - 128), min(height, y_center + 128)
|
| 15 |
+
|
| 16 |
+
crop = img.crop((x1, y1, x2, y2))
|
| 17 |
+
assert crop.size == (256, 256)
|
| 18 |
+
crop.save(crop_path)
|
| 19 |
+
|
| 20 |
+
resize = crop.resize(size=(128, 128), resample=Image.BICUBIC)
|
| 21 |
+
assert resize.size == (128, 128)
|
| 22 |
+
resize.save(resize_path)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def transform_intrinsic(intrinsic_path, bbox_path, crop_path, resize_path):
|
| 26 |
+
intrinsic = np.loadtxt(intrinsic_path)
|
| 27 |
+
fx, fy = intrinsic[0, 0], intrinsic[1, 1]
|
| 28 |
+
cx, cy = intrinsic[0, 2], intrinsic[1, 2]
|
| 29 |
+
width, height = cx * 2, cy * 2
|
| 30 |
+
|
| 31 |
+
with open(bbox_path, 'r') as f:
|
| 32 |
+
_, x_center, y_center, _, _ = map(float, f.readline().split())
|
| 33 |
+
x_center, y_center = int(x_center * width), int(y_center * height)
|
| 34 |
+
|
| 35 |
+
x1, y1 = max(0, x_center - 128), max(0, y_center - 128)
|
| 36 |
+
|
| 37 |
+
K = np.array([[fx, 0, cx - x1],
|
| 38 |
+
[0, fy, cy - y1],
|
| 39 |
+
[0, 0, 1]])
|
| 40 |
+
np.savetxt(crop_path, K, fmt="%.5f", delimiter=" ")
|
| 41 |
+
|
| 42 |
+
K[:2] /= 2
|
| 43 |
+
np.savetxt(resize_path, K, fmt="%.5f", delimiter=" ")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
# crop and resize image
|
| 48 |
+
image_path = '640x480/image/0000_00.png'
|
| 49 |
+
bbox_path = '640x480/bbox/0000_00.txt'
|
| 50 |
+
crop_path = '256x256/image/0000_00.png'
|
| 51 |
+
resize_path = '128x128/image/0000_00.png'
|
| 52 |
+
crop_and_resize(image_path, bbox_path, crop_path, resize_path)
|
| 53 |
+
|
| 54 |
+
# transform intrinsic matrix
|
| 55 |
+
intrinsic_path = '640x480/intrinsic.txt'
|
| 56 |
+
bbox_path = '640x480/bbox/0000_00.txt'
|
| 57 |
+
crop_path = '256x256/intrinsic/0000_00.txt'
|
| 58 |
+
resize_path = '128x128/intrinsic/0000_00.txt'
|
| 59 |
+
transform_intrinsic(intrinsic_path, bbox_path, crop_path, resize_path)
|