Segmentation Using Mathematical Morphology

There are multiple ways to do segmentation. Mathematical morphology is a geometric approach for extracting image components that are useful in the representation and description of region shapes. In this exploration, we illustrate several basic concepts in mathematical morphology, such as erosion, dilation, opening and closing. We will also examine how to apply these simple operators to perform segmentation on medical images.
June 23, 2017—Wenzhen Zhu

Introduction of Segmentation

Our task is separating an object from the background in a grayscale picture. Here is an example.
⟶

Helper Functions

In[]:=
SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[EvaluationNotebook[]],{1},FrontEnd`FileName]]];​​PNGtoGray[filename_]:=Reverse[Map[Map[First,#]&,Import[filename,"Data"]]];​​GIFtoGray[filename_]:=255-Reverse[Map[Map[First,#]&,Import[filename,"Data"]]];​​showGrayImage[img_]:=Graphics[Raster[img/Max[img]],FrameFalse];​​showBinaryImage[img_]:=Graphics[Raster[img],FrameFalse];​​showBinaryOverlay[img_,mask_]:=Graphics[Raster[MapThread[MapThread[If[#21,{1,0,0},{#1,#1,#1}]&,{#1,#2}]&,​​{img,mask}]],FrameFalse];​​showGrayOverlay[img_,mask_]:=Graphics[{Raster[img/Max[img]],Raster[Map[Map[{#,0,0,If[#1,.5,0]}&,#]&,mask]]},FrameFalse];​​toBinary[x_,threshold_]:=If[x>threshold,1,0]​​getBinarizedImage[img_,threshold_]:=(toBinary[#,threshold]&/@((N@img〚#〛/255))&/@(Range[Length[img]]))
In[]:=
kernelFromStructure[structure_]:=Block​​ {size=2Max@Abs@MinMax@structure+1},​​ ReplacePartTable[0,size,size],ThreadAppend[structure,{0,0}]+
size+1
2
1​​
In[]:=
kernelFromStructure[structure_]:=Block​​ {size=2Max@Abs@MinMax@structure+1},​​ ReplacePartTable[0,size,size],ThreadAppend[structure,{0,0}]+
size+1
2
1​​​​​​erode[imageData_,structure_,n_]:=Nest[erode[#,structure]&,imageData,n]​​​​erode[imageData_,structure_]:=Block​​ ​​ kernel=(*Transpose@*)kernelFromStructure[structure],​​ paddedData,neighborhoods,validNeighbors,newPixels​​ ,​​ paddedData=1-ArrayPadimageData,
Length@kernel-1
2
,1;​​ neighborhoods=Partition[paddedData,Table[Length@kernel,2],1];​​ validNeighbors=Map[#*kernel&,neighborhoods,{2}];​​ newPixels=1-Map[Max,validNeighbors,{2}];​​ newPixels​​​​​​dilate[imageData_,structure_,n_]:=Nest[dilate[#,structure]&,imageData,n]​​​​dilate[imageData_,structure_]:=1-erode[1-imageData,structure]​​​​open[imageData_,structure_,r_]:=dilate[erode[imageData,structure,r],structure,r]​​close[imageData_,structure_,r_]:=erode[dilate[imageData,structure,r],structure,r]

Connected Components

Mathematical Morphology

Bone Segmentation

Other Segmentation

FURTHER EXPLORATIONS
Morphology on Grayscale Images Retinal Vessel Segmentation Code:
github.com/zhuwenzhen/Vessel-Segmentation
​ Paper:
Segmentation of Vessel-Like Patterns Using Mathematical Morphology and Curvature Evaluation
Corneal Ulcer Segmentation Project page:
EyeCare
AUTHORSHIP INFORMATION
Wenzhen Zhu
06/23/2017