Operating on binary images: morphological operators

Load binary test image

img = ReadImage["binary_tst.ppm"] /. 1255 ; scale = 2 ;

Display image

ShowImage[img, scale] ;

[Graphics:../HTMLFiles/index_13.gif]

Median filtering

tmp = MedianFilter[img, 2] ; ShowImage[tmp, scale] ;

[Graphics:../HTMLFiles/index_15.gif]

Morphological operators

Dilation operator

Structural element definition

s = Table[1, {3}, {3}] ; s//MatrixForm

( 1   1   1 )            1   1   1            1   1   1

First dilation

g1 = ShowImage[img, 2, 255, 0, DisplayFunctionIdentity] ; g2 = ShowImage[tmp = Dilatio ... 4;Identity] ; Show[GraphicsArray[{g1, g2}], ImageSizeLast[Dimensions[img]] * scale * 2] ;

[Graphics:../HTMLFiles/index_19.gif]

Second dilation

g2 = ShowImage[tmp2 = Dilation[tmp, s], 2, 255, 0, DisplayFunctionIdentity] ; Show[GraphicsArray[{g1, g2}], ImageSizeLast[Dimensions[img]] * scale * 2] ;

[Graphics:../HTMLFiles/index_21.gif]

Erosion operator

Structural element definition

s = Table[1, {3}, {3}] ; s//MatrixForm

( 1   1   1 )            1   1   1            1   1   1

First erosion

g1 = ShowImage[img, 2, 255, 0, DisplayFunctionIdentity] ; g2 = ShowImage[tmp = Erosion ... 4;Identity] ; Show[GraphicsArray[{g1, g2}], ImageSizeLast[Dimensions[img]] * scale * 2] ;

[Graphics:../HTMLFiles/index_25.gif]

Second erosion

g2 = ShowImage[tmp2 = Erosion[tmp, s], 2, 255, 0, DisplayFunctionIdentity] ; Show[GraphicsArray[{g1, g2}], ImageSizeLast[Dimensions[img]] * scale * 2] ;

[Graphics:../HTMLFiles/index_27.gif]

Closing operator (dilation followed by erosion)

Structural element definition

s = Table[1, {3}, {3}] ; s//MatrixForm

( 1   1   1 )            1   1   1            1   1   1

Closing

g1 = ShowImage[img, 2, 255, 0, DisplayFunctionIdentity] ; g2 = ShowImage[tmp = Closing ... 4;Identity] ; Show[GraphicsArray[{g1, g2}], ImageSizeLast[Dimensions[img]] * scale * 2] ;

[Graphics:../HTMLFiles/index_31.gif]

Opening operator (erosion followed by dilation)

Structural element definition

s = Table[1, {3}, {3}] ; s//MatrixForm

( 1   1   1 )            1   1   1            1   1   1

Opening

g1 = ShowImage[img, 2, 255, 0, DisplayFunctionIdentity] ; g2 = ShowImage[tmp = Opening ... 4;Identity] ; Show[GraphicsArray[{g1, g2}], ImageSizeLast[Dimensions[img]] * scale * 2] ;

[Graphics:../HTMLFiles/index_35.gif]

Larger structural element definition

s = Table[1, {5}, {5}] ; s//MatrixForm

( 1   1   1   1   1 )            1   1   1   1   1            1   1   1   1   1            1   1   1   1   1            1   1   1   1   1

Opening

g1 = ShowImage[img, 2, 255, 0, DisplayFunctionIdentity] ; g2 = ShowImage[tmp = Opening ... 4;Identity] ; Show[GraphicsArray[{g1, g2}], ImageSizeLast[Dimensions[img]] * scale * 2] ;

[Graphics:../HTMLFiles/index_39.gif]

Connected-component labeling after two morphological operators

Connected components in original image

tmp = BlobFind[img] ; ShowImage[BlobImage[tmp], scale] ; BlobCount[tmp]

[Graphics:../HTMLFiles/index_41.gif]

9

Connected components following Closing operator

s = Table[1, {3}, {3}] ; m1 = Closing[img, s] ;

tmp = BlobFind[m1] ; ShowImage[BlobImage[tmp], scale] ; BlobCount[tmp]

[Graphics:../HTMLFiles/index_45.gif]

6

Connected components following (1) Closing and then (2) Opening operator

m2 = Opening[m1, s] ;

tmp = BlobFind[m2] ; ShowImage[BlobImage[tmp], scale] ; BlobCount[tmp]

[Graphics:../HTMLFiles/index_49.gif]

3


Created by Mathematica  (February 5, 2004)