-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrockSuperPixelMergeAndBoostColor.py
More file actions
180 lines (126 loc) · 5.93 KB
/
rockSuperPixelMergeAndBoostColor.py
File metadata and controls
180 lines (126 loc) · 5.93 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
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
import re
import glob
import threading
import matplotlib.pyplot as plt
import numpy as np
import cv2
from skimage import io
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from sklearn.neighbors import KNeighborsClassifier
def calculateStats(numToDo, start, segments_slic, img, avgColorInRegion, numPixelsInRegion):
for i in range(numToDo):
mask = segments_slic == i + start
validPixels = img[mask]
avg = np.mean(validPixels, axis = 0)
tot = len(validPixels)
avgColorInRegion[i + start] = avg
numPixelsInRegion[i + start] = tot
def convertRegionsToRocks(regionNums, segments_slic, img, model):
for i in range(len(regionNums)):
mask = segments_slic == regionNums[i]
validPixels = img[mask]
avg = np.mean(validPixels, axis = 0)
avg = avg / 255
closestFile = model.predict(avg.reshape(1,-1))
closestRock = io.imread(closestFile[0])
numsAsStrings = re.findall("\d+\.\d+", closestFile[0])
vals = np.asarray([float(x) for x in numsAsStrings])
mul = np.asarray([vals[i] / (avg[i] + (1 if avg[i] == 0 else 0)) for i in range(3)])
indexes = np.where(mask)
rows = indexes[0]
cols = indexes[1]
startRow = rows.min()
startCol = cols.min()
for j in range(len(rows)):
row = rows[j]
col = cols[j]
# img[row,col] = closestRock[row - startRow,col-startCol] * mul
img[row,col] = closestRock[row - startRow,col-startCol]
def calculateStatsOwner(numRegions, segments_slic, img, avgColorInRegion, numPixelsInRegion):
numthreads = 45
threads = []
for i in range(numthreads):
start = int(i * numRegions/numthreads)
stop = int((i+1) * numRegions/numthreads)
numToDo = stop - start
newT = threading.Thread(target=calculateStats, args=(numToDo, start, segments_slic, img, avgColorInRegion, numPixelsInRegion))
threads.append(newT)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
def convertRegionsToRocksOwner(validRegionNumbers, segments_slic, img, model):
numRegions = len(validRegionNumbers)
numthreads = 45
threads = []
for i in range(numthreads):
start = int(i * numRegions/numthreads)
stop = int((i+1) * numRegions/numthreads)
newT = threading.Thread(target=convertRegionsToRocks, args=(validRegionNumbers[start:stop], segments_slic, img, model))
threads.append(newT)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
def convertToRocks(img, model):
imgAsFloat = img_as_float(img)
segments_slic = slic(imgAsFloat, n_segments=100000, compactness=0.5, sigma=1,
start_label=0)
numRegions = len(np.unique(segments_slic))
avgColorInRegion = {}
numPixelsInRegion = {}
regionRowCount = {}
regionColCount = {}
calculateStatsOwner(numRegions, segments_slic, img, avgColorInRegion, numPixelsInRegion)
h, w, _ = img.shape
for row in range(h-1):
for col in range(w-1):
baseReg = segments_slic[row,col]
nextReg = segments_slic[row+1,col+1]
if baseReg != nextReg and sum(abs(avgColorInRegion[baseReg] - avgColorInRegion[nextReg])) < 12:
segments_slic[segments_slic == nextReg] = baseReg
baseAvg = avgColorInRegion[baseReg]
baseNumPixels = numPixelsInRegion[baseReg]
nextAvg = avgColorInRegion[nextReg]
nextNumPixels = numPixelsInRegion[nextReg]
avgColorInRegion[baseReg] = (baseAvg*baseNumPixels + nextAvg*nextNumPixels)/(baseNumPixels + nextNumPixels)
numPixelsInRegion[baseReg] = baseNumPixels + nextNumPixels
avgColorInRegion.pop(nextReg)
numPixelsInRegion.pop(nextReg)
for col in range(w-1):
for row in range(h-1):
baseReg = segments_slic[row,col]
nextReg = segments_slic[row+1,col+1]
if baseReg != nextReg and sum(abs(avgColorInRegion[baseReg] - avgColorInRegion[nextReg])) < 12:
segments_slic[segments_slic == nextReg] = baseReg
baseAvg = avgColorInRegion[baseReg]
baseNumPixels = numPixelsInRegion[baseReg]
nextAvg = avgColorInRegion[nextReg]
nextNumPixels = numPixelsInRegion[nextReg]
avgColorInRegion[baseReg] = (baseAvg*baseNumPixels + nextAvg*nextNumPixels)/(baseNumPixels + nextNumPixels)
numPixelsInRegion[baseReg] = baseNumPixels + nextNumPixels
avgColorInRegion.pop(nextReg)
numPixelsInRegion.pop(nextReg)
assert avgColorInRegion.keys() == numPixelsInRegion.keys()
validRegionNumbers = list(avgColorInRegion.keys())
# cv2.imwrite('img3.png', mark_boundaries(img, segments_slic))
convertRegionsToRocksOwner(validRegionNumbers, segments_slic, img, model)
# cv2.imwrite('img3.png', cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
cv2.imwrite('img8.png', img)
def createModel(src):
files = glob.glob(f'{src}/*')
rgbValues = []
for file in files:
numsAsStrings = re.findall("\d+\.\d+", file)
vals = [float(x) for x in numsAsStrings]
rgbValues.append(vals)
model = KNeighborsClassifier(n_neighbors=1)
model.fit(rgbValues, files)
return model
if __name__ == "__main__":
model = createModel('largeRGBImagesProp2')
img = io.imread('numbers.jpg')
img = img[:,:,0:3]
convertToRocks(img, model)