-
Notifications
You must be signed in to change notification settings - Fork 3
/
image.py
90 lines (74 loc) · 2.78 KB
/
image.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
#
# This file is part of wganvo.
# This file is based on a file from https://github.com/ori-mrg/robotcar-dataset-sdk
# (see original license below)
#
# Modifications copyright (C) 2019 Javier Cremona (CIFASIS-CONICET)
# For more information see <https://github.com/CIFASIS/wganvo>
#
# This file is licensed under the Creative Commons
# Attribution-NonCommercial-ShareAlike 4.0 International License.
# To view a copy of this license, visit
# http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to
# Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
#
################################################################################
#
# Copyright (c) 2017 University of Oxford
# Authors:
# Geoff Pascoe (gmp@robots.ox.ac.uk)
#
# This work is licensed under the Creative Commons
# Attribution-NonCommercial-ShareAlike 4.0 International License.
# To view a copy of this license, visit
# http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to
# Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
#
###############################################################################
import re
import numpy as np
from scipy.misc import imresize, imsave, imread
from PIL import Image
from colour_demosaicing import demosaicing_CFA_Bayer_bilinear as demosaic
BAYER_STEREO = 'gbrg'
BAYER_MONO = 'rggb'
def load_image(image_path, model=None):
"""Loads and rectifies an image from file.
Args:
image_path (str): path to an image from the dataset.
model (camera_model.CameraModel): if supplied, model will be used to undistort image.
Returns:
numpy.ndarray: demosaiced and optionally undistorted image
"""
if model:
camera = model.camera
else:
camera = re.search('(stereo|mono_(left|right|rear))', image_path).group(0)
if camera == 'stereo':
pattern = BAYER_STEREO
else:
pattern = BAYER_MONO
if model:
img = demosaic(Image.open(image_path), pattern)
img = model.undistort(img)
img = rgb_2_grey(img)
else:
img = non_demosaic_load(image_path)
assert isinstance(img, np.ndarray) and img.dtype == np.uint8 and img.flags.contiguous
return img
def non_demosaic_load(image_path):
return imread(image_path)
def crop_image(num_array, cropx, cropy):
y = num_array.shape[0]
x = num_array.shape[1]
startx = x // 2 - (cropx // 2)
starty = y // 2 - (cropy // 2)
return num_array[starty:starty + cropy, startx:startx+cropx]
def scale_image(num_array, sizex, sizey):
return imresize(num_array, (sizey,sizex))
def save_image(num_array, path):
imsave(path, num_array)
def savez_compressed(path, array):
np.savez_compressed(path, array)
def rgb_2_grey(img):
return np.dot(img[...,:3],[0.299, 0.587, 0.114]).astype(img.dtype)