-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
59 lines (52 loc) · 2.23 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
from tifffile import imread, imsave
from matplotlib import pyplot as plt
import numpy as np
import os
import cv2
import tensorflow as tf
def split_images(directory, split_by=2, resize=None):
image_names = os.listdir(directory)
image_paths = list()
for i in image_names:
image_paths.append(directory + "/" + i)
img1 = imread(image_paths[0])
original_height = len(img1)
original_width = len(img1[0])
height = original_height // split_by
width = original_width // split_by
dirname = directory + str(height) + "x" + str(width) + "split" + str(split_by)
if not resize == None:
dirname = directory + str(resize[0]) + "x" + str(resize[1]) + "split" + str(split_by)
print(dirname)
if not os.path.exists(dirname):
os.makedirs(dirname)
for iteration in range(len(image_paths)):
path = image_paths[iteration]
image_name = image_names[iteration]
img = imread(path)
print(path)
counter = 0
for i in range(split_by):
for j in range(split_by):
counter += 1
image_name_without_filetype = image_name.split(".", 1)[0]
filetype = image_name.split(".", 1)[1]
filepath = dirname + "/" + image_name_without_filetype + "_" + str(height) + "x" + str(
width) + "_" + str(
counter) + "split" + str(split_by) + "." + filetype
destination_image = img[i * height:(i + 1) * height, j * width:(j + 1) * width]
if not resize == None:
destination_image = cv2.resize(destination_image, dsize=resize, interpolation=cv2.INTER_CUBIC)
filepath = dirname + "/" + image_name_without_filetype + "_" + str(resize[0]) + "x" + str(
resize[1]) + "_" + str(
counter) + "split" + str(split_by) + "." + filetype
print(filepath)
imsave(filepath, destination_image)
def plot_normal_distribution():
with tf.compat.v1.Session().as_default():
codings = tf.random.normal(shape=[1024]).eval()
array = np.array(codings)
print(array.shape)
plt.plot(array, 'o')
plt.show()
split_images("Track1-RGB", 8)