-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_data_processing.py
More file actions
142 lines (113 loc) · 4.25 KB
/
Copy pathfinal_data_processing.py
File metadata and controls
142 lines (113 loc) · 4.25 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
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
import cv2
import numpy as np
from math import sqrt
from matplotlib import pyplot as plt
import pickle
image_original = cv2.imread("Wood_Masked.jpg")
# fix the colors (BGR to RGB)
image_original = np.flip(image_original, axis=-1)
# get rid of the white part so we can find the colors
image = np.array_split(image_original, 3)
image = np.stack(image)
image = np.array_split(image, 3, axis=2)
image= np.stack(image)
image = list(image.reshape(9, 300, 300, 3))
del image[4]
image = np.concatenate(np.stack(image), axis=0)
# number of bins/colors to find
bin_count = 4
min_percent = .005
show_image = False
show_palette = True
try:
bins = pickle.load(open("bins.p", "rb"))
except:
bins = None
binned_pixels = []
total_pixels = len(image.reshape(-1, 3))*(8/9)
if bins is None or len(bins) != bin_count:
print("Defining Bins")
# must be square
bins = np.random.randint(high=256, low = 0, size=(bin_count, 3))
# find nearest bin for each value
for iteration in range(100):
binned_pixels = [[] for i in range(bin_count)]
print("Finding Nearest Bin for Each Value")
# find distances for each bin for each pixel
dist = np.sum((bins[:, None, :]-image.reshape(-1, 3))**2, axis=2)
# get closest bin for each pixel
idxs = np.argmin(dist, axis=0)
# assign pixel to bin
for i, pixel in enumerate(image.reshape(-1, 3)):
binned_pixels[idxs[i]].append(pixel)
print("Calculating Mean for Each Bin")
# calculate mean for each bin and redefine bin value
total_zeros = 0
max = 0
max_bin = 0
for i, bin in enumerate(binned_pixels):
print(len(bin)/total_pixels)
if len(bin)/total_pixels > min_percent:
bins[i] = np.mean(bin, axis=0)
if len(bin) > max:
max = len(bin)
max_bin = i
else:
bins[i] = np.random.randint(high=256, low = 0, size=(3))
total_zeros += 1
if total_zeros == 0:
break
# stop pixels from collecting at local maximum
bins[max_bin] = np.random.randint(high=256, low = 0, size=(3))
print("Iteration: ", iteration, " total_zeros: ", total_zeros)
if show_palette:
plt.imshow(bins.reshape(1, -1, 3).astype(np.uint8))
plt.show()
if show_image:
# find nearest bin for each value
dist = np.sum((bins[:, None, :]-image_original.reshape(-1, 3))**2, axis=2)
idxs = np.argmin(dist, axis=0)
# Map the image to the nearest colors
quantized = bins[idxs]
plt.imshow(quantized.reshape(image_original.shape).astype(np.uint8))
plt.show()
pickle.dump(bins, open("bins.p", "wb"))
bin_sizes = {}
if len(binned_pixels) != 0:
for i, bin in enumerate(binned_pixels):
bin_sizes.update({i: len(bin)/total_pixels})
# print(bin_sizes)
pickle.dump(bin_sizes, open("bin_sizes.p", "wb"))
def quantize(image, bins = bins):
# find nearest bin for each value
dist = np.sum((bins[:, None, :]-image.reshape(-1, 3))**2, axis=2)
idxs = np.argmin(dist, axis=0)
# Map the image to the nearest colors
quantized = bins[idxs]
return quantized.reshape(image.shape).astype(np.uint8)
def quantize_indexs(image, bins = bins):
# find nearest bin for each value
dist = np.sum((bins[:, None, :]-image.reshape(-1, 3))**2, axis=2)
idxs = np.argmin(dist, axis=0)
# Map the image to the nearest colors
quantized = idxs
return quantized.reshape(image.shape[0], image.shape[1], 1).astype(np.uint8)
# print(bins)
def get_original_data():
image_original = cv2.imread("Wood_Masked.jpg")
# fix the colors (BGR to RGB)
image_original = np.flip(image_original, axis=-1)
# get rid of the white part so we can find the colors
image = np.array_split(image_original, 3)
image = np.stack(image)
image = np.array_split(image, 3, axis=2)
image= np.stack(image)
image = list(image.reshape(9, 300, 300, 3))
del image[4]
processed_data = []
for i, image in enumerate(image):
processed_data.append(image)
# for r in range(4):
# processed_data.append(np.rot90(image))
image = np.concatenate(np.stack(processed_data), axis=0)
return image