0% found this document useful (0 votes)
3 views13 pages

11 NumPy Image

The document provides Python code for creating and manipulating images using NumPy and Matplotlib, including functions to generate RGB and grayscale images, apply pixel transformations, and process colors. It also includes functions for image convolution, histogram generation, and binarization. Various image processing techniques such as mirroring, rotation, and color component extraction are demonstrated.

Uploaded by

Moatez Bouzayene
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

11 NumPy Image

The document provides Python code for creating and manipulating images using NumPy and Matplotlib, including functions to generate RGB and grayscale images, apply pixel transformations, and process colors. It also includes functions for image convolution, histogram generation, and binarization. Various image processing techniques such as mirroring, rotation, and color component extraction are demonstrated.

Uploaded by

Moatez Bouzayene
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NumPy Image

April 26, 2025

[8]: import numpy as np


import [Link] as plt
import random

1 creation image rgb


[2]: def creerImage( h , w ) :
A = [Link]((h,w,3) , dtype = 'uint8')
for i in range(h) :
for j in range(w) :
A[i,j] = [Link](0,256,size = 3)
return A

[6]: Ic = creerImage( 5 , 10)


[Link](figsize=(3,5))
[Link](Ic)

[6]: <[Link] at 0x275930b6350>

[7]: print(Ic[2,3])

[ 13 253 21]

1
2 Creation image Gris
[10]: def creerImageGris( h , w ) :
A = [Link]((h,w) , dtype = 'uint8')
for i in range(h) :
for j in range(w) :
A[i,j] = [Link](0,255)
return A

[16]: Ig = creerImageGris( 5 , 10)


[Link](figsize=(3,5))
[Link](Ig , cmap = 'gray')

[16]: <[Link] at 0x27593493250>

[14]: print(Ig[2,7])

247

[18]: def echec( N ) :


A = [Link]((8*N , 8*N) , dtype = 'uint8')
bloc = [Link]((N,N))*255
for x in range(8 ) :
for y in range(8) :
if (x+y) % 2 == 1 :
A[ x*N : N*(x+1) , y*N : N*(y+1) ] = bloc
return A

E = echec(10)
[Link](figsize=(3,3))
[Link](E , cmap = 'gray')

[18]: <[Link] at 0x275936c8810>

2
3 Traitement des pixels
[19]: Img = [Link]('[Link]')
[Link](figsize=(3,3))
[Link](Img)

[19]: <[Link] at 0x275935494d0>

[22]: def miroirV( A ) :


h,w,c = [Link]
B = [Link](A)
for j in range(w) :

3
Cj = A[ : , j ]
B[ : , w-j-1] = Cj

return B

def miroirV( A ) :
B = A[ : , : : -1]
return B

E = miroirV(Img)
[Link](figsize=(3,3))
[Link](E)

[22]: <[Link] at 0x27593580810>

[23]: def rotationD( A ) :


h,w,c = [Link]
B = [Link]((w,h,3) , dtype = 'uint8')
for i in range( h ) :
B[ : , -i-1 ] = A[i]
return B

E = rotationD(Img)
[Link](figsize=(3,3))
[Link](E)

[23]: <[Link] at 0x2759524ba10>

4
4 Traitement des couleurs
[24]: def composante_rouge( A ) :
B = [Link](A)
B[ : , : , 1 : 3 ] = 0
return B

E = composante_rouge(Img)
[Link](figsize=(3,3))
[Link](E)

[24]: <[Link] at 0x27595507110>

5
[25]: def composante_vert( A ) :
B = [Link](A)
B[ : , : , ::2 ] = 0
return B

E = composante_vert(Img)
[Link](figsize=(3,3))
[Link](E)

[25]: <[Link] at 0x275935c0810>

[26]: def composante_bleu( A ) :


B = [Link](A)
B[ : , : , :2] = 0
return B

E = composante_bleu(Img)
[Link](figsize=(3,3))
[Link](E)

[26]: <[Link] at 0x275955746d0>

6
[27]: Iorg = composante_bleu(Img) + composante_rouge(Img) + composante_vert(Img)

[Link](figsize=(3,3))
[Link](Iorg)

[27]: <[Link] at 0x275938002d0>

[28]: Iorg = composante_bleu(Img) + composante_rouge(Img)


[Link](figsize=(3,3))
[Link](Iorg)

[28]: <[Link] at 0x2759375c6d0>

7
[29]: def negatif( A ) :
return 255 - A
E = negatif(Img)
[Link](figsize=(3,3))
[Link](E)

[29]: <[Link] at 0x275956753d0>

[32]: def rgb_to_gray(A) :


h,w,c = [Link]
B = [Link]((h,w) , dtype = 'uint8')
Vcoef = [Link]([ 0.3 , 0.6 , 0.1])
for i in range(h):
for j in range(w) :
#B[i,j] = A[i,j,0]*0.3 + ...

8
#B[i,j] = [Link]( A[i,j] * Vcoef )
B[i,j] = [Link]( A[i,j] , Vcoef )
return B

def rgb_to_gray2(A) :
R = A[ : , : , 0] *0.3
V = A[ : , : , 1] *0.6
B = A[ : , : , 2] *0.1

return [Link](R+V+B)

E = rgb_to_gray2(Img)
[Link](figsize=(3,3))
[Link](E , cmap = 'gray')

[32]: <[Link] at 0x275957e9d10>

[40]: def voisinage( A , i , j) :


return A[ i-1 : i+2 , j-1 : j+2 ]

def Set_voisinage( p ) : # p est un pixel = (i,j)


i,j = p
S = {(k,l) for k in range(i-1,i+2) for l in range(j-1,j+2) if p!=(k,l)}
return S

[41]: Set_voisinage((1,4))

[41]: {(0, 3), (0, 4), (0, 5), (1, 3), (1, 5), (2, 3), (2, 4), (2, 5)}

9
[42]: def convolution( A , kernel ) :
B = [Link](A)
for i in range(1 , [Link][0]-1) :
for j in range(1 , [Link][1]-1) :
V = voisinage( A , i , j)
B[i,j] = [Link]( V * kernel )
return B

[43]: kernel_sharpen = [Link]((3,3))*-1


kernel_sharpen[1,1] = 9
Icon = convolution( E , kernel_sharpen)
[Link](figsize=(3,3))
[Link](Icon , cmap = 'gray')

[43]: <[Link] at 0x27597363bd0>

[52]: def histogramme( A ) :


"""retourner V / V[k] = nombre de pixel de ton de gris = k """
V = [Link]( 256 )
for ton in range(256) :
for i in range([Link][0]) :
for j in range([Link][1]) :
if A[i,j] == ton :
V[ton] += 1
return V

# complexité = O(256.h.w)

def histogramme( A ) :
"""retourner V / V[k] = nombre de pixel de ton de gris = k """
V = [Link]( 256 )

10
for ton in range(256) :
B = (A == ton)
V[ton] = [Link](B)

return V

# complexité = O(256.h.w)

def histogramme( A ) :
"""retourner V / V[k] = nombre de pixel de ton de gris = k """
V = [Link]( 256 )
for i in range([Link][0]) :
for j in range([Link][1]) :
k = int( A[i,j] )
V[ k ] += 1
return V

# complexité = O(h.w)

[56]: def tracerHist( A ) :


"""axe des abscisse = niveau de gris
axe des ordonne = nombre de pixel """
Lx = [Link](0 , 256 , 1)
Lx = [Link](0 , 255 , 256)
Ly = histogramme( A )
[Link](figsize=(5,5))
[Link](Lx , Ly)
[Link]()

[57]: print(E[0,0])

110.0

[58]: tracerHist( E )

11
[59]: def binariser( A , seuil ) :
B = (A > seuil)*255
return B

[60]: E = binariser(E , 20)


[Link](figsize=(3,3))
[Link](E , cmap = 'gray')

[60]: <[Link] at 0x275976eced0>

12
[ ]:

13

You might also like