Skip to content
Snippets Groups Projects
Commit 6b30b339 authored by paul_pvc's avatar paul_pvc
Browse files

push GLCM pas optimisé

parent f55f2fc6
No related branches found
No related tags found
1 merge request!1Gris
......@@ -31,8 +31,29 @@ def buildSampleFromPath(path1, path2, size=0):
def computePixelBW_histo(image):
return Image.fromarray(np.array(image.convert("L"))).histogram()
return image.convert("L").histogram()
def compute_glcm(image):
img_array = np.array(image)
distance, angle = 5, 0
glcm = np.zeros((256, 256))
offsets = (int(distance * np.cos(angle)), int(distance * np.sin(angle)))
for i in range(image.height - abs(offsets[1])):
for j in range(image.width - abs(offsets[0])):
px1 = img_array[i, j]
px2 = img_array[i + offsets[1], j + offsets[0]]
glcm[px1, px2] += 1
glcm /= np.sum(glcm)
return glcm
def extract_data_glcm(glcm):
contrast = np.sum([[(i - j) ** 2 * glcm[i, j] for j in range(glcm.shape[1])] for i in range(glcm.shape[0])])
energy = np.sum(glcm ** 2)
homogeneity = np.sum(
[[(glcm[i, j] / (1 + abs(i - j))) for j in range(glcm.shape[1])] for i in range(glcm.shape[0])])
return [contrast, energy, homogeneity]
def computeDict(image_path, path, y_true_value, max_size: tuple):
"""
......@@ -56,6 +77,7 @@ def computeDict(image_path, path, y_true_value, max_size: tuple):
#"resized_image": resized,
"X_histo": computeHisto(resized),
"X_pixelbw": computePixelBW_histo(resized),
"X_glcm_data": extract_data_glcm(compute_glcm(resized)),
"y_true_class": y_true_value,
"y_predicted_class": None}
......@@ -94,7 +116,7 @@ def fitFromHisto(S, algo):
S_train, S_test, y_train, y_test = train_test_split(S, y, test_size=0.2, random_state=42)
X_train = np.array([np.array(l["X_histo"]+l["X_pixelbw"]) for l in S_train])
X_train = np.array([np.array(l["X_histo"]+l["X_glcm_data"]) for l in S_train])
#X_train = df[["X_histo", "X_pixelbw"]]
#print(X_train)
#print(len(X_train[0]))
......@@ -113,7 +135,7 @@ def predictFromHisto(S, model, list_dict=True):
:param list_dict: is the sample in list(dict)
:return: None
"""
tab = model.predict(np.array([x["X_histo"]+x["X_pixelbw"] for x in S]))
tab = model.predict(np.array([x["X_histo"]+x["X_glcm_data"] for x in S]))
if list_dict:
for i in range(len(S)):
S[i]["y_predicted_class"] = tab[i]
......
......@@ -29,7 +29,7 @@ def test_sample():
def global_test():
S = TP.buildSampleFromPath(path1_t, path2_t)
classifier, S_test, y_test, S_train, y_train = TP.fitFromHisto(S, SVC())
classifier, S_test, y_test, S_train, y_train = TP.fitFromHisto(S, SVC(kernel="rbf"))
TP.predictFromHisto(S, classifier)
print("Erreur empirique :", TP.computeError(S_train), "erreurs")
print("Erreur réelle :", TP.computeError(S_test), "erreurs")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment