-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
188 lines (155 loc) · 5.83 KB
/
Copy pathpreprocessing.py
File metadata and controls
188 lines (155 loc) · 5.83 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
181
182
183
184
185
186
187
188
__author__ = 'hamdiahmadi'
import cv2
import copy
import os
from PIL import Image as Images
import statistics as stat
import numpy as np
#class Image, untuk membaca gambar, show gambar, convert apapun menggunakan library cv2
class Image:
def __init__(self):
pass
def readImage(self,path):
return cv2.imread(path)
def imageToGray(self,image):
return cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
def medianBluring(self,image,filter_size):
return cv2.medianBlur(image,filter_size)
def saveImage(self,file_name,image):
return cv2.imwrite(file_name,image)
def edgeDetection(self,image):
return cv2.Canny(image,100,200)
def imageToBinary(self,image):
return cv2.threshold(image,127,255,cv2.THRESH_BINARY_INV)
def imageToInverseBinary(self,image):
return cv2.threshold(image,0,127,cv2.THRESH_BINARY)
class DeNoising(Image):
def __init__(self):
pass
#using median bluring
def medianFilter(self,image,filter_size):
return Image.medianBluring(self,image,filter_size)
class Binarization(Image):
def __init__(self):
pass
def binarization(self,image):
return Image.imageToBinary(self,image)[1]
class EdgeDetection(Image):
def __init__(self):
pass
def cannyEdgeDetection(self,image):
return Image.edgeDetection(self,image)
class Data:
def __init__(self):
pass
def clockWise(self):
clocks = []
clocks.append([-1,-1])
clocks.append([-1,0])
clocks.append([-1,1])
clocks.append([0,-1])
clocks.append([0,1])
clocks.append([1,-1])
clocks.append([1,0])
clocks.append([1,1])
return clocks
class Localization(Image, Data):
def __init__(self):
pass
def localization(self,image):
background_counter = dict()
background_counter[0] = 0
background_counter[255] = 0
is_visit = copy.copy(image)*0
region_number = 0
region = []
for x in range(0,len(image[0])):
for y in range(0,len(image)):
background_counter[image[y][x]]+=1
if is_visit[y][x] == 0:
stack = []
region_number+=1
stack.append([y,x])
is_visit[y][x] = int(region_number)
coordinate,is_visit = self.growing(image,is_visit,stack,region_number)
img = image[coordinate[2]:coordinate[0],coordinate[3]:coordinate[1]]
if (coordinate[0]-coordinate[2])>= len(image)/3 and (coordinate[1]-coordinate[3])>=len(image[0])/50 and (coordinate[0]-coordinate[2])<= len(image)/1.5 and (coordinate[1]-coordinate[3])<=len(image[0])/1.5:
region.append(img)
if background_counter[255] > background_counter[0]:
result = region
else :
result = []
cntr = 0
for x in region:
cntr+=1
for row in range(0,len(x)):
for col in range(0,len(x[row])):
if x[row][col] == 255:
x[row][col] = 0
else :
x[row][col] = 255
result.append(x)
return result
def growing(self,image,is_visit,stack,region_number):
listsY = []
listsX = []
clock = Data.clockWise(self)
while len(stack)!=0:
coory,coorx = stack[0]
stack.pop(0)
listsY.append(coory)
listsX.append(coorx)
for x in clock:
try :
if coory+x[0] < 0 or coory+x[0] > len(is_visit) or coorx+x[1] <0 or coorx+x[1] > len(is_visit[0]):
pass
elif is_visit[coory+x[0]][coorx+x[1]] == 0 and image[coory][coorx] == image[coory+x[0]][coorx+x[1]]:
is_visit[coory+x[0]][coorx+x[1]] = region_number
stack.append([coory+x[0],coorx+x[1]])
except :
pass
return [max(listsY),max(listsX),min(listsY),min(listsX)],is_visit
class File:
def __init__(self):
pass
def deleteFileInFolder(self,path):
for files in os.listdir(path):
files_ = os.path.join(path, files)
try :
if os.path.isfile(files_):
os.unlink(files_)
except:
pass
return
def readFileFolder(self,path):
return os.listdir(path)
class GetColor:
def __init__(self):
pass
def getDataTesting(self,path):
image = Images.open(path)
image = image.resize((50,50))
res = self.most_frequent_colour(image)
return res
def most_frequent_colour(self,image):
w, h = image.size
pixels = image.getcolors(w * h)
most_frequent_pixel = pixels[0]
for count, colour in pixels:
if count > most_frequent_pixel[0]:
most_frequent_pixel = (count, colour)
res = list(most_frequent_pixel[1][0:3])
return res
def category(self,most_frequent_color):
if most_frequent_color[0]<128 and most_frequent_color[1]<128 and most_frequent_color[2]<128:
return 'hitam'
elif most_frequent_color[0] >= 128 and most_frequent_color[1]>= 128 and most_frequent_color[2]< 128:
return 'kuning'
elif most_frequent_color[0] >= 128 and most_frequent_color[1]<128 and most_frequent_color[2]<128:
return 'merah'
elif most_frequent_color[0] >= 128 and most_frequent_color[1]>=128 and most_frequent_color[2]>= 128:
return 'putih'
elif most_frequent_color[0] < 128 and most_frequent_color[1]>=128 and most_frequent_color[2]>= 128:
return 'putih'
else:
return 'unknown color'