-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombined.py
More file actions
64 lines (43 loc) · 1.59 KB
/
combined.py
File metadata and controls
64 lines (43 loc) · 1.59 KB
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
from __future__ import division
from __future__ import print_function
import numpy as np
import cv2
import os.path as path
from glob import glob
from operator import mul
# === Imageset Loading ===
def image_paths(directory):
return sorted(glob(path.join(directory, '*')))
def load_images(filepaths, option=cv2.IMREAD_COLOR):
return [cv2.imread(filepath, option) for filepath in filepaths]
def load_imageset(directory, option=cv2.IMREAD_COLOR):
return load_images(image_paths(directory), option)
# === Imageset Stats ===
def imageset_stats(imageset):
stats = {}
count = len(imageset)
shape = {}
height_sum, width_sum = 0, 0
for img in imageset:
height_sum += img.shape[0]
width_sum += img.shape[1]
shape["mean_height"] = height_sum / count
shape["mean_width"] = width_sum / count
stats["count"] = count
stats["shape"] = shape
return stats
# === Resize Images ===
def resize_images(images, shape=(128,128)):
return [cv2.resize(img, dsize=shape, interpolation=cv2.INTER_CUBIC) for img in images]
def resize_to_mean(imageset):
imgset_stats = imageset_stats(imageset)
target_shape = (int(imgset_stats["shape"]["mean_width"]), int(imgset_stats["shape"]["mean_height"]))
return resize_images(imageset, shape=target_shape)
# === Columnize ===
def columnize(dataset):
return [elem.reshape(reduce(mul, elem.shape, 1)) for elem in dataset]
if __name__ == "__main__":
input_dir = "images"
imageset = resize_to_mean(load_imageset("images", cv2.IMREAD_GRAYSCALE))
data = columnize(imageset)
print(np.array(data).shape)