Transformaciones con vecinos
Filtros-Ruido
Taller de inform´atica I
Universidad de Buenos Aires
Temario
1 Filtros
2 Operaciones entre vecinos
3 Ruido y restauraci´on
Operaciones entre vecinos
Modificar los pixels, seg´un una funci´on que involucra a los vecinos
de cada pixel.
Se tiene un pixel a procesar Se selecciona el entorno del
pixel(forma, tama˜no)
Se aplica una m´ascara y una operaci´on
Se modifica el p´ıxel de la imagen de salida equivalente al p´ıxel de la imagen de entrada, por el valor devuelto por la operaci´on
Repetir en cada pixel de la imagen de entrada.
Filtros con vecinos
nlfilter
Max
inline, nlfilter
f u n c t i o n I 1=imMax(I) imagenmax=i n l i n e( ’ max ( x ( : ) ) ’) ; %i n l i n e d e f i n e imagenmax I 1=n l f i l t e r(I , [ 3 , 3 ] ,imagenmax) ; %n l f i l t e r e s o p e r a c i o n e n t r e v e c i n o s de 3 x3 l l a m a d a imagenmax end Im=i m r e a d(’ cameraman . t i f ’) ; S=imMax(Im) imshow(S)Max
inline, nlfilter
f u n c t i o n I 1=imMin(I) imagenmin=i n l i n e( ’ min ( x ( : ) ) ’) ; %i n l i n e d e f i n e imagenmax I 1=n l f i l t e r(I , [ 3 , 3 ] ,imagenmin) ; %n l f i l t e r e s o p e r a c i o n e n t r e v e c i n o s de 3 x3 l l a m a d a imagenmax end Im=i m r e a d(’ cameraman . t i f ’) ; S=imMin(Im) imshow(S)Operar con los vecinos, CORRELACI ´
ON
Para operar con los vecinos se necesita un pixel de la imagen y una
m´ascara, cuyo centro se superpone con el pixel abarcando los
vecinos.
Operaci´on en pixel i,j, CORRELACI ´ON
Operar con los vecinos, CONVOLUCI ´
ON
Para operar con los vecinos se necesita un pixel de la imagen y una
m´ascara, cuyo centro se superpone con el pixel abarcando los
vecinos.
Operaci´on en pixel i,j, CONVOLUCI ´ON
Operar con los vecinos. Ejemplo
Operaci´on en pixel i,j
S(i,j) == 19 ∗106 + 19 ∗104 + 19∗100 + 19∗108 + 19∗99 +19 ∗
IMFILTER: operar con los vecinos
I = i m r e a d( ’ b l o o d 1 . t i f ’) ; h = o n e s( 5 , 5 ) / 2 5 ;%s e n e c e s i t a una m\’ a s c a r a I 2 = i m f i l t e r(I,h) ; %f i l t r a l a i m a g e n con l a m \’ a s c a r a imshow(I) , t i t l e( ’ O r i g i n a l i m a g e ’) f i g u r e , imshow(I 2) , t i t l e(’ F i l t e r e d i m a g e ’)fspecial
fspecialCrea filtros 2-D Predefinidos.
H = fspecial(TYPE) crea un filtro bidimensional H de un tipo espec´ıfico.
Posibles valores para TYPE:
’average’averaging filter
’disk’circular averaging filter
’gaussian’Gaussian lowpass filter
’laplacian’filter approximating the 2-D Laplacian operator
’log’Laplacian of Gaussian filter
’motion’motion filter
’prewitt’Prewitt horizontal edge-emphasizing filter
fspecial, ejemplos
>> f s p e c i a l( ’ a v e r a g e ’ , 3 ) a n s = 0 . 1 1 1 1 0 . 1 1 1 1 0 . 1 1 1 1 0 . 1 1 1 1 0 . 1 1 1 1 0 . 1 1 1 1 0 . 1 1 1 1 0 . 1 1 1 1 0 . 1 1 1 1 >> f s p e c i a l( ’ a v e r a g e ’ , 5 ) a n s = 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0 0 . 0 4 0 0Ejemplo
>>I=i m r e a d( ’ c a l a b a z a . j p g ’) ; >> I=i m r e s i z e(I, 0 . 1 5 , ’ b i l i n e a r ’) ; >> m=f s p e c i a l(’ a v e r a g e ’ , 7 ) ; >> S=i m f i l t e r(I,m) ; >> s u b p l o t( 1 , 2 , 1 ) ,imshow(I) >> s u b p l o t( 1 , 2 , 2 ) ,imshow(S)CORRELACION Y CONVOLUCION
Formalmente:Correlaci´on
g = imfilter(f, w, filtering mode,
boundary options, size options)
donde:
f: imagen original
w: m´ascara
filtering mode: corr o conv
boundary options: symmetric, replicate o circular.
imfilter(A,H,OPTION1,OPTION2,...)
B = imfilter(A,H,OPTION1,OPTION2,...)performs
multidimensional filtering according to the specified options. Option arguments can have the following values:
Filtering mode: Correlation and convolution
corr imfilter performs multidimensional filtering using
correlation, which is the same way that FILTER2 performs filtering.
conv imfilter performs multidimensional filtering using
convolution.
When no correlation or convolution option is specified, imfilter uses correlation.
imfilter(A,H)
imfilter filtra im´agenes N-dimesionales.
B = imfilter(A,H)filtra el array multidimensional A con el filtro multidimensional H.
Acan be logical or it can be a nonsparse numeric array of any
class and dimension.
The result, B, has the same size and class as A.
Each element of the output, B, is computed using double-precision floating point.
If A is an integer or logical array, then output elements that
exceed the range of the given type are truncated,and fractional values are rounded.
imfilter(A,H,OPTION1,OPTION2,...)
B = imfilter(A,H,OPTION1,OPTION2,...)puede tener
argumentos:
Boundary options
X Input array values outside the bounds of the array are implicitly assumed to have the value X. When no boundary option is specified, imfilter uses X = 0.
symmetric Input array values outside the bounds of the array
are computed by mirror-reflecting the array across the array border.
replicate Input array values outside the bounds of the array
are assumed to equal the nearest array border value.
circular Input array values outside the bounds of the array are
imfilter(A,H,OPTION1,OPTION2,...)
B = imfilter(A,H,OPTION1,OPTION2,...)performs
multidimensional filtering according to the specified options. Option arguments can have the following values:
Output size options
same The output array is the same size as the input array.
This is the default behavior when no output size options are specified.
full The output array is the full filtered result, and so is larger
Ejemplo
o r i g i n a l R G B = i m r e a d(’ p e p p e r s . png ’) ; h = f s p e c i a l( ’ m o t i o n ’ , 5 0 , 4 5 ) ; f i l t e r e d R G B = i m f i l t e r(o r i g i n a l R G B ,h) ; s u b p l o t( 1 , 3 , 1 ) , imshow(o r i g i n a l R G B) , s u b p l o t( 1 , 3 , 2 ) , imshow(f i l t e r e d R G B) b o u n d a r y R e p l i c a t e R G B = i m f i l t e r(o r i g i n a l R G B ,h, ’ r e p l i c a t e ’) ; s u b p l o t( 1 , 3 , 3 ) , imshow(b o u n d a r y R e p l i c a t e R G B)Otro Ejemplo: blancos y negros
f=z e r o s( 2 5 6 ) ; f( 1 : 1 2 7 , 1 2 8 : end ) =1; f( 1 2 8 : end , 1 : 1 2 7 ) =1; s u b p l o t( 2 , 3 , 1 ) ,imshow(f , [ ] ) ,t i t l e( ’ o r i g i n a l ’) w=o n e s( 3 1 ) / 3 1 ;Otro Ejemplo
gd = i m f i l t e r(f, w) ; s u b p l o t( 2 , 3 , 2 ) ,imshow( gd, [ ] ) ,t i t l e (’ P a d d i n g 0 ’) g r = i m f i l t e r(f, w, ’ r e p l i c a t e ’) ; s u b p l o t( 2 , 3 , 3 ) , imshow(g r , [ ] ) ,t i t l e (’ r e p l i c a t e ’) g s = i m f i l t e r(f, w, ’ s y m m e t r i c ’) ; s u b p l o t( 2 , 3 , 4 ) ,imshow(gs, [ ] ) , t i t l e(’ s y m m e t r i c ’) gc = i m f i l t e r(f, w, ’ C i r c u l a r ’) ; s u b p l o t( 2 , 3 , 5 ) , imshow(gc, [ ] ) ,t i t l e (’ c i r c u l a r ’) f 8 = i m 2 u i n t 8(f) ; g 8 r = i m f i l t e r(f 8 , w, ’ r e p l i c a t e ’) ;Ruido y Restauraci´
on
Restaurar una imagen cuando est´a fue degradada consiste en:
Identificar el proceso de degradaci´on y revertirlo.
Las fuentes de ruido en una imagen digital se producen durante la adquisici´on, digitalizaci´on) y transmisi´on. Los sensores pueden ser afectados por las condiciones del ambiente.
Durante la transmisi´on puede
Tipos de Ruido
Aditivo: El m´as usual es elgaussiano, que es esencialmente aditivo y la se˜nal independiente, g(z,y) =f(z,y) +∇(z,y)
donde g(z,y) es el resultado de la distorsi´on de imagen
originalf(z,y) por el ruido gaussiano aditivo ∇(z,y)
Impulsivo: Frecuentemente los sensores generan ruido
impulsivo. Algunas veces el ruido generado por transmisi´on
digital (o incluso anal´ogico) es impulsivo. Puede ser modelado
como: g(x,y) = (1−p)∗f(x,y) +p∗i(x,y) donde i(z,y) es el ruido impulsivo y p ∈ {0,1}.
Multiplicativo: el ruido de aspecto granulado de las im´agenes de radares y ecograf´ıas. Es esencialmente multiplicativo. Tiene un aspecto moteado. g(z,y) =f(z,y)∗m(x,y), donde
imnoise()
J = IMNOISE(I,TYPE,...)Adiciona de los siguiente tipos:
gaussian Ruido Gausiano blanco con media y varianza
constante.
localvar Ruido Gausiano Blanco con media cero y varianza.
poisson Ruido con distribuci´on Poisson.
salt & pepperCambia los pixeles a blanco o negro.
Ruido gaussiano
El valor final del pixel es el real m´as una cierta cantidad de error.
Puede describirse como una variable gaussiana que sigue una distribuci´on normal.
P(g(x,y)−σ <f(x,y)<g(x,y) +σ) = 70 %
Ruido Gaussiano
>> I=i m r e a d( ’ t i r e . t i f ’) ; >> R1=i m n o i s e(I, ’ g a u s s i a n ’ , 0 . 3 ) ; >> R2=i m n o i s e(I, ’ g a u s s i a n ’ , 0 . 0 3 ) ; >> s u b p l o t( 1 , 3 , 1 ) ,imshow(I) >> s u b p l o t( 1 , 3 , 2 ) ,imshow(R1) >> s u b p l o t( 1 , 3 , 3 ) ,imshow(R2) >>f i g u r e( ) >>s u b p l o t( 1 , 3 , 1 ) ,b a r(i m h i s t(I) ) >>s u b p l o t( 1 , 3 , 2 ) ,b a r(i m h i s t(R1) ) >>s u b p l o t( 1 , 3 , 3 ) ,b a r(i m h i s t(R2) ) >>Ruido Impulsivo: Sal y Pimienta
El valor que toma el p´ıxel no tiene relaci´on con el valor real sino
que toma valores muy altos o muy bajos.
Toma el valor m´aximo (sal) o el m´ınimo (pimienta). En Matlab el
comando es:
J= imnoise(I, ’salt & pepper’,d)
A˜nade el ruido a la imagen I donde d es la densidad del ruido.
Se afecta aproximadamente a los d*num(I) pixeles. El valor de d por defecto es 0,05.
Ejemplo
Filtros de la Mediana. No lineal
I = i m r e a d( ’ e i g h t . t i f ’) ; J = i m n o i s e(I ,’ s a l t & p e p p e r ’ , 0 . 0 2 ) ; K = m e d f i l t 2(J) ;%f i l t r o de l a m e d i a n a d e f a u l t 3 x3 f i g u r e , imshow(J) , f i g u r e , imshow(K)Otros filtrosno lineales: Max. Min, Moda(el valor de intensidad
Ejercicios
Hacer funciones que dada una imagen de entrada, tenga como salida una que responda a cada requerimiento:
1 Generar una imagen de 2 cuadrados blancos y dos negros, de
512x512. Reproduzca el experimento con distintos bordes y
muestre peque˜nas regiones(por ejemplo, de 5 x 10 sobre alg´un
borde) y muestre cada caso.
2 Contamine una imagen con ruido gaussiano y mit´ıguelo con el
filtro de la media de 3x3, 5x5, 7x7, 11x11. usando imfilter
con distintos par´ametros y modo de filtrado. Compare y
Comente
3 Contamine una imagen con rudio Sal y Pimienta con
distintos porcentajes de contaminaci´on, elimine con
mediana(prueba con max, min, midpoint(es el valor medio entre el mayor y el menor en cada ventana).