0% found this document useful (0 votes)
17 views2 pages

Correction Traitement

Uploaded by

SOULOX3
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)
17 views2 pages

Correction Traitement

Uploaded by

SOULOX3
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
You are on page 1/ 2

CPGE : Reda Slaoui Année scolaire : 2023/2024

Correction TP : Traitement d’images

1 from s c i p y import misc


2 import numpy a s np
3 import m a t p l o t l i b . p y p l o t a s p l t
4
5
6 M = misc . f a c e ( gray=True )
7 p l t . imshow (M, cmap=p l t . cm . gray )
8
9
10 d e f i n v e r s e (M) :
11 n , m = M. shape
12 Mc = np . z e r o s ( ( n ,m) )
13 f o r i in range (n) :
14 f o r j i n r a n g e (m) :
15 Mc [ i , j ] = 255−M[ i , j ]
16 r e t u r n Mc
17
18 d e f l u m i n o s i t e (M, v ) :
19 n , m = M. shape
20 Mc = np . z e r o s ( ( n ,m) )
21 f o r i in range (n) :
22 f o r j i n r a n g e (m) :
23 i f M[ i , j ]+v > 2 5 5 :
24 Mc [ i , j ] = 255
25 e l i f M[ i , j ]+v < 0 :
26 Mc [ i , j ] = 0
27 else :
28 Mc [ i , j ] = M[ i , j ] + v
29 r e t u r n Mc
30

31 d e f moyenne (M) :
32 n , m = M. shape
33 s = 0
34 f o r i in range (n) :
35 f o r j i n r a n g e (m) :
36 s += M[ i , j ]
37 r e t u r n s / ( n∗m)
38
39 d e f s e u i l l a g e (M) :
40 n , m = M. shape
41 Mc = np . z e r o s ( ( n ,m) )
42 S = moyenne (M)

1
43 f o r i in range (n) :
44 f o r j i n r a n g e (m) :
45 i f M[ i , j ] < S :
46 Mc [ i , j ] = 0
47 else :
48 Mc [ i , j ] = 255
49 r e t u r n Mc
50

51 d e f o c c u r r e n c e s (M) :
52 C = [0]∗256
53 n , m = M. shape
54 f o r i in range (n) :
55 f o r j i n r a n g e (m) :
56 C [M[ i , j ] ] += 1
57 return C
58
59 d e f histogramme (C) :
60 H = []
61 f o r k in range (256) :
62 H. append ( ( k , C [ k ] ) )
63 return H
64
65 d e f normal (H) :
66 s = 0
67 f o r x i n H:
68 s += x [ 1 ]
69 r e t u r n [ ( x [ 0 ] , x [ 1 ] / s ) f o r x i n H]
70
71 d e f e n t r o p i e (P) :
72 e = 0
73 f o r k i n r a n g e ( l e n (P) ) :
74 i f P[ k ] [ 1 ] > 0:
75 e −= P [ k ] [ 1 ] ∗ np . l o g 2 (P [ k ] [ 1 ] )
76 return e
77
78
79 d e f cumul (P) :
80 D = { 0 :P [ 0 ] [ 1 ] }
81 f o r k i n r a n g e ( 1 , l e n (P) ) :
82 D[ k ] = D[ k −1] + P [ k ] [ 1 ]
83 return D

You might also like