Segmentation Clean-up
For cleaning up your stuff
Overview
As we saw in the previous module, automatic thresholding can often get us most of the way to segmenting a region of interest, but often leaves some part of the region of interest unsegmented.
Fortunately, there are easy ways to clean this up, which is what this module is about.
Things you should know
-
How to use Morphological Operations to clean up the masks
-
Calculate quantities, such as area and length, using image masks and simple mathematical operations
-
Convert calculated quantities into real-world measurements
-
Calculate the region properties of connected components using the Image Region Analyzer tool or the
regionprops
function
Important Terminology
- Morphological Operations: Image processing that works on Regions. Examples include removing small regions from an image (noise clean-up) or changing the shapes of the regions by, for example, making them fatter or skinnier.
- Region Properties: The properties of said regions. Examples include area, count, roundness, etc.
- 4- or 8 -Connected Neighborhoods: Refers to the Method of determining whether two pixels are contiguous (touching). Are their sides touching (4-connected, more restrictive)? Or are their corners touching too (8-connected, less restrictive)?
Stuff you should read
Functions you should know
- bwmorph - Perform a select set of morphological operations on an image
- bwareaopen - Remove small objects from binary image
- bwareafilt - Extract objects from binary image by size
- imfill - Fill image regions and holes
Morphological Operations
Once we have created a mask, it is often useful to clean up the mask using Morphological Operations. As the name implies, morphological operations operate on the shape of a mask. These operations can change the shape of a mask or, in some cases, remove the mask entirely.
These operations depend on the concept of "touching" or "connected pixels." So, what does it mean to say that pixels are "connected"? Consider the following two diagrams:
Pixel Connectivity. Left Panel. In a 4-connected neighborhood, only pixels that share an edge (horizontally or vertically) are considered touching. In this definition, the Red pixels touch the white pixel, while the gray pixels do not. A 4-connected neighborhood is a more stringent definition of touching. Right Panel. In an 8-connected neighborhood, pixels that touch an edge or a corner are considered touching. In the above figure, all of the red pixels are considered to be touching the white pixel. This is the default setting.
A collection of touching pixels is known by many names: connected components, blobs, objects, or even regions. And you can have more than one blob per image mask, which are known as non-contiguous blobs.
Basic Morphological Operations
The image processing toolbox offers many functions that manipulate connected-components based on their size, shape, or location.
These operations work using a Structuring Element, which is like a mini-mask with a specified shape like a disk or a diamond
Morphological Operations on masks can be summarized as follows
- Dilate: Expand the mask and shrink holes in the mask
- Erode: Shrink the mask and expand holes in the mask
- Open: Erosion followed by dilation. Remove masks objects smaller than the structuring element
- Close: Dilation followed by erosion. This removes holes in the mask smaller than the structuring element
For example, the most basic operations make masks thicker (aka "dilate"):
Left Panel. Original Mask. Right Panel. Dilated Mask
…or thinner (aka "erosion")
Left Panel. Original Mask. Right Panel. Eroded Mask
Common matlab functions that we will use include:
bwareaopen
- used to clean up small pixel clusters.bwmorph
- a general morphologic operations toolbwareafilt
- filter out connected-components by sizeimfill
- When used with the 'holes' input, fills any gaps (or holes) in the connected componentsimclearborder
- remove connected components that touch the edges of the image
Morph Ops Example
To clean up the mask of our moon, we could use the following morphological operations:
Here we display the incremental steps in the Segmentation and Mask Clean-up process. To organize all of the incrementally changing masks, we package everything in structure,
p
, which then allows us to easily create a FOR LOOP that displaying all of the images. img. The original image. mask. The thresholded image (a binary image) generated usingimbinarize
. edges. The mask afterbwmorph
with a 'close' second input. fill. The mask afterimfill
. ms_nz. The mask after a `bwareaopen.
Notice in the code that for each the morphological operation, we input the output from the previous step. So, we are in effect daisy chain morphological operations. This is a very common procedure as each morphological operation typically only affects one aspect of the mask.