File size: 6,841 Bytes
ae29340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import argparse
import tensorflow as tf
import sys
import os
import numpy as np
import cv2
from random import random
import scipy.misc
from glob import glob
import matplotlib.pyplot as plt
import math


def make_image_square(image):

    if image.shape[0] > image.shape[1]:
        shift = int((image.shape[0] - image.shape[1]) / 2)
        print(shift)
        small_dim = image.shape[1]
        image = image[shift : shift + small_dim, :, :]

    else:
        shift = int((image.shape[1] - image.shape[0]) / 2)
        print(shift)
        small_dim = image.shape[0]
        image = image[:, shift : shift + small_dim]

    return image


def mix_with_background(path_background, frame, fg):

    images = [img for img in os.listdir(path_background) if img.endswith(".jpg")]

    # Assure that there is a binary image
    # fg[np.where(fg > 150)] = 255
    # fg[np.where(fg < 150)] = 0

    # fg[np.where(fg == 1)] = 255
    # fg[np.where(fg != 1)] = 0

    upper_limit = 0
    lower_limit = len(images) - 1
    num = np.uint8(random() * (upper_limit - lower_limit) + lower_limit)
    # print(images[num])
    bkg = cv2.imread(os.path.join(path_background, images[num]))
    # bkg = cv2.cvtColor(bkg, cv2.COLOR_BGR2RGB)
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    [index_r, index_c] = np.where((fg[:, :] >= 200))
    # [index_r, index_c] = np.where((fg[:,:] == 1))

    for i in range(1, len(index_r)):

        bkg[index_r[i], index_c[i], 0] = frame[index_r[i], index_c[i], 0]
        bkg[index_r[i], index_c[i], 1] = frame[index_r[i], index_c[i], 1]
        bkg[index_r[i], index_c[i], 2] = frame[index_r[i], index_c[i], 2]

    return bkg


def equalize(image):

    output = np.zeros((image.shape[0], image.shape[1]))

    img_yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
    # This configuration achieves a very slight equalization
    clahe = cv2.createCLAHE(clipLimit=1.0, tileGridSize=(1, 1))
    img_yuv[:, :, 0] = clahe.apply(img_yuv[:, :, 0])
    output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)

    return output


def show_image(image, text=None):
    plt.imshow(image)
    plt.show()
    if text is not None:
        plt.title(text)


def show_two_images(image, image2, text=None, cmap=None, horizontal=True):

    plt.figure(1)

    if horizontal:
        plt.subplot(121)
    else:
        plt.subplot(211)

    if cmap:
        plt.imshow(image, cmap=cmap)
    else:
        plt.imshow(cv2.cvtColor(image.astype("float32"), cv2.COLOR_BGR2RGB))

    if text is not None:
        plt.title(text)

    if horizontal:
        plt.subplot(122)
    else:
        plt.subplot(212)

    if cmap:
        plt.imshow(image2, cmap=cmap)
    else:
        plt.imshow(cv2.cvtColor(image2.astype("float32"), cv2.COLOR_BGR2RGB))
    plt.show()


def show_three_images(image, image2, image3, text=None):

    plt.figure(1)
    plt.subplot(311)
    plt.imshow(image)

    if text is not None:
        plt.title(text)

    plt.subplot(312)

    # plt.imshow(image2,cmap='gray', vmin=0, vmax=1)
    plt.imshow(image2)
    plt.subplot(313)

    # plt.imshow(image3,cmap='gray', vmin=0, vmax=1)
    plt.imshow(image3)
    plt.show()


def show_four_images(image, image2, image3, image4, text=None):

    plt.figure(1)
    plt.subplot(221)
    plt.imshow(image)

    if text is not None:
        plt.title(text)

    plt.subplot(222)
    plt.imshow(image2)
    # plt.imshow(image2, cmap='gray', vmin=0, vmax=1)

    plt.subplot(223)
    plt.imshow(image3)
    # plt.imshow(image3, cmap='gray', vmin=0, vmax=1)

    plt.subplot(224)
    plt.imshow(image4)
    # plt.imshow(image4, cmap='gray', vmin=0, vmax=1)
    plt.show()


def show_six_images(image, image2, image3, image4, image5, image6, text=None):

    plt.figure(1)
    plt.subplot(231)
    plt.imshow(image)

    if text is not None:
        plt.title(text)

    plt.subplot(232)

    plt.imshow(image2, cmap="gray", vmin=0, vmax=1)

    plt.subplot(233)

    plt.imshow(image3, cmap="gray", vmin=0, vmax=1)

    plt.subplot(234)

    plt.imshow(image4, cmap="gray", vmin=0, vmax=1)

    plt.subplot(235)

    plt.imshow(image5, cmap="gray", vmin=0, vmax=1)

    plt.subplot(236)

    plt.imshow(image6, cmap="gray", vmin=0, vmax=1)

    plt.show()


def show_image_per_channel(image, text):

    plt.figure(1)
    plt.subplot(311)
    plt.imshow(image[:, :, 0])
    plt.subplot(312)
    plt.imshow(image[:, :, 1])
    plt.subplot(313)
    plt.imshow(image[:, :, 2])

    plt.show()
    if text is not None:
        plt.title(text)


def pixels_to_labels(image):

    print(image.shape)
    output = np.zeros((image.shape[0], image.shape[1]))

    output[np.where(image > 200)] = 255
    output[np.where(image < 200)] = 0


def jpg_to_png(path):

    image_paths = glob(os.path.join(path, "*.jpg"))

    for i in range(0, len(image_paths)):
        print(i)
        name = image_paths[i]
        idx = name.rfind("/")
        image = scipy.misc.imread(image_paths[i])
        scipy.misc.imsave(
            os.path.join(path, name[idx + 1 : -4] + ".png"), image.astype(np.uint8)
        )  # Really important to convert to uint8


def jpeg_to_jpg(path):

    image_paths = glob(os.path.join(path, "*.jpeg"))

    for i in range(0, len(image_paths)):
        print(i)
        name = image_paths[i]
        idx = name.rfind("/")
        image = scipy.misc.imread(image_paths[i])
        scipy.misc.imsave(
            os.path.join(path, name[idx + 1 : -5] + ".jpg"), image
        )  # Really important to convert to uint8


def overlap_image_with_label(image: np.ndarray, mask: np.ndarray) -> np.ndarray:
    """
    This function overlaps the mask with the image.
    In other words, it only plots the image where the
    segmentation mask is >= 1
    Args:
    - image (numpy array): RGB image of shape (1, 480, 640, 3)
    - label (numpy array): segmentation mask of shape (480, 640)
    Returns:
    - overlap (numpy array): overlapped image
    """
    binary_mask = (mask > 0).astype(np.uint8)
    overlapped = image.copy().squeeze(0)
    overlapped[binary_mask == 0] = 0

    return overlapped


def show_x_images(images, titles=None, cmap=None, horizontal=False):
    num_images = len(images)

    if num_images == 1:
        plt.imshow(images[0])
        if titles:
            plt.title(titles[0])
        plt.show()
        return

    if horizontal:
        cols = num_images
        rows = 1
    else:
        cols = math.ceil(math.sqrt(num_images))
        rows = math.ceil(num_images / cols)

    plt.figure(figsize=(15, 5))
    for i, image in enumerate(images):
        plt.subplot(rows, cols, i + 1)
        if cmap:
            plt.imshow(image, cmap=cmap)
        else:
            plt.imshow(cv2.cvtColor(image.astype("float32"), cv2.COLOR_BGR2RGB))

        if titles and i < len(titles):
            plt.title(titles[i])

    plt.show()
    return