2020-05-12 00:58:26 +02:00
|
|
|
from sklearn.preprocessing import LabelEncoder
|
|
|
|
import numpy as np
|
|
|
|
import mahotas
|
|
|
|
import cv2
|
|
|
|
import os
|
|
|
|
import h5py
|
|
|
|
|
|
|
|
rozmiar_zdj = tuple((500, 500))
|
|
|
|
sciezka_do_zdj = "resources/smieci_stare"
|
|
|
|
h5_dane = 'parametry_zdjec.h5'
|
|
|
|
h5_etykiety = 'etykiety.h5'
|
|
|
|
|
|
|
|
|
|
|
|
def hu_moments(zdj):
|
|
|
|
zdj = cv2.cvtColor(zdj, cv2.COLOR_BGR2GRAY)
|
|
|
|
momenty = cv2.HuMoments(cv2.moments(zdj)).flatten()
|
|
|
|
return momenty
|
|
|
|
|
|
|
|
|
|
|
|
def haralick(zdj):
|
|
|
|
szare_zdj = cv2.cvtColor(zdj, cv2.COLOR_BGR2GRAY)
|
|
|
|
haralick = mahotas.features.haralick(szare_zdj).mean(axis=0)
|
|
|
|
return haralick
|
|
|
|
|
|
|
|
|
|
|
|
def histogram(zdj, mask=None):
|
|
|
|
zdj = cv2.cvtColor(zdj, cv2.COLOR_BGR2HSV)
|
|
|
|
hist = cv2.calcHist([zdj], [0, 1, 2], mask, [8, 8, 8], [0, 256, 0, 256, 0, 256])
|
|
|
|
cv2.normalize(hist, hist)
|
|
|
|
return hist.flatten()
|
|
|
|
|
|
|
|
|
2020-05-12 12:21:31 +02:00
|
|
|
klasy = os.listdir(sciezka_do_zdj) # glass metal paper plastic
|
2020-05-12 00:58:26 +02:00
|
|
|
|
|
|
|
klasy.sort()
|
|
|
|
|
2020-05-12 12:21:31 +02:00
|
|
|
dane = [] # parametry kazdego zdjecia
|
|
|
|
|
|
|
|
etykiety_zdj = [] # typ kazdego zdjecia
|
2020-05-12 00:58:26 +02:00
|
|
|
|
|
|
|
for klasa in klasy:
|
|
|
|
katalog = os.path.join(sciezka_do_zdj, klasa)
|
|
|
|
|
|
|
|
for plik in os.listdir(katalog):
|
2020-05-12 12:21:31 +02:00
|
|
|
zdj = cv2.imread(os.path.join(katalog, plik)) # odczyt zdjecia
|
|
|
|
zdj = cv2.resize(zdj, rozmiar_zdj) # zmiana rozmiaru
|
2020-05-12 00:58:26 +02:00
|
|
|
|
2020-05-12 12:21:31 +02:00
|
|
|
# wyznaczanie parametrow zdjecia
|
2020-05-12 00:58:26 +02:00
|
|
|
fv_hu_moments = hu_moments(zdj)
|
|
|
|
fv_haralick = haralick(zdj)
|
|
|
|
fv_histogram = histogram(zdj)
|
|
|
|
|
2020-05-12 12:21:31 +02:00
|
|
|
wiersz = np.hstack([fv_hu_moments, fv_histogram, fv_haralick]) # ustaw poziomo, jeden za drugim
|
|
|
|
etykiety_zdj.append(klasa)
|
2020-05-12 00:58:26 +02:00
|
|
|
dane.append(wiersz)
|
|
|
|
|
2020-05-12 12:21:31 +02:00
|
|
|
# ilosc wszystkich etykiet to 1987 - tyle ile wszystkich zdj
|
2020-05-12 00:58:26 +02:00
|
|
|
le = LabelEncoder()
|
2020-05-12 12:21:31 +02:00
|
|
|
znormalizowane_etykiety = le.fit_transform(etykiety_zdj) # normalizacja do 0-glass 1-metal itp.
|
2020-05-12 00:58:26 +02:00
|
|
|
|
2020-05-12 12:21:31 +02:00
|
|
|
h5f_dane = h5py.File(h5_dane, 'w')
|
|
|
|
h5f_dane.create_dataset('dataset_1', data=np.array(dane))
|
2020-05-12 00:58:26 +02:00
|
|
|
|
2020-05-12 12:21:31 +02:00
|
|
|
h5f_etykiety = h5py.File(h5_etykiety, 'w')
|
|
|
|
h5f_etykiety.create_dataset('dataset_1', data=np.array(znormalizowane_etykiety))
|
2020-05-12 00:58:26 +02:00
|
|
|
|
2020-05-12 12:21:31 +02:00
|
|
|
h5f_dane.close()
|
|
|
|
h5f_etykiety.close()
|