diff --git a/imageEngine/__pycache__/umage.cpython-311.pyc b/imageEngine/__pycache__/umage.cpython-311.pyc index 7bc6f62..c07f66a 100644 Binary files a/imageEngine/__pycache__/umage.cpython-311.pyc and b/imageEngine/__pycache__/umage.cpython-311.pyc differ diff --git a/imageEngine/images/lena.png b/imageEngine/images/lena.png new file mode 100644 index 0000000..9760430 Binary files /dev/null and b/imageEngine/images/lena.png differ diff --git a/imageEngine/images/valve.png b/imageEngine/images/valve.png new file mode 100644 index 0000000..79ee8d5 Binary files /dev/null and b/imageEngine/images/valve.png differ diff --git a/imageEngine/re_serie_7.py b/imageEngine/re_serie_7.py index 7bbe91d..1ca1744 100644 --- a/imageEngine/re_serie_7.py +++ b/imageEngine/re_serie_7.py @@ -1,5 +1,5 @@ import umage as um -from math import sqrt, atan2 +from math import sqrt, atan2, sin, cos, pi def greyscale(mat_img): gray_img = [] @@ -100,12 +100,20 @@ def appliquer_convolution(img, mat, i, j): ###################################################################### ########################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 = [] for j in range(len(mat_img)): ligne = [] 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) return_img.append(ligne) return return_img @@ -118,8 +126,8 @@ def reduction_bruit(img, mat, i, j): pixel_j = j - (len(mat) // 2) + y pix = pixel(img, pixel_i, pixel_j) somme += pix[0]*mat[y][x] - normalise = int(round(somme / (1/159))) - return min(max(normalise,0), 255) + normalise = round(somme) + return normalise def filtre_canny(img): @@ -128,7 +136,7 @@ def filtre_canny(img): color_y = pixel2[0] norm = round(sqrt(color_x**2 + color_y**2)) - norm = int(min(norm, 255)) + norm = min(norm, 255) grad = atan2(color_y, color_x) return norm, grad @@ -146,29 +154,140 @@ def filtre_canny(img): if not is_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_y = [[1],[0],[-1]] - #lissage - img = convolution_gauss(img, mat_gauss) + #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) -image = um.load("my_images\\Zero_Two_1.jpeg") -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] - ] -image = convolution_gauss(image, mat_gauss) -um.save(image, "test\\zero_two", "png") + #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") diff --git a/imageEngine/test/valve.png b/imageEngine/test/valve.png new file mode 100644 index 0000000..f422ef2 Binary files /dev/null and b/imageEngine/test/valve.png differ diff --git a/imageEngine/test/zt_bruit.png b/imageEngine/test/zt_bruit.png new file mode 100644 index 0000000..64345df Binary files /dev/null and b/imageEngine/test/zt_bruit.png differ diff --git a/imageEngine/test/zt_hysteresis.png b/imageEngine/test/zt_hysteresis.png new file mode 100644 index 0000000..be4cd7c Binary files /dev/null and b/imageEngine/test/zt_hysteresis.png differ diff --git a/imageEngine/test/zt_no_bruit.png b/imageEngine/test/zt_no_bruit.png new file mode 100644 index 0000000..7073128 Binary files /dev/null and b/imageEngine/test/zt_no_bruit.png differ diff --git a/imageEngine/test/zt_no_maxima.png b/imageEngine/test/zt_no_maxima.png new file mode 100644 index 0000000..2c63275 Binary files /dev/null and b/imageEngine/test/zt_no_maxima.png differ diff --git a/new2.jpg b/new2.jpg new file mode 100644 index 0000000..18f6d22 Binary files /dev/null and b/new2.jpg differ