-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.py
162 lines (114 loc) · 4.21 KB
/
start.py
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
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 21 00:02:31 2019
@author: Ivan Alves
"""
import cv2
import numpy as np
from skimage.io import imread
from scipy import signal as sg
from leituraImagens import LeituraImagens
from crescimentoRegioes import CrescimentoRegioes
from kmeans import Kmeans
from time import time
import os
import matplotlib.pyplot as plt
def crescimentoRegioes(imagens, segmentadas, path):
#crescimento de Regioes
c = CrescimentoRegioes(imagens)
qtd = 230
for i in imagens:
img = imread( imagens[qtd][0], as_gray = True)
img = (img * 255).round().astype(np.uint8)
img = cv2.resize(img, (segmentadas[qtd][2], segmentadas[qtd][1]))
semente = c.obterSemente(img)
media3 = [[1./9., 1./9., 1./9.],
[1./9., 1./9., 1./9.],
[1./9., 1./9., 1./9.]]
c_media = sg.convolve( img , media3, "valid")
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (300, 300));
blackhat = cv2.morphologyEx( c_media , cv2.MORPH_BLACKHAT, kernel)
#cv2.imwrite("blackhat.jpg", blackhat)
qtd +=1
regiao = c.crescerRegiao( blackhat , semente, epsilon=3)
cv2.imwrite(os.path.join(path, "segmentada_{}.png" .format(qtd)) , regiao)
print("imagem: ", qtd)
def kmeans(imagens, segmentadas, path):
k = Kmeans(imagens)
qtd = 0
for i in imagens:
# Leitura Imagem
img = imread(imagens[qtd][0])
#img = cv2.resize(img, (segmentadas[qtd][2], segmentadas[qtd][1]))
res2 = k.kmeans_seg(img, 2) / 255
res3 = k.kmeans_seg(img, 3) / 255
res9 = k.kmeans_seg(img, 5) / 255
cv2.imwrite("res2.png", res2)
cv2.imwrite("res3.png", res3)
cv2.imwrite("res9.png", res9)
fig = plt.figure(figsize=(9,3), dpi=200)
k.add_image(fig, img, 1, 4, 1, 'original')
k.add_image(fig, res2, 1, 4, 2, 'k=2')
k.add_image(fig, res3, 1, 4, 3, 'k=3')
k.add_image(fig, res9, 1, 4, 4, 'k=5')
if __name__ == '__main__':
#leitura das Imagens
start_time = time()
imagens = []
l = LeituraImagens
imagens = l.leitura('../dataset/images/*/*/*.jpg')
stop_time = time()
tempo_total = stop_time - start_time
print("Tempo total da leitura das imagens: ", tempo_total)
#leitura das Imagens Segmentadas
start_time = time()
segmentadas = []
s = LeituraImagens
segmentadas = s.leitura('../dataset/segmented/*/*/*.png')
stop_time = time()
tempo_total = stop_time - start_time
print("Tempo total da leitura das imagens segmentadas: ", tempo_total)
#Crescimento de Regioes
try:
os.mkdir('./Crescimento Regioes')
except OSError:
print("Diretorio Crescimendo de Regioes já foi criado")
path = './Crescimento Regioes'
start_time = time()
crescimentoRegioes(imagens, segmentadas, path)
stop_time = time()
tempo_total = stop_time - start_time
print("Tempo total do crescimento de regiões: ", tempo_total)
try:
os.mkdir('./Kmeans')
except OSError:
print("Diretorio Kmeans ja foi criado")
path = './Kmeans'
start_time = time()
kmeans(imagens, segmentadas, path)
stop_time = time()
tempo_total = stop_time - start_time
print("Tempo total do kmeans: ", tempo_total)
"""
# Leitura Imagem
#img1 = imread('3095_lg.tiff')
img1 = imread('t.jpg')
res2 = kmeans_seg(img1, 2) / 255
res5 = kmeans_seg(img1, 5) / 255
res9 = kmeans_seg(img1, 9) / 255
fig = plt.figure(figsize=(9,3), dpi=200)
add_image(fig, img1, 1, 4, 1, 'original')
add_image(fig, res2, 1, 4, 2, 'k=2')
add_image(fig, res5, 1, 4, 3, 'k=5')
add_image(fig, res9, 1, 4, 4, 'k=9')
"""
# Leitura Imagem
img1 = imread('t.jpg')
res2 = kmeans_seg(img1, 2) / 255
res3 = kmeans_seg(img1, 3) / 255
res9 = kmeans_seg(img1, 5) / 255
fig = plt.figure(figsize=(9,3), dpi=200)
add_image(fig, img1, 1, 4, 1, 'original')
add_image(fig, res2, 1, 4, 2, 'k=2')
add_image(fig, res3, 1, 4, 3, 'k=3')
add_image(fig, res9, 1, 4, 4, 'k=5')