Está en la página 1de 3

Laboratorio Sergio Mora

Laboratorio 2 PDI Muestreo


Profesor Sergio Mora

i m p o r t numpy a s np
from cv2 i m p o r t imread
import m a t p l o t l i b . p y p l o t as p l t

! wget h t t p s : / /www . d r o p b o x . com/ s /2 q a d i i d n n u 8 2 v e 9 / m a n d r i l . png


! wget h t t p s : / /www . d r o p b o x . com/ s / x c k 9 t d n t 1 k x z a h 8 / s c a l e . png

d e f h o w i s ( img ) :
p r i n t ( ’ s i z e = ’ , img . s h a p e )
p r i n t ( ’ max = ’ , np . max ( img ) )
p r i n t ( ’ min = ’ , np . min ( img ) )

s t i m g = ’ m a n d r i l . png ’
s t i m g = ’ s c a l e . png ’

img = i m r e a d ( s t i m g )
X = img [ : , : , 0 ]
p l t . f i g u r e ( f i g s i z e =(8 ,8))
p l t . imshow (X , cmap=’ g r a y ’ )
p l t . show ( )

Mustreo Espacial

d = 16 # se muestrea cada d p i x e l e s
( Nx , Mx) = X . s h a p e
i x = r a n g e ( 0 , Nx , d )
j x = r a n g e ( 0 , Mx , d )
Ny = l e n ( i x )
My = l e n ( j x )
Y = np . z e r o s ( ( Ny , My ) , np . u i n t 8 )
f o r i i n r a n g e ( Ny ) :
f o r j i n r a n g e (My ) :
Y[ i , j ] = X[ i x [ i ] , jx [ j ] ]
p l t . f i g u r e ( f i g s i z e =(8 ,8))
p l t . imshow (Y , cmap=’ g r a y ’ )
p l t . show ( )
h o w i s (Y)

d = 8 # se muestrea cada d p i x e l e s
( Nx , Mx) = X . s h a p e
Ny = Nx
My = Mx
Y = np . z e r o s ( ( Ny , My ) , np . u i n t 8 )
f o r i i n r a n g e ( Ny ) :
f o r j i n r a n g e (My ) :
i x = i n t ( np . f i x ( i / d ) ∗ d )
j x = i n t ( np . f i x ( j / d ) ∗ d )

2023
Y[ i , j ] = X[ ix , jx ]
p l t . f i g u r e ( f i g s i z e =(8 ,8))
p l t . imshow (Y , cmap=’ g r a y ’ )
p l t . show ( )
h o w i s (Y)
p r i n t ( ’ d e s p l e g a d o como = ’ , i n t ( Nx/ d ) , ’ x ’ , i n t ( Nx/ d ) )

Definición de histograma

d e f i m h i s t (X , n = 2 5 6 ) :
(N ,M) = X . s h a p e
h = np . z e r o s ( ( n , ) )
f o r i i n r a n g e (N ) :
f o r j i n r a n g e (M) :
x = X[ i , j ]
h [ x ] = h [ x ]+1
return h

n = 256
h = i m h i s t (Y , n=n )
p l t . f i g u r e ( f i g s i z e =(12 ,8))
p l t . plot ( range (n ) , h [ 0 : n ] )
p l t . show ( )

Muestreo en tonos de gris (Cuantización)

p = 32 # s e m u e s t r e a e l t o n o de g r i s c a d a p v a l o r e s de g r i s
( Nx , Mx) = X . s h a p e
Ny = Nx
My = Mx
Y = np . z e r o s ( ( Ny , My ) , np . u i n t 8 )
f o r i i n r a n g e ( Ny ) :
f o r j i n r a n g e (My ) :
x = i n t ( np . f i x (X [ i , j ] / p ) ∗ p )
Y[ i , j ] = x
p l t . f i g u r e ( f i g s i z e =(8 ,8))
p l t . imshow (Y , cmap=’ g r a y ’ )
p l t . show ( )
h o w i s (Y)
p r i n t ( ’ e s t a i m a g e n t i e n e = ’ , i n t ( 2 5 6 / p ) , ’ t o n o s de g r i s ’ )

n = 256
h = i m h i s t (Y , n=n )
p l t . f i g u r e ( f i g s i z e =(12 ,8))
p l t . plot ( range (n ) , h [ 0 : n ] )
p l t . show ( )

print (h [0:256])

Actividades
1. Consulte, explique y comente que están realizando los bloques del código suministrado.

2
2. En el muestreo espacial, ¿Cuál es la función de la variable d? ¿Qué ocurre cuando cambia el valor a 8, 32,
64? ¿Cuál es el valor máximo al que lo puede modificar?

3. En el muestreo en tonos de gris, ¿Qué función cumple la variable p? ¿Qué ocurre si cambia su valor a 16,
64, 128?¿Hasta que valor puede variar p?
4. Interprete los resultados de cada gráfica obtenida. Presente los resultados con su debida interpretación.

También podría gustarte