Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras....

60
Capítulo 6: Procesamiento morfológico [email protected]

Transcript of Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras....

Page 1: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Capiacutetulo 6 Procesamiento

morfoloacutegico

carlosplateroupmes

Procesamiento Morfoloacutegico

Necesidad del post-procesado

Morfologiacutea Geometriacutea y forma

Conocimiento a priori

Teoriacutea de Conjuntos

No lineal

Aplicaciones varias Procesado deteccioacuten de bordes

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Procesamiento Morfoloacutegico

Nociones sobre la Teoriacutea de Conjuntos Inclusioacuten

Interseccioacuten

Unioacuten

YpoXppYX |

YpyXppYX |

XpYpXY Conjunto

totalmente ordenado

reflexiva

antisimeacutetrica

transitiva

conmutativa asociativa e idempotente

Operaciones sobre los conjuntos(12)

Operaciones sobre conjuntos

Extensiva

Antiextensiva

Idempotente

Elemento estructurante

Geometria y tamantildeo

XX

XX

XX

Operaciones sobre los conjuntos(12)

Erosioacuten

Minimizar

XBxX xB |

XXB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythreshold(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk2)

gtgt imgEroBW = imerode(imgBWse)

gtgt imshow([imgBWimgEroBW])

Ejercicio 1

Para la siguiente figura con 8 niveles de grises de cuantificacioacuten obtener

1 Determinar el umbral de segmentacioacuten

2 Sobre la imagen binarizada aplicar una erosioacuten con un elemento estructurante cuadrado de 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 1

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Operaciones sobre los conjuntos(22)

Dilatacioacuten

Maximizar

xB BXxX |

XX B

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk5)

gtgt imgDilBW = imdilate(imgBWse)

gtgt imshow([imgBWimgDilBW])

Operaciones sobre los conjuntos(22)

Apertura

Eliminar objetos pequentildeos

XX BBB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythres(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk2)

gtgtimgEroBW = imerode(imgBWse)

gtgtimgOpenBW = imopen(imgBWse)

gtgtimshow([imgBWimgEroBW imgOpenBW])

Operaciones sobre los conjuntos(22)

Cierre

Cerrar grietas

XX BBB

XXXXX BBBBBB

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk3)

gtgtimgDilBW = imdilate(imgBWse)

gtgtimgCloseBW = imclose(imgBWse)

gtgtimshow([imgBWimgDilBW imgCloseBW])

Ejemplo con elemento estructurante 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 2: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Procesamiento Morfoloacutegico

Necesidad del post-procesado

Morfologiacutea Geometriacutea y forma

Conocimiento a priori

Teoriacutea de Conjuntos

No lineal

Aplicaciones varias Procesado deteccioacuten de bordes

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Procesamiento Morfoloacutegico

Nociones sobre la Teoriacutea de Conjuntos Inclusioacuten

Interseccioacuten

Unioacuten

YpoXppYX |

YpyXppYX |

XpYpXY Conjunto

totalmente ordenado

reflexiva

antisimeacutetrica

transitiva

conmutativa asociativa e idempotente

Operaciones sobre los conjuntos(12)

Operaciones sobre conjuntos

Extensiva

Antiextensiva

Idempotente

Elemento estructurante

Geometria y tamantildeo

XX

XX

XX

Operaciones sobre los conjuntos(12)

Erosioacuten

Minimizar

XBxX xB |

XXB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythreshold(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk2)

gtgt imgEroBW = imerode(imgBWse)

gtgt imshow([imgBWimgEroBW])

Ejercicio 1

Para la siguiente figura con 8 niveles de grises de cuantificacioacuten obtener

1 Determinar el umbral de segmentacioacuten

2 Sobre la imagen binarizada aplicar una erosioacuten con un elemento estructurante cuadrado de 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 1

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Operaciones sobre los conjuntos(22)

Dilatacioacuten

Maximizar

xB BXxX |

XX B

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk5)

gtgt imgDilBW = imdilate(imgBWse)

gtgt imshow([imgBWimgDilBW])

Operaciones sobre los conjuntos(22)

Apertura

Eliminar objetos pequentildeos

XX BBB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythres(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk2)

gtgtimgEroBW = imerode(imgBWse)

gtgtimgOpenBW = imopen(imgBWse)

gtgtimshow([imgBWimgEroBW imgOpenBW])

Operaciones sobre los conjuntos(22)

Cierre

Cerrar grietas

XX BBB

XXXXX BBBBBB

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk3)

gtgtimgDilBW = imdilate(imgBWse)

gtgtimgCloseBW = imclose(imgBWse)

gtgtimshow([imgBWimgDilBW imgCloseBW])

Ejemplo con elemento estructurante 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 3: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Procesamiento Morfoloacutegico

Nociones sobre la Teoriacutea de Conjuntos Inclusioacuten

Interseccioacuten

Unioacuten

YpoXppYX |

YpyXppYX |

XpYpXY Conjunto

totalmente ordenado

reflexiva

antisimeacutetrica

transitiva

conmutativa asociativa e idempotente

Operaciones sobre los conjuntos(12)

Operaciones sobre conjuntos

Extensiva

Antiextensiva

Idempotente

Elemento estructurante

Geometria y tamantildeo

XX

XX

XX

Operaciones sobre los conjuntos(12)

Erosioacuten

Minimizar

XBxX xB |

XXB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythreshold(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk2)

gtgt imgEroBW = imerode(imgBWse)

gtgt imshow([imgBWimgEroBW])

Ejercicio 1

Para la siguiente figura con 8 niveles de grises de cuantificacioacuten obtener

1 Determinar el umbral de segmentacioacuten

2 Sobre la imagen binarizada aplicar una erosioacuten con un elemento estructurante cuadrado de 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 1

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Operaciones sobre los conjuntos(22)

Dilatacioacuten

Maximizar

xB BXxX |

XX B

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk5)

gtgt imgDilBW = imdilate(imgBWse)

gtgt imshow([imgBWimgDilBW])

Operaciones sobre los conjuntos(22)

Apertura

Eliminar objetos pequentildeos

XX BBB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythres(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk2)

gtgtimgEroBW = imerode(imgBWse)

gtgtimgOpenBW = imopen(imgBWse)

gtgtimshow([imgBWimgEroBW imgOpenBW])

Operaciones sobre los conjuntos(22)

Cierre

Cerrar grietas

XX BBB

XXXXX BBBBBB

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk3)

gtgtimgDilBW = imdilate(imgBWse)

gtgtimgCloseBW = imclose(imgBWse)

gtgtimshow([imgBWimgDilBW imgCloseBW])

Ejemplo con elemento estructurante 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 4: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Operaciones sobre los conjuntos(12)

Operaciones sobre conjuntos

Extensiva

Antiextensiva

Idempotente

Elemento estructurante

Geometria y tamantildeo

XX

XX

XX

Operaciones sobre los conjuntos(12)

Erosioacuten

Minimizar

XBxX xB |

XXB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythreshold(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk2)

gtgt imgEroBW = imerode(imgBWse)

gtgt imshow([imgBWimgEroBW])

Ejercicio 1

Para la siguiente figura con 8 niveles de grises de cuantificacioacuten obtener

1 Determinar el umbral de segmentacioacuten

2 Sobre la imagen binarizada aplicar una erosioacuten con un elemento estructurante cuadrado de 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 1

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Operaciones sobre los conjuntos(22)

Dilatacioacuten

Maximizar

xB BXxX |

XX B

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk5)

gtgt imgDilBW = imdilate(imgBWse)

gtgt imshow([imgBWimgDilBW])

Operaciones sobre los conjuntos(22)

Apertura

Eliminar objetos pequentildeos

XX BBB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythres(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk2)

gtgtimgEroBW = imerode(imgBWse)

gtgtimgOpenBW = imopen(imgBWse)

gtgtimshow([imgBWimgEroBW imgOpenBW])

Operaciones sobre los conjuntos(22)

Cierre

Cerrar grietas

XX BBB

XXXXX BBBBBB

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk3)

gtgtimgDilBW = imdilate(imgBWse)

gtgtimgCloseBW = imclose(imgBWse)

gtgtimshow([imgBWimgDilBW imgCloseBW])

Ejemplo con elemento estructurante 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 5: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Operaciones sobre los conjuntos(12)

Erosioacuten

Minimizar

XBxX xB |

XXB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythreshold(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk2)

gtgt imgEroBW = imerode(imgBWse)

gtgt imshow([imgBWimgEroBW])

Ejercicio 1

Para la siguiente figura con 8 niveles de grises de cuantificacioacuten obtener

1 Determinar el umbral de segmentacioacuten

2 Sobre la imagen binarizada aplicar una erosioacuten con un elemento estructurante cuadrado de 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 1

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Operaciones sobre los conjuntos(22)

Dilatacioacuten

Maximizar

xB BXxX |

XX B

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk5)

gtgt imgDilBW = imdilate(imgBWse)

gtgt imshow([imgBWimgDilBW])

Operaciones sobre los conjuntos(22)

Apertura

Eliminar objetos pequentildeos

XX BBB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythres(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk2)

gtgtimgEroBW = imerode(imgBWse)

gtgtimgOpenBW = imopen(imgBWse)

gtgtimshow([imgBWimgEroBW imgOpenBW])

Operaciones sobre los conjuntos(22)

Cierre

Cerrar grietas

XX BBB

XXXXX BBBBBB

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk3)

gtgtimgDilBW = imdilate(imgBWse)

gtgtimgCloseBW = imclose(imgBWse)

gtgtimshow([imgBWimgDilBW imgCloseBW])

Ejemplo con elemento estructurante 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 6: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejercicio 1

Para la siguiente figura con 8 niveles de grises de cuantificacioacuten obtener

1 Determinar el umbral de segmentacioacuten

2 Sobre la imagen binarizada aplicar una erosioacuten con un elemento estructurante cuadrado de 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 1

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Operaciones sobre los conjuntos(22)

Dilatacioacuten

Maximizar

xB BXxX |

XX B

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk5)

gtgt imgDilBW = imdilate(imgBWse)

gtgt imshow([imgBWimgDilBW])

Operaciones sobre los conjuntos(22)

Apertura

Eliminar objetos pequentildeos

XX BBB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythres(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk2)

gtgtimgEroBW = imerode(imgBWse)

gtgtimgOpenBW = imopen(imgBWse)

gtgtimshow([imgBWimgEroBW imgOpenBW])

Operaciones sobre los conjuntos(22)

Cierre

Cerrar grietas

XX BBB

XXXXX BBBBBB

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk3)

gtgtimgDilBW = imdilate(imgBWse)

gtgtimgCloseBW = imclose(imgBWse)

gtgtimshow([imgBWimgDilBW imgCloseBW])

Ejemplo con elemento estructurante 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 7: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejercicio 1

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 1 1 1 1 1 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Operaciones sobre los conjuntos(22)

Dilatacioacuten

Maximizar

xB BXxX |

XX B

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk5)

gtgt imgDilBW = imdilate(imgBWse)

gtgt imshow([imgBWimgDilBW])

Operaciones sobre los conjuntos(22)

Apertura

Eliminar objetos pequentildeos

XX BBB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythres(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk2)

gtgtimgEroBW = imerode(imgBWse)

gtgtimgOpenBW = imopen(imgBWse)

gtgtimshow([imgBWimgEroBW imgOpenBW])

Operaciones sobre los conjuntos(22)

Cierre

Cerrar grietas

XX BBB

XXXXX BBBBBB

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk3)

gtgtimgDilBW = imdilate(imgBWse)

gtgtimgCloseBW = imclose(imgBWse)

gtgtimshow([imgBWimgDilBW imgCloseBW])

Ejemplo con elemento estructurante 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 8: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Operaciones sobre los conjuntos(22)

Dilatacioacuten

Maximizar

xB BXxX |

XX B

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgt se = strel(disk5)

gtgt imgDilBW = imdilate(imgBWse)

gtgt imshow([imgBWimgDilBW])

Operaciones sobre los conjuntos(22)

Apertura

Eliminar objetos pequentildeos

XX BBB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythres(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk2)

gtgtimgEroBW = imerode(imgBWse)

gtgtimgOpenBW = imopen(imgBWse)

gtgtimshow([imgBWimgEroBW imgOpenBW])

Operaciones sobre los conjuntos(22)

Cierre

Cerrar grietas

XX BBB

XXXXX BBBBBB

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk3)

gtgtimgDilBW = imdilate(imgBWse)

gtgtimgCloseBW = imclose(imgBWse)

gtgtimshow([imgBWimgDilBW imgCloseBW])

Ejemplo con elemento estructurante 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 9: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Operaciones sobre los conjuntos(22)

Apertura

Eliminar objetos pequentildeos

XX BBB

Resolucioacuten Matlab

gtgtimgEnt=imread(ricepng)imshow(imgEnt)pause

gtgtumbral=graythres(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk2)

gtgtimgEroBW = imerode(imgBWse)

gtgtimgOpenBW = imopen(imgBWse)

gtgtimshow([imgBWimgEroBW imgOpenBW])

Operaciones sobre los conjuntos(22)

Cierre

Cerrar grietas

XX BBB

XXXXX BBBBBB

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk3)

gtgtimgDilBW = imdilate(imgBWse)

gtgtimgCloseBW = imclose(imgBWse)

gtgtimshow([imgBWimgDilBW imgCloseBW])

Ejemplo con elemento estructurante 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 10: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Operaciones sobre los conjuntos(22)

Cierre

Cerrar grietas

XX BBB

XXXXX BBBBBB

Resolucioacuten Matlab

gtgtimgEnt=imread(coinspng)imshow(imgEnt)pause

gtgtumbral=graythresh(imgEnt)

gtgtimgBW=im2BW(imgEnt umbral)

gtgtse = strel(disk3)

gtgtimgDilBW = imdilate(imgBWse)

gtgtimgCloseBW = imclose(imgBWse)

gtgtimshow([imgBWimgDilBW imgCloseBW])

Ejemplo con elemento estructurante 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 11: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo con elemento estructurante 3x3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 12: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Morfologiacutea en niveles de grises

Erosioacuten y dilatacioacuten son operaciones crecientes

Una funcioacuten puede ser vista como una pila de conjuntos

decrecientes gfgfSi

gfgfSi

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 13: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Operaciones sobre funciones

Erosioacuten y dilatacioacuten

Apertura y cierre

Byyxff

Byyxff

B

B

max

min

Erosioacuten y dilatacioacuten en niveles de grises

Apertura y cierre en niveles de grises

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 14: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejercicio2

Dada la siguiente imagen realizar una apertura

morfoloacutegica con un elemento estructurante cuadrado de

3X3

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211 1 1 1 0 1 1 1 1 1 0

1 1 2 2 2 2 2 2 1 0

1 1 2 2 2 2 2 2 1 1

2 2 3 6 6 6 2 2 1 1

2 2 3 6 6 6 3 1 1 1

2 2 3 6 6 6 3 0 1 1

2 2 2 5 5 5 3 2 1 1

1 1 2 3 3 3 3 2 1 0

1 1 1 3 3 3 3 2 1 0

0 1 1 1 1 1 1 1 1 0

1 1 0 0 0 1 1 1 0 0

1 1 0 0 0 1 1 1 0 0

1 1 1 2 2 2 2 1 0 0

1 1 1 2 2 2 1 1 1 1

2 2 2 3 6 2 0 0 0 1

2 2 2 2 5 3 0 0 0 1

1 1 1 2 3 3 0 0 0 0

1 1 1 1 3 3 2 1 0 0

0 0 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 0 0

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 15: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 16: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Gradientes morfoloacutegicos(12)

El residuo de dos operaciones y es su diferencia

X X X

fff

Gradientes morfoloacutegicos o de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

XXXg

XXXg

XXXg

BB

B

B

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 17: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Gradientes morfoloacutegicos(22)

El residuo de dos operaciones y es su diferencia

fff Gradiente morfoloacutegico de Beucher

simeacutetrico gradiente

dilatacioacutenpor gradiente

erosioacutenpor gradiente

fffg

fffg

fffg

BB

B

B

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 18: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Top-Hat

Encontrar aquellas estructuras eliminadas en la apertura o

en el cierre

Top-hat en niveles de grises

fffXXX

fffXXX

BBBBBB

BBBBBB

imshow([imgEntimgEnt-imgOpenGrisimgCloseGris-imgEnt])

imshow([imgBW imgCloseBW- imgBW imgBW -imgOpenBW])

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 19: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Utilidades de las operaciones

Erosioacuten (Antiextensiva)

Binaria Disminuir y eliminar objetos maacutes pequentildeos que el elemento estructurante

Funciones La imagen es maacutes oscura

Dilatacioacuten (Extensiva) Binaria Aumentar de tamantildeo a los objetos

Funciones La imagen es maacutes brillante

Apertura (Idempotente)

Binaria eliminar objetos maacutes pequentildeos que el elemento estructurante recuperando parcialmente la forma de los objetos no eliminados

Funciones eliminar objetos brillantes maacutes pequentildeos que el elemento estructurante

Cierre (Idempotente)

Binaria Cerrar las grietas y agujeros de los objetos

Funciones eliminar objetos oscuros maacutes pequentildeos que el elemento estructurante

Gradiente morfoloacutegico (residuo) Binaria Determinar las fronteras de los objetos

Funciones Detector de bordes

Top-hat (residuo) Descubrir los objetos eliminados por la apertura o por el cierre

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 20: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 21: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

0111211110

1223333111

0123553211

1123556222

2306676622

1116677622

1122666323

1122352211

0122333311

1131220211

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 22: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejercicio 3

iquestCoacutemo segmentariacutea la carretera de la imagen aeacuterea

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 23: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejercicio de examen(experimental)

Dada la imagen lsquoricepngrsquo obtener mediante teacutecnicas de procesamiento

morfoloacutegico las imaacutegenes de las figuras El objetivo es eliminar la falta de

iluminacioacuten uniforme presente en la imagen de partida mediante dos

estrategias distintas

Estrategia nordm 1 Estrategia nordm 2

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 24: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejercicio de examen(experimental)

Estrategia nordm 1 Estrategia nordm 2

imIn=imread(ricepng)

imOp=imopen(imInstrel(disk5))

imTH=imIn-imOp

figure

imshow([imInimTH])

imOp=imopen(imInstrel(disk15))

imTH=imIn-imOp

figure

imshow([imInimTH])

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 25: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Transformaciones geodeacutesicas

Distancia geodeacutesica

Siempre seraacute mayor o igual a la eucliacutedea

Disco geodeacutesico

Dilatacioacuten geodeacutesica

Unitaria

zydyxdzxd

yxyxd

xydyxd

XXX

X

XX

0

YyyBY XX

XYYX

| X XB z y d z y z X y X

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 26: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Dilatacioacuten geodeacutesica y la reconstruccioacuten

maacutescara

marcador

XY

XXXYYY Xn

nX

YYR i

XX

YY i

X

i

X

1

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 27: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

Dada la imagen de la izquierda lsquonodulosjpgrsquo describir un algoritmo que a partir de esta imagen determine una imagen binaria donde soacutelo aparecen los noacutedulos cuyo radio tiene una longitud inferior a 9 piacutexeles y un aacuterea mayor a 20 piacutexeles (la imagen de la derecha mostrariacutea el resultado final sobre la imagen de entrada) Acompaacutentildeese con comando en seudo-matlab Se adjunta el histograma de la imagen de entrada

0

1000

2000

3000

4000

5000

6000

7000

0 50 100 150 200 250

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 28: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Resolucioacuten imgEnt = imread(nodulosjpg)

imgBW = im2bw(imgEntgraythresh(imgEnt))

imgBW2 =bwareaopen(imgBW==020)

imgErode=imerode(imgBW2strel(disk9))

imgOpenMej = imreconstruct(imgErodeimgBW2)

imgSal = imgOpenMej==0 amp imgBW2

figure(1)

imshow([imgBWimgBW2imgSal])

imgAux=imgEnt

imgAux(imdilate(bwperim(imgSal)strel(square3)))=255

figure(2)

imshow([padarray(imgEnt[5 5]255)

padarray(imgAux[5 5]255)])

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 29: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Erosioacuten geodeacutesica

YX

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

XYYX

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 30: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Dilatacioacuten geodeacutesica y la reconstruccioacuten

a) Imagen umbralizada (mascara) b) marcador c)Reconstruccioacuten geodeacutesica d)AperturaEliminacioacuten de objetos en el borde por reconstruccioacuten geodesica

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 31: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Relleno

BW2 = IMFILL(BW1holes) fills holes in the

input image A hole is a set of background

pixels that cannot be reached by filling in the

background from the edge of the image

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 32: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Relleno

a)Imagen umbralizada de las monedas b)Relleno mediante reconstruccioacuten

I2 = IMFILL(I1) fills holes in an intensity image I1 In this

case a hole is an area of dark pixels surrounded by lighter pixels

a)Imagen de entrada b)Imagen con eliminacioacuten de partiacuteculas oscuras

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 33: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo deteccioacuten de ceacutelula

A) B) C)

D) E) F)

Explicar el algoritmo de segmentacioacuten de la ceacutelula en cada fase

A) B) C) D) E) y F) junto con su seudo-coacutedigo en Matlab

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 34: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo deteccioacuten de ceacutelula

original image

Image courtesy of Alan PartinJohns Hopkins Univ ersity

binary gradient mask dilated gradient mask

binary image with filled holes cleared border image segmented image

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 35: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo deteccioacuten de ceacutelula

D)

A) B) C)

E) F)

Step A Read Image

I = imread(celltif)

Step B Detect Entire Cell

[junk threshold] = edge(I sobel)

fudgeFactor = 5

BWs = edge(Isobel threshold fudgeFactor)

Step C Dilate the Image

se90 = strel(line 3 90)

se0 = strel(line 3 0)

BWsdil = imdilate(BWs [se90 se0])

Step D Fill Interior Gaps

BWdfill = imfill(BWsdil holes)

Step E Remove Connected Objects on Border

BWnobord = imclearborder(BWdfill 4)

Step F Smoothen the Object

seD = strel(diamond1)

BWfinal = imerode(BWnobordseD)

BWfinal = imerode(BWfinalseD)

figure imshow(BWfinal) title(segmented image)

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 36: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Dilatacioacuten geodeacutesica en funciones

min

ggg

fgg

ffffn

nf

f

a)Original b)Apertura claacutesica y c)Apertura con reconstruccioacuten

Resolucioacuten Matlab

imgMascara=imread(cameramantif)

se = strel(disk3)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imgOpen = imopen(imgMascarase)

imshow([imgMascara imgOpenimgReconst])

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 37: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

a)Entrada b)Realzada y como mascara c)Reconstruccioacuten

Resolucioacuten Matlab

imgEnt = imread(snowflakespng)

imgMascara = adapthisteq(imgEnt)

se = strel(disk5)

imgMarcador = imerode(imgMascarase)

imgReconst = imreconstruct(imgMarcadorimgMascara)

imshow([imgEntimgMascaraimgReconst])

min

ggg

fgg

ffffn

nf

f

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 38: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

a) b) c) d) e)

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 39: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

a) b) c) d) e)

imPro=histeq(imgEnt)

imOpen =imreconstruct(imerode(imProstrel(disk5))imPro)

topHat = imPro-imOpen

segm = im2bw(topHatgraythresh(topHat))

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 40: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Erosioacuten geodeacutesica en funciones

Complementaria

El marcador se erosiona hasta el limite de la maacutescara

Iterando llega a coincidir con los miacutenimos de la funcioacuten

fggf max

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 41: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Regiones maacuteximas

Reconstruccioacuten por dilatacioacuten

Regioacuten maacutexima Se define regioacuten maacutexima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores menores a t

YY i

X

i

X

1

YYR i

XX

cfRffMax f gtgt imgEnt = imread(glasspng)

gtgt imgRegMaxGris = imgEnt-imreconstruct(imgEnt-50imgEnt)

gtgt imgRegMax=imextendedmax(I50)

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 42: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

regiones miacutenimasimagen original

Regiones miacutenimas

Reconstruccioacuten por erosioacuten

Regioacuten miacutenima Se define regioacuten miacutenima a un grupo de piacutexeles con conectividad

entre siacute de forma que todos tienen el mismo valor t y los piacutexeles externos a la regioacuten tienen valores mayores a t

YY i

X

i

X

1

YYR i

XX

fcfRfMin f

gtgt imgEnt = imread(glasspng)

gtgt imgRegMinGris = imreconstruct(imgEntimgEnt+30)-imgEnt

gtgt imgRegMin=imextendedmin(I30)

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 43: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 44: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 45: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 46: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Esqueleto por influencia geodeacutesica SKIZ

Zona de influencia y esqueleto

Watershed

jXiXiX BpdBpdijkjXpBIZ 1|

k

i

iXX BIZBIZ1

X XSKIZ B X IZ B

fRMINSKIZfWS f

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 47: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Watershed bw

Distance transform of ~bw

D = bwdist(~bw)

D = -D

D(~bw) = -Inf

L = watershed(D)

Watershed transform of D

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 48: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejercicio

Dada la imagen de ceacutelulas lsquon1bmprsquo describir un algoritmo que a partir de esta imagen determine las ceacutelulas uacutenicas completas y aquellas que esteacuten solapadas Se sabe que el aacuterea de una ceacutelula es superior a 200 piacutexeles y menor a 1400 Sugiera ademaacutes coacutemo separar las ceacutelulas solapadas Escriba tambieacuten seudo-coacutedigo de Matlab

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 49: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Resolucioacuten

imgIn = imread(n1bmp)

imgInRes = imgIn(20end5end)

imBW = imgInRes gt 55

imBW_CB = imclearborder(bwareaopen(imBW200))

imBWSolap = bwareaopen(imBW_CB1400)

imBWUnicas = (imBWSolap == 0) amp imBW_CB

clf

figure(1)

subplot(221)imshow(imgInRes)title(Imagen de entrada)

subplot(222)imhist(imgInRes)title(Histograma)

se=strel(disk1)

imgAux=imgInResbwborde=imdilate(bwperim(imBWUnicas)se)imgAux(bwborde)=255

subplot(223)imshow(imgAux)title(Ceacutelulas uacutenicas)

imgAux=imgInResbwborde=imdilate(bwperim(imBWSolap)se)imgAux(bwborde)=255

subplot(224)imshow(imgAux)title(Ceacutelulas solapadas)

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 50: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Resolucioacuten

D = bwdist(~imBWSolap) D = -D D(~imBWSolap) = -Inf L = watershed(D) rgb = label2rgb(Ljet[5 5 5]) imshow(rgb)

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 51: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Sobre-segmentacioacuten Por el ruido hay muchos miacutenimos locales

Se requiere una eleccioacuten cuidadosa de las componentes o regiones miacutenimas

Marcador Mascara

Watershed a)Sin procesar b)Con procesamiento

Resolucioacuten Matlab

imgEnt = imread(liftingbodypng)

se = strel(disk5)

imgBWMarcador= imfill(imdilate(edge(imgEntcanny21)se)hole)

imgMascara=imfilter(imgEntfspecial(log51))

imgMascara(imgBWMarcador==0)=0

imshow([label2rgb(watershed(imgEnt))label2rgb(watershed(imgMascara))])

Segmentacioacuten mediante watershed

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 52: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 53: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

rgb = imread(pearspng) I = rgb2gray(rgb)

hy = fspecial(sobel) hx = hy Iy = imfilter(double(I) hy

replicate) Ix = imfilter(double(I) hx replicate) gradmag

= sqrt(Ix^2 + Iy^2)

L = watershed(gradmag) Lrgb = label2rgb(L) Watershed transform of gradient magnitude (Lrgb)Gradient magnitude (gradmag)

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 54: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

se = strel(disk 20) Io = imopen(I se)

Ioc = imclose(Io se)

Ie = imerode(I se) Iobr = imreconstruct(Ie I)

Opening-closing (Ioc) Opening-closing by reconstruction (Iobrcbr)

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 55: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo

fgm = imregionalmax(Iobrcbr)

Opening-closing by reconstruction (Iobrcbr) Regional maxima of opening-closing by reconstruction (fgm)

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 56: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Marcadores

bw = im2bw(Iobrcbr graythresh(Iobrcbr))

D = bwdist(bw) DL = watershed(D) bgm = DL == 0

Thresholded opening-closing by reconstruction (bw) Watershed ridge lines (bgm)

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 57: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo de watershed

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

gradmag2 = imimposemin(gradmag bgm | fgm4)

L = watershed(gradmag2)

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 58: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo de watershed

Image courtesy of Corel

Regional maxima superimposed on original image (I2)

Markers and object boundaries superimposed on original image (I4)

Gradient magnitude (gradmag)

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 59: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Ejemplo de watershed

Image courtesy of Corel

Colored watershed label matrix (Lrgb)

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed

Page 60: Capítulo 6: Procesamiento morfológico - elai.upm.es · morfológico las imágenes de las figuras. El objetivo es eliminar la falta de iluminación uniforme presente en la imagen

Cuestiones

1 Erosioacuten y dilatacioacuten en conjuntos y en funciones

2 Operaciones de apertura y cierre

3 Operaciones de residuos

4 Coacutemo eliminar los objetos que tocan el borde en las imaacutegenes binarias

5 Coacutemo rellenar agujeros en imaacutegenes binarias

6 Queacute es el algoritmo de watershed