-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_data.py
More file actions
72 lines (56 loc) · 2.03 KB
/
format_data.py
File metadata and controls
72 lines (56 loc) · 2.03 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
65
66
67
68
69
70
71
72
import numpy as np
import cv2
import sys
import glob
import os.path
from pathlib import Path
# Maps regions around each pixel and organizes them in
# the form of a matrix where each line corresponds to a region
def image_mapping(image, k):
edge = k - 1
A = np.uint8(edge / 2)
temp = cv2.resize(image, (image.shape[1] + edge, image.shape[0] + edge), interpolation=cv2.INTER_LANCZOS4)
temp[A:image.shape[0] + A, A:image.shape[1] + A] = image[:, :]
regions = np.zeros((image.shape[0] * image.shape[1], k**2), dtype='uint8')
u = 0
for i in range(A, image.shape[0] + A):
for j in range(A, image.shape[1] + A):
aux = temp[i - A:i + 1 + A, j - A:j + 1 + A]
regions[u, :] = aux.reshape(1, aux.shape[0] * aux.shape[1])
u+=1
return regions
# Create data with k*k features and 2 targets
def format_data(image, k):
Y, Cr, Cb = cv2.split(cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb))
inputs = image_mapping(Y, k)
targets = np.transpose(np.concatenate((Cr.reshape(1, Cr.shape[0] * Cr.shape[1]),
Cb.reshape(1, Cb.shape[0] * Cb.shape[1]))))
data = np.zeros((inputs.shape[0], inputs.shape[1] + targets.shape[1]), dtype='uint8')
data[:, 0:inputs.shape[1]] = inputs[:,:]
data[:, inputs.shape[1]:inputs.shape[1] + targets.shape[1]] = targets[:,:]
return data
def main():
FIRST_ARG = 1
FLAG = 2
window_size = np.uint8(input("Enter the side window size (must be odd): "))
if len(sys.argv) > 2 and sys.argv[FLAG] == '-all':
temp = Path(sys.argv[1])
if temp.is_dir() is False:
print("Invalid path!")
sys.exit()
for file in np.sort(glob.glob("%s*.png" % sys.argv[FIRST_ARG])):
image = cv2.imread(file)
data = format_data(image, window_size)
np.savetxt('%s.data' % file, data)
elif len(sys.argv) > 1:
temp = os.path.isfile(sys.argv[FIRST_ARG])
if temp is False:
print("Invalid file!")
sys.exit()
image = cv2.imread("%s" % sys.argv[FIRST_ARG])
data = format_data(image, window_size)
np.savetxt("%s.data" % (sys.argv[FIRST_ARG]), data, fmt='%d')
else:
print("Enter a valid argument.")
if __name__ == '__main__':
main()