Many things added

This commit is contained in:
Andy K 2022-11-06 13:34:55 +01:00
parent 5535417e9f
commit 8c64e5215b
10 changed files with 144 additions and 25 deletions

BIN
imageEngine/images/lena.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 548 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

View File

@ -1,5 +1,5 @@
import umage as um import umage as um
from math import sqrt, atan2 from math import sqrt, atan2, sin, cos, pi
def greyscale(mat_img): def greyscale(mat_img):
gray_img = [] gray_img = []
@ -100,12 +100,20 @@ def appliquer_convolution(img, mat, i, j):
###################################################################### ######################################################################
########################Exercices personnelles######################## ########################Exercices personnelles########################
###################################################################### ######################################################################
def convolution_gauss(mat_img, mat): 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 = [] return_img = []
for j in range(len(mat_img)): for j in range(len(mat_img)):
ligne = [] ligne = []
for i in range(len(mat_img[0])): for i in range(len(mat_img[0])):
val = reduction_bruit(mat_img, mat, i, j) val = reduction_bruit(mat_img, mat_gauss, i, j)
ligne.append((val,)*3) ligne.append((val,)*3)
return_img.append(ligne) return_img.append(ligne)
return return_img return return_img
@ -118,8 +126,8 @@ def reduction_bruit(img, mat, i, j):
pixel_j = j - (len(mat) // 2) + y pixel_j = j - (len(mat) // 2) + y
pix = pixel(img, pixel_i, pixel_j) pix = pixel(img, pixel_i, pixel_j)
somme += pix[0]*mat[y][x] somme += pix[0]*mat[y][x]
normalise = int(round(somme / (1/159))) normalise = round(somme)
return min(max(normalise,0), 255) return normalise
def filtre_canny(img): def filtre_canny(img):
@ -128,7 +136,7 @@ def filtre_canny(img):
color_y = pixel2[0] color_y = pixel2[0]
norm = round(sqrt(color_x**2 + color_y**2)) norm = round(sqrt(color_x**2 + color_y**2))
norm = int(min(norm, 255)) norm = min(norm, 255)
grad = atan2(color_y, color_x) grad = atan2(color_y, color_x)
return norm, grad return norm, grad
@ -146,29 +154,140 @@ def filtre_canny(img):
if not is_greyscale(img): if not is_greyscale(img):
img = greyscale(img) img = greyscale(img)
mat_gauss = [
[2, 4, 5, 4,2],
[4, 9,12, 9,4],
[5,12,15,12,5],
[4, 9,12, 9,4],
[2, 4, 5, 4,2]
]
mat_x = [[-1,0,1]] mat_x = [[-1,0,1]]
mat_y = [[1],[0],[-1]] mat_y = [[1],[0],[-1]]
#lissage #lissage/suppression des bri
img = convolution_gauss(img, mat_gauss) img_no_bruit = convolution_gauss(img)
Jx = convolution(img, mat_x) Jx = convolution(img, mat_x)
Jy = convolution(img, mat_y) Jy = convolution(img, mat_y)
normGrad = liste_normGrad(Jx, Jy) normGrad = liste_normGrad(Jx, Jy)
image = um.load("my_images\\Zero_Two_1.jpeg") #Suppresion des non-maximum
mat_gauss = [
[2, 4, 5, 4,2],
[4, 9,12, 9,4], #temp
[5,12,15,12,5], def norme_gradient(pixel1, pixel2):
[4, 9,12, 9,4], color_x = pixel1[0]
[2, 4, 5, 4,2] color_y = pixel2[0]
]
image = convolution_gauss(image, mat_gauss) norm = round(sqrt(color_x**2 + color_y**2))
um.save(image, "test\\zero_two", "png") 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")

BIN
imageEngine/test/valve.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 480 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 KiB

BIN
new2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB