Compare commits
12 Commits
0b39c40408
...
filter_can
Author | SHA1 | Date | |
---|---|---|---|
4aa559772c | |||
7da1c028ac | |||
975e7b9c23 | |||
e345a69657
|
|||
3d071bae42 | |||
fc6ed23f4c
|
|||
3810e0bc88 | |||
450908c2b8 | |||
34cc884c5d | |||
346dff092d | |||
1e2225337e | |||
8e5d626074 |
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
.vscode/
|
||||
imageEngine/images/
|
||||
imageEngine/test/
|
||||
imageEngine/__pycache__/
|
||||
imageEngine/filters/__pycache__/
|
||||
imageEngine/test.py
|
3
.vscode/settings.json
vendored
@ -1,3 +0,0 @@
|
||||
{
|
||||
"python.analysis.typeCheckingMode": "basic"
|
||||
}
|
32
imageEngine/filters/canny.py
Normal file
@ -0,0 +1,32 @@
|
||||
from copy import deepcopy
|
||||
from filters.usefull_func import *
|
||||
from math import sqrt, atan2, pi
|
||||
|
||||
|
||||
def filtreCanny(img, Th):
|
||||
Tl = Th / 2
|
||||
|
||||
filtred_image = filtre_gaussien(img)
|
||||
|
||||
norme_gradient, angle_normale_gradient = calculGradient(filtred_image)
|
||||
|
||||
non_maxima = dltNoMaxima(norme_gradient, angle_normale_gradient)
|
||||
|
||||
contours = seuillageHysteresis(non_maxima, angle_normale_gradient, Th, Tl)
|
||||
|
||||
return contours
|
||||
|
||||
"""
|
||||
def filtreCannySemiAuto(img, centile):
|
||||
filtred_image = filtre_gaussien(img)
|
||||
|
||||
norme_gradient, angle_normale_gradient = calculGradient(filtred_image)
|
||||
|
||||
non_maxima = dltNoMaxima(norme_gradient, angle_normale_gradient)
|
||||
|
||||
Th = calculTh(norme_gradient, centile)
|
||||
Tl = Th / 2
|
||||
contours = seuillageHysteresis(non_maxima, angle_normale_gradient, Th, Tl)
|
||||
|
||||
return contours
|
||||
"""
|
15
imageEngine/filters/sobel.py
Normal file
@ -0,0 +1,15 @@
|
||||
from filters.usefull_func import *
|
||||
|
||||
def filtre_sobel(img):
|
||||
|
||||
if not is_greyscale(img):
|
||||
img = greyscale(img)
|
||||
|
||||
mat_x = [[-1,0,1],[-2,0,2],[-1,0,1]]
|
||||
mat_y = [[-1,-2,-1],[0,0,0],[1,2,1]]
|
||||
Gx = convolution(img, mat_x)
|
||||
Gy = convolution(img, mat_y)
|
||||
|
||||
filtred_image = application_norme(Gx,Gy)
|
||||
return filtred_image
|
||||
|
293
imageEngine/filters/usefull_func.py
Normal file
@ -0,0 +1,293 @@
|
||||
from copy import deepcopy
|
||||
from math import atan2, sqrt, pi
|
||||
|
||||
def greyscale(mat_img):
|
||||
gray_img = []
|
||||
for ligne in mat_img:
|
||||
lig = []
|
||||
for r,g,b in ligne:
|
||||
v = int(r*0.2125 + g*0.7154 + b*0.0721)
|
||||
lig.append((v,)*3)
|
||||
gray_img.append(lig)
|
||||
return gray_img
|
||||
|
||||
|
||||
def appliquer_convolution(img, mat, i, j):
|
||||
somme = 0
|
||||
for x in range(len(mat)):
|
||||
for y in range(len(mat[0])):
|
||||
coord_i = i - (len(mat) // 2) + x
|
||||
corrd_j = j - (len(mat[0]) // 2) + y
|
||||
pix = pixel(img, coord_i, corrd_j)
|
||||
somme += pix[0]*mat[x][y]
|
||||
return min(max(somme,0), 255)
|
||||
|
||||
|
||||
def convolution(mat_img, mat):
|
||||
return_img = []
|
||||
for i in range(len(mat_img)):
|
||||
ligne = []
|
||||
for j in range(len(mat_img[0])):
|
||||
val = appliquer_convolution(mat_img, mat, i, j)
|
||||
ligne.append((val,)*3)
|
||||
return_img.append(ligne)
|
||||
return return_img
|
||||
|
||||
|
||||
def is_greyscale(img):
|
||||
_greyscale = True
|
||||
for ligne in img:
|
||||
for r,g,b in ligne:
|
||||
if not (r==g and g==b):
|
||||
_greyscale = False
|
||||
break
|
||||
if not _greyscale:
|
||||
break
|
||||
return _greyscale
|
||||
|
||||
|
||||
def invert(img):
|
||||
result_image = []
|
||||
for ligne in img:
|
||||
result_ligne = []
|
||||
for r,g,b in ligne:
|
||||
result_ligne.append((255-r, 255-g, 255-b))
|
||||
result_image.append(result_ligne)
|
||||
return result_image
|
||||
|
||||
|
||||
def pixel(img, i, j, default=(0,0,0)):
|
||||
#i la colone et j la ligne
|
||||
if 0 <= i < len(img) and 0 <= j < len(img[0]):
|
||||
return img[i][j]
|
||||
else:
|
||||
return default
|
||||
|
||||
|
||||
def reduction_bruit(img, mat, i, j):
|
||||
somme = 0
|
||||
for x in range(len(mat)):
|
||||
for y in range(len(mat[0])):
|
||||
pixel_i = i - (len(mat) // 2) + x
|
||||
pixel_j = j - (len(mat[0]) // 2) + y
|
||||
pix = pixel(img, pixel_i, pixel_j)
|
||||
somme += pix[0]*mat[x][y]
|
||||
normalise = round(somme)
|
||||
return normalise
|
||||
|
||||
|
||||
def filtre_gaussien(mat_img):
|
||||
mat_gauss = [
|
||||
[2/159, 4/159, 5/159, 4/159,2/159],
|
||||
[4/159, 9/159,12/159, 9/159,4/159],
|
||||
[5/159,12/159,15/159,12/159,5/159],
|
||||
[4/159, 9/159,12/159, 9/159,4/159],
|
||||
[2/159, 4/159, 5/159, 4/159,2/159]
|
||||
]
|
||||
|
||||
return_img = []
|
||||
for i in range(len(mat_img)):
|
||||
ligne = []
|
||||
for j in range(len(mat_img[0])):
|
||||
val = reduction_bruit(mat_img, mat_gauss, i, j)
|
||||
ligne.append((val,)*3)
|
||||
return_img.append(ligne)
|
||||
return return_img
|
||||
|
||||
|
||||
def calcul_norme(pixel1, pixel2):
|
||||
valeur = pixel1[0]**2 + pixel2[0]**2
|
||||
norm = round(sqrt(valeur))
|
||||
norm = int(min(norm, 255))
|
||||
return norm
|
||||
|
||||
|
||||
def application_norme(im_x, im_y):
|
||||
result_image = []
|
||||
for i in range(len(im_x)):
|
||||
ligne = []
|
||||
for j in range(len(im_x[0])):
|
||||
pixel1 = im_x[i][j]
|
||||
pixel2 = im_y[i][j]
|
||||
norme = calcul_norme(pixel1, pixel2)
|
||||
ligne.append((norme,)*3)
|
||||
result_image.append(ligne)
|
||||
return result_image
|
||||
|
||||
|
||||
def calculGradient(filtred_image):
|
||||
mask_x = [[1, 0, -1]]
|
||||
mask_y = [[1],[0],[-1]]
|
||||
|
||||
mask_gradient_x = convolution(filtred_image, mask_x)
|
||||
mask_gradient_y = convolution(filtred_image, mask_y)
|
||||
|
||||
norme_gradient = copyNullMatrix(filtred_image)
|
||||
angle_normal_gradient = copyNullMatrix(filtred_image)
|
||||
|
||||
for i in range(len(filtred_image)):
|
||||
for j in range(len(filtred_image[0])):
|
||||
Jx = mask_gradient_x[i][j][0]
|
||||
Jy = mask_gradient_y[i][j][0]
|
||||
norme_gradient[i][j] = sqrt(Jx**2 + Jy**2)
|
||||
angle_temp = atan2(Jy,Jx)
|
||||
angle_normal_gradient[i][j] = transform_angle(angle_temp)
|
||||
|
||||
return norme_gradient, angle_normal_gradient
|
||||
|
||||
|
||||
def copyNullMatrix(mat):
|
||||
nullMat = deepcopy(mat)
|
||||
for i in range(len(nullMat)):
|
||||
for j in range(len(nullMat[0])):
|
||||
nullMat[i][j] = 0
|
||||
return nullMat
|
||||
|
||||
|
||||
def transform_angle(radient):
|
||||
angle = radient * 180 / pi
|
||||
if angle < 0:
|
||||
angle += 180
|
||||
|
||||
#On veut que la valeur de l'angle soit 0, 45, 90 ou 135°
|
||||
seuil_min_45 = 45/2
|
||||
seuil_min_90 = (90+45)/2
|
||||
seuil_min_135 = (135+90)/2
|
||||
seuil_max_135 = (180+135)/2
|
||||
|
||||
if seuil_min_45 <= angle < seuil_min_90:
|
||||
angle = 45
|
||||
elif seuil_min_90 <= angle < seuil_min_135:
|
||||
angle = 90
|
||||
elif seuil_min_135 <= angle < seuil_max_135:
|
||||
angle = 135
|
||||
else:
|
||||
angle = 0
|
||||
|
||||
return angle
|
||||
|
||||
|
||||
def dltNoMaxima(norme_gradient, angle_normal_gradient):
|
||||
non_maxima = copyNullMatrix(norme_gradient)
|
||||
|
||||
for i in range(len(non_maxima)):
|
||||
for j in range(len(non_maxima[0])):
|
||||
angle = angle_normal_gradient[i][j]
|
||||
voisin1, voisin2 = norm_voisin(norme_gradient, angle, i, j)
|
||||
if norme_gradient[i][j] < voisin1 or norme_gradient[i][j] < voisin2:
|
||||
non_maxima[i][j] = 0
|
||||
else:
|
||||
non_maxima[i][j] = norme_gradient[i][j]
|
||||
return non_maxima
|
||||
|
||||
|
||||
def get_norm(norm_list, i, j):
|
||||
norm = 0
|
||||
if 0 <= i < len(norm_list) and 0 <= j < len(norm_list[0]):
|
||||
norm = norm_list[i][j]
|
||||
return norm
|
||||
|
||||
|
||||
def norm_voisin(norm_list, angle, i, j):
|
||||
voisin1 = None
|
||||
voisin2 = None
|
||||
|
||||
if angle == 0:
|
||||
voisin1 = get_norm(norm_list,i,j-1)
|
||||
voisin2 = get_norm(norm_list,i,j+1)
|
||||
elif angle == 45:
|
||||
voisin2 = get_norm(norm_list,i-1,j+1)
|
||||
voisin1 = get_norm(norm_list,i+1,j-1)
|
||||
elif angle == 90:
|
||||
voisin1 = get_norm(norm_list,i-1,j)
|
||||
voisin2 = get_norm(norm_list,i+1,j)
|
||||
elif angle == 135:
|
||||
voisin2 = get_norm(norm_list,i-1,j-1)
|
||||
voisin1 = get_norm(norm_list,i+1,j+1)
|
||||
|
||||
return voisin1, voisin2
|
||||
|
||||
|
||||
def seuillageHysteresis(non_maxima, angle_normale_gradient, Th, Tl):
|
||||
contours = deepcopy(non_maxima)
|
||||
|
||||
for i in range(len(angle_normale_gradient)):
|
||||
for j in range(len(angle_normale_gradient[0])):
|
||||
if non_maxima[i][j] > Th:
|
||||
contours[i][j] = (255,)*3
|
||||
elif non_maxima[i][j] < Tl:
|
||||
contours[i][j] = (0,)*3
|
||||
|
||||
|
||||
for i in range(len(angle_normale_gradient)):
|
||||
for j in range(len(angle_normale_gradient[0])):
|
||||
if Tl <= non_maxima[i][j] <= Th:
|
||||
angle = angle_normale_gradient[i][j] + 90
|
||||
if angle >= 180:
|
||||
angle -= 180
|
||||
|
||||
voisin1, voisin2 = norm_voisin(non_maxima, angle, i, j)
|
||||
if voisin1 > Th and voisin2 > Th:
|
||||
contours[i][j] = (255,)*3
|
||||
else:
|
||||
contours[i][j] = (0,)*3
|
||||
return contours
|
||||
|
||||
|
||||
"""
|
||||
def calculTh(norme_gradient, centile):
|
||||
histogramme, pas = calculHistogram(norme_gradient)
|
||||
fonctionRepartition = calculFonctionRepartition(histogramme)
|
||||
|
||||
nbrPixels = len(norme_gradient)*len(norme_gradient[0])
|
||||
pivot = nbrPixels * centile
|
||||
|
||||
Th_index = 0
|
||||
for i in range(len(fonctionRepartition)):
|
||||
if (pivot - fonctionRepartition[0][Th_index]) > (pivot - fonctionRepartition[0][i]) and (pivot - fonctionRepartition[i] > 0):
|
||||
Th_index = i
|
||||
|
||||
Th = pas * (Th_index - 1)
|
||||
return Th
|
||||
|
||||
def calculHistogram(norme_gradient):
|
||||
norme_max = Maxi(norme_gradient)
|
||||
norme_min = Minim(norme_gradient)
|
||||
ecart = norme_max - norme_min
|
||||
|
||||
nb_pas = 1000
|
||||
pas = ecart / (nb_pas - 1)
|
||||
|
||||
histogram = [[0]*nb_pas]
|
||||
|
||||
for i in range(len(norme_gradient)):
|
||||
for j in range(len(norme_gradient[0])):
|
||||
valeur_pixel = norme_gradient[i][j]
|
||||
position = floor((valeur_pixel - norme_min) / pas)
|
||||
histogram[0][position] += 1
|
||||
|
||||
return histogram, pas
|
||||
|
||||
def calculFonctionRepartition(histogram):
|
||||
fonctionRepartition = deepcopy(histogram)
|
||||
for i in range(1, len(fonctionRepartition[0])):
|
||||
fonctionRepartition[0][i] = fonctionRepartition[0][i-1] + histogram[0][i-1]
|
||||
|
||||
return fonctionRepartition
|
||||
|
||||
def Maxi(mat):
|
||||
mx = 0
|
||||
for i in range(len(mat)):
|
||||
for j in range(len(mat[0])):
|
||||
if mat[i][j] > mx:
|
||||
mx = mat[i][j]
|
||||
return mx
|
||||
|
||||
def Minim(mat):
|
||||
mn = 1000
|
||||
for i in range(len(mat)):
|
||||
for j in range(len(mat[0])):
|
||||
if mat[i][j] < mn:
|
||||
mn = mat[i][j]
|
||||
return mn
|
||||
"""
|
Before Width: | Height: | Size: 989 KiB |
Before Width: | Height: | Size: 114 B |
Before Width: | Height: | Size: 186 KiB |
Before Width: | Height: | Size: 98 B |
Before Width: | Height: | Size: 548 KiB |
Before Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 1.4 MiB |
18
imageEngine/main.py
Normal file
@ -0,0 +1,18 @@
|
||||
from tkinter import Tk, N, W, E, S, StringVar
|
||||
from tkinter import ttk
|
||||
|
||||
|
||||
class Window(Tk):
|
||||
|
||||
"""This is the class for the window systeme"""
|
||||
|
||||
def __init__(self, title):
|
||||
Tk.__init__(self)
|
||||
self.title = title
|
||||
|
||||
# TODO: Affichage ici
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
w = Window("test")
|
||||
w.mainloop()
|
Before Width: | Height: | Size: 568 KiB |
Before Width: | Height: | Size: 95 KiB |
Before Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 75 KiB |
Before Width: | Height: | Size: 106 KiB |
Before Width: | Height: | Size: 59 KiB |
Before Width: | Height: | Size: 81 KiB |
Before Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 87 KiB |
Before Width: | Height: | Size: 46 KiB |
Before Width: | Height: | Size: 86 KiB |
Before Width: | Height: | Size: 70 KiB |
Before Width: | Height: | Size: 55 KiB |
Before Width: | Height: | Size: 54 KiB |
Before Width: | Height: | Size: 164 KiB |
Before Width: | Height: | Size: 540 KiB |
Before Width: | Height: | Size: 322 KiB |
Before Width: | Height: | Size: 65 KiB |
Before Width: | Height: | Size: 75 KiB |
Before Width: | Height: | Size: 218 KiB |
Before Width: | Height: | Size: 170 KiB |
Before Width: | Height: | Size: 63 KiB |
Before Width: | Height: | Size: 190 KiB |
Before Width: | Height: | Size: 340 KiB |
Before Width: | Height: | Size: 128 KiB |
Before Width: | Height: | Size: 158 KiB |
Before Width: | Height: | Size: 39 KiB |
Before Width: | Height: | Size: 119 KiB |
Before Width: | Height: | Size: 138 KiB |
Before Width: | Height: | Size: 58 KiB |
Before Width: | Height: | Size: 116 KiB |
Before Width: | Height: | Size: 105 KiB |
Before Width: | Height: | Size: 112 KiB |
Before Width: | Height: | Size: 201 KiB |
Before Width: | Height: | Size: 86 KiB |
Before Width: | Height: | Size: 52 KiB |
Before Width: | Height: | Size: 237 KiB |
Before Width: | Height: | Size: 48 KiB |
Before Width: | Height: | Size: 82 KiB |
Before Width: | Height: | Size: 102 KiB |
Before Width: | Height: | Size: 53 KiB |
Before Width: | Height: | Size: 85 KiB |
Before Width: | Height: | Size: 561 KiB |
Before Width: | Height: | Size: 96 KiB |
Before Width: | Height: | Size: 69 KiB |
Before Width: | Height: | Size: 135 KiB |
Before Width: | Height: | Size: 46 KiB |
Before Width: | Height: | Size: 62 KiB |
Before Width: | Height: | Size: 107 KiB |
Before Width: | Height: | Size: 100 KiB |
Before Width: | Height: | Size: 71 KiB |
Before Width: | Height: | Size: 55 KiB |
Before Width: | Height: | Size: 31 KiB |
Before Width: | Height: | Size: 69 KiB |
Before Width: | Height: | Size: 50 KiB |
Before Width: | Height: | Size: 57 KiB |
Before Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 39 KiB |
Before Width: | Height: | Size: 72 KiB |
Before Width: | Height: | Size: 118 KiB |
Before Width: | Height: | Size: 1.2 MiB |
Before Width: | Height: | Size: 154 KiB |
Before Width: | Height: | Size: 102 KiB |
Before Width: | Height: | Size: 79 KiB |
@ -1,294 +0,0 @@
|
||||
import umage as um
|
||||
from math import sqrt, atan2, sin, cos, pi
|
||||
|
||||
def greyscale(mat_img):
|
||||
gray_img = []
|
||||
for ligne in mat_img:
|
||||
lig = []
|
||||
for r,g,b in ligne:
|
||||
v = int(r*0.2125 + g*0.7154 + b*0.0721)
|
||||
lig.append((v,)*3)
|
||||
gray_img.append(lig)
|
||||
return gray_img
|
||||
|
||||
def convolution(mat_img, mat):
|
||||
return_img = []
|
||||
for j in range(len(mat_img)):
|
||||
ligne = []
|
||||
for i in range(len(mat_img[0])):
|
||||
val = appliquer_convolution(mat_img, mat, i, j)
|
||||
ligne.append((val,)*3)
|
||||
return_img.append(ligne)
|
||||
return return_img
|
||||
|
||||
def filtre_sobel(img):
|
||||
|
||||
def calcul_norme(pixel1, pixel2):
|
||||
valeur = pixel1[0]**2 + pixel2[0]**2
|
||||
norm = round(sqrt(valeur))
|
||||
norm = int(min(norm, 255))
|
||||
return norm
|
||||
|
||||
def application_norme(im_x, im_y):
|
||||
result_image = []
|
||||
for j in range(len(im_x)):
|
||||
ligne = []
|
||||
for i in range(len(im_x[0])):
|
||||
pixel1 = im_x[j][i]
|
||||
pixel2 = im_y[j][i]
|
||||
norme = calcul_norme(pixel1, pixel2)
|
||||
ligne.append((norme,)*3)
|
||||
result_image.append(ligne)
|
||||
return result_image
|
||||
|
||||
if not is_greyscale(img):
|
||||
img = greyscale(img)
|
||||
|
||||
mat_x = [[-1,0,1],[-2,0,2],[-1,0,1]]
|
||||
mat_y = [[-1,-2,-1],[0,0,0],[1,2,1]]
|
||||
Gx = convolution(img, mat_x)
|
||||
Gy = convolution(img, mat_y)
|
||||
|
||||
filtred_image = application_norme(Gx,Gy)
|
||||
return filtred_image
|
||||
|
||||
|
||||
|
||||
#########################################################################
|
||||
########################Exercices Supplémentaires########################
|
||||
#########################################################################
|
||||
|
||||
def is_greyscale(img):
|
||||
_greyscale = True
|
||||
for ligne in img:
|
||||
for r,g,b in ligne:
|
||||
if not (r==g and g==b):
|
||||
_greyscale = False
|
||||
break
|
||||
if not _greyscale:
|
||||
break
|
||||
return _greyscale
|
||||
|
||||
def invert(img):
|
||||
result_image = []
|
||||
for ligne in img:
|
||||
result_ligne = []
|
||||
for r,g,b in ligne:
|
||||
result_ligne.append((255-r, 255-g, 255-b))
|
||||
result_image.append(result_ligne)
|
||||
return result_image
|
||||
|
||||
def pixel(img, i, j, default=(0,0,0)):
|
||||
#i la colone et j la ligne
|
||||
if 0 <= i < len(img[0]) and 0 <= j < len(img):
|
||||
return img[j][i]
|
||||
else:
|
||||
return default
|
||||
|
||||
def appliquer_convolution(img, mat, i, j):
|
||||
somme = 0
|
||||
for y in range(len(mat)):
|
||||
for x in range(len(mat[0])):
|
||||
pixel_i = i - (len(mat[0]) // 2) + x
|
||||
pixel_j = j - (len(mat) // 2) + y
|
||||
pix = pixel(img, pixel_i, pixel_j)
|
||||
somme += pix[0]*mat[y][x]
|
||||
return min(max(somme,0), 255)
|
||||
|
||||
|
||||
|
||||
######################################################################
|
||||
########################Exercices personnelles########################
|
||||
######################################################################
|
||||
def convolution_gauss(mat_img):
|
||||
mat_gauss = [
|
||||
[2/159, 4/159, 5/159, 4/159,2/159],
|
||||
[4/159, 9/159,12/159, 9/159,4/159],
|
||||
[5/159,12/159,15/159,12/159,5/159],
|
||||
[4/159, 9/159,12/159, 9/159,4/159],
|
||||
[2/159, 4/159, 5/159, 4/159,2/159]
|
||||
]
|
||||
|
||||
return_img = []
|
||||
for j in range(len(mat_img)):
|
||||
ligne = []
|
||||
for i in range(len(mat_img[0])):
|
||||
val = reduction_bruit(mat_img, mat_gauss, i, j)
|
||||
ligne.append((val,)*3)
|
||||
return_img.append(ligne)
|
||||
return return_img
|
||||
|
||||
def reduction_bruit(img, mat, i, j):
|
||||
somme = 0
|
||||
for y in range(len(mat)):
|
||||
for x in range(len(mat[0])):
|
||||
pixel_i = i - (len(mat[0]) // 2) + x
|
||||
pixel_j = j - (len(mat) // 2) + y
|
||||
pix = pixel(img, pixel_i, pixel_j)
|
||||
somme += pix[0]*mat[y][x]
|
||||
normalise = round(somme)
|
||||
return normalise
|
||||
|
||||
def filtre_canny(img):
|
||||
|
||||
def norme_gradient(pixel1, pixel2):
|
||||
color_x = pixel1[0]
|
||||
color_y = pixel2[0]
|
||||
|
||||
norm = round(sqrt(color_x**2 + color_y**2))
|
||||
norm = min(norm, 255)
|
||||
|
||||
grad = atan2(color_y, color_x)
|
||||
return norm, grad
|
||||
|
||||
def liste_normGrad(im1, im2):
|
||||
liste = []
|
||||
for j in range(len(im1)):
|
||||
ligne = []
|
||||
for i in range(len(im1[0])):
|
||||
normGrad = norme_gradient(im1[j][i], im2[j][i])
|
||||
ligne.append(normGrad)
|
||||
liste.append(ligne)
|
||||
return liste
|
||||
|
||||
if not is_greyscale(img):
|
||||
img = greyscale(img)
|
||||
|
||||
mat_x = [[-1,0,1]]
|
||||
mat_y = [[1],[0],[-1]]
|
||||
|
||||
#lissage/suppression des bri
|
||||
img_no_bruit = convolution_gauss(img)
|
||||
Jx = convolution(img, mat_x)
|
||||
Jy = convolution(img, mat_y)
|
||||
normGrad = liste_normGrad(Jx, Jy)
|
||||
|
||||
#Suppresion des non-maximum
|
||||
|
||||
|
||||
#temp
|
||||
def norme_gradient(pixel1, pixel2):
|
||||
color_x = pixel1[0]
|
||||
color_y = pixel2[0]
|
||||
|
||||
norm = round(sqrt(color_x**2 + color_y**2))
|
||||
norm = min(norm, 255)
|
||||
|
||||
grad = atan2(color_y, color_x)
|
||||
return norm, grad
|
||||
|
||||
#temp
|
||||
def liste_normGrad(im1, im2):
|
||||
liste = []
|
||||
for j in range(len(im1)):
|
||||
ligne = []
|
||||
for i in range(len(im1[0])):
|
||||
normGrad = norme_gradient(im1[j][i], im2[j][i])
|
||||
ligne.append(normGrad)
|
||||
liste.append(ligne)
|
||||
return liste
|
||||
|
||||
mat_x = [[-1,0,1]]
|
||||
mat_y = [[1],[0],[-1]]
|
||||
#temp
|
||||
#lissage
|
||||
img = um.load("imageEngine\\images\\valve.png")
|
||||
img = convolution_gauss(img)
|
||||
Jx = convolution(img, mat_x)
|
||||
Jy = convolution(img, mat_y)
|
||||
normGrad = liste_normGrad(Jx, Jy)
|
||||
###########
|
||||
|
||||
|
||||
|
||||
def find_neighbord_norm(mat, i, j, rad):
|
||||
x = 0
|
||||
y = 0
|
||||
if sin(pi/8) <= abs(sin(rad)):
|
||||
y = 1
|
||||
if cos(3*pi/8)>abs(cos(rad)):
|
||||
x = 1
|
||||
|
||||
norm_pix1 = -1
|
||||
norm_pix2 = -1
|
||||
if 0 <= j-y < len(mat):
|
||||
if 0 <= i-x < len(mat[0]):
|
||||
norm_pix1 = mat[j-y][i-x][0]
|
||||
if 0 <= j+y < len(mat):
|
||||
if 0 <= i+x < len(mat[0]):
|
||||
norm_pix2 = mat[j+y][i+x][0]
|
||||
|
||||
return norm_pix1, norm_pix2
|
||||
|
||||
def delete_pixel(mat_img, mat):
|
||||
img_to_return = []
|
||||
for j in range(len(mat)):
|
||||
ligne = []
|
||||
for i in range(len(mat[0])):
|
||||
rad = mat[j][i][1]
|
||||
norms = find_neighbord_norm(mat, i, j, rad)
|
||||
if rad < norms[0] or rad < norms[1]:
|
||||
ligne.append((0,)*3)
|
||||
else:
|
||||
ligne.append(mat_img[j][i])
|
||||
img_to_return.append(ligne)
|
||||
return img_to_return
|
||||
|
||||
|
||||
"""
|
||||
def hysteresis(mat_img, mat_norm, Th):
|
||||
Tl = Th / 2
|
||||
mat_img = yesOrNo(mat_img, Th, Tl)
|
||||
result_image = []
|
||||
for j in range(len(mat_img)):
|
||||
ligne = []
|
||||
for i in range(len(mat_img[0])):
|
||||
rad = mat_norm[j][i][1]
|
||||
color1, color2 = find_neighbord_pixel(mat_img, i, j, rad+(pi/2))
|
||||
if color1 == 255 or color2 == 255:
|
||||
ligne.append((255,)*3)
|
||||
else:
|
||||
ligne.append((0,)*3)
|
||||
result_image.append(ligne)
|
||||
return result_image
|
||||
|
||||
def find_neighbord_pixel(mat_image, i, j, rad):
|
||||
x = 0
|
||||
y = 0
|
||||
if sin(pi/8) <= abs(sin(rad)):
|
||||
y = 1
|
||||
if cos(3*pi/8)>abs(cos(rad)):
|
||||
x = 1
|
||||
|
||||
color_pix1 = 0
|
||||
color_pix2 = 0
|
||||
if 0 <= j-y < len(mat_image):
|
||||
if 0 <= i-x < len(mat_image[0]):
|
||||
color_pix1 = mat_image[j-y][i-x][0]
|
||||
if 0 <= j+y < len(mat_image):
|
||||
if 0 <= i+x < len(mat_image[0]):
|
||||
color_pix2 = mat_image[j+y][i+x][0]
|
||||
|
||||
return color_pix1, color_pix2
|
||||
|
||||
def yesOrNo(mat_img, Th, Tl):
|
||||
result_image = []
|
||||
for j in range(len(mat_img)):
|
||||
ligne = []
|
||||
for i in range(len(mat_img[0])):
|
||||
pix = mat_img[j][i]
|
||||
if Th <= pix[0]:
|
||||
ligne.append((255,)*3)
|
||||
elif pix[0] < Tl:
|
||||
ligne.append((0,)*3)
|
||||
else:
|
||||
ligne.append(pix)
|
||||
result_image.append(ligne)
|
||||
return result_image
|
||||
|
||||
|
||||
zt_no_maxima = delete_pixel(img, normGrad)
|
||||
zt_hysteresis = hysteresis(zt_no_maxima, normGrad, 200)
|
||||
|
||||
um.save(zt_hysteresis, "imageEngine\\test\\valve", "png")
|
||||
"""
|
Before Width: | Height: | Size: 392 KiB |
Before Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 480 KiB |
Before Width: | Height: | Size: 56 KiB |
Before Width: | Height: | Size: 312 KiB |
Before Width: | Height: | Size: 215 KiB |