-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
98 lines (70 loc) · 2.73 KB
/
utils.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
91
92
93
94
95
96
97
98
################################################################################
# image manipulation utilities - general and stereo camera specific
# Copyright (c) 2018 Toby Breckon, Durham University, UK
# License: MIT License (MIT)
################################################################################
import cv2
import numpy as np
################################################################################
# concatenate two RGB/grayscale images horizontally (left to right) handling
# differing channel numbers or image heights in the input
def h_concatenate(img1, img2):
# get size and channels for both images
height1 = img1.shape[0]
width1 = img1.shape[1]
if (len(img1.shape) == 2):
channels1 = 1
else:
channels1 = img1.shape[2]
height2 = img2.shape[0]
width2 = img2.shape[1]
if (len(img2.shape) == 2):
channels2 = 1
else:
channels2 = img2.shape[2]
# make all images 3 channel, or assume all same channel
if ((channels1 > channels2) and (channels1 == 3)):
out2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)
out1 = img1
elif ((channels2 > channels1) and (channels2 == 3)):
out1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
out2 = img2
else: # both must be equal
out1 = img1
out2 = img2
# height of first image is master height, width can remain unchanged
if (height1 != height2):
out2 = cv2.resize(out2, (height1, width2))
return np.hstack((out1, out2))
################################################################################
# concatenate two RGB/grayscale images vertically (top to bottom) handling
# differing channel numbers or image heights in the input
def v_concatenate(img1, img2):
# get size and channels for both images
height1 = img1.shape[0]
width1 = img1.shape[1]
if (len(img1.shape) == 2):
channels1 = 1
else:
channels1 = img1.shape[2]
height2 = img2.shape[0]
width2 = img2.shape[1]
if (len(img2.shape) == 2):
channels2 = 1
else:
channels2 = img2.shape[2]
# make all images 3 channel, or assume all same channel
if ((channels1 > channels2) and (channels1 == 3)):
out2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)
out1 = img1
elif ((channels2 > channels1) and (channels2 == 3)):
out1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
out2 = img2
else: # both must be equal
out1 = img1
out2 = img2
# width of first image is master height, height can remain unchanged
if (width1 != width2):
out2 = cv2.resize(out2, (height2, width1))
return np.vstack((out1, out2))
################################################################################