CS 4495 Computer Vision – A. Bobick Morphology

CS 4495 Computer Vision Binary images and Morphology

Aaron Bobick School of Interactive Computing CS 4495 Computer Vision – A. Bobick Morphology Administrivia

• PS6 – should be working on it! Due Sunday Nov 24th. • Some issues with reading frames. Resolved?

• Exam: Tues November 26th . • Short answer and multiple choice (mostly short answer) • Study guide is posted in calendar. • Bring a pen.

• PS7 – we still hope to have out by 11/26. Will be straight forward implementation of Motion History Images

CS 4495 Computer Vision – A. Bobick Morphology Binary Image Analysis

Binary image analysis • consists of a set of image analysis operations that are used to produce or process binary images, usually images of 0’s and 1’s.

0 represents the background 1 represents the foreground

00010010001000 00011110001000 00010010001000

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Binary Image Analysis

• Is used in a number of practical applications • Part inspection • Manufacturing • Document processing

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology What kinds of operations?

• Separate objects from background and from one another

• Aggregate pixels for each object

• Compute features for each object

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Example: red blood cell image

• Many blood cells are separate objects • Many touch – bad! • Salt and pepper noise from thresholding • How useable is this data?

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Results of analysis

• 63 separate objects detected • Single cells have area about 50 • Noise spots • Gobs of cells

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Useful Operations

• Thresholding a gray-scale image

• Determining good thresholds

• Connected components analysis

• Binary

• All sorts of feature extractors • (area, centroid, circularity, …)

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology

Thresholding

• Background is black • Healthy cherry is bright • Bruise is medium dark • Histogram shows two cherry regions (black background has been removed)

pixel counts

0 gray-scale values 255 Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology

Histogram-Directed Thresholding

How can we use a histogram to separate an image into 2 (or several) different regions?

Is there a single clear threshold? 2? 3?

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Automatic Thresholding: Otsu’s Method

Grp 1 Grp 2 Assumption: the histogram is bimodal

t Method: find the threshold t that minimizes the weighted sum of within-group variances for the two groups that result from separating the gray tones at value t.

In practice, this operator works very well for true bimodal distributions and not too badly for others.

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Thresholding Example

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Connected Components Labeling

Once you have a binary image, you can identify and then analyze each connected set of pixels.

The connected components operation takes in a binary image and produces a labeled image in which each pixel has the integer label of either the background (0) or a component.

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Methods for CC Analysis

1. Recursive Tracking (almost never used)

2. Parallel Growing (needs parallel hardware)

3. Row-by-Row (most common)

• Classical Algorithm

• Efficient Run-Length Algorithm (developed for speed in real industrial applications)

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Equivalent Labels

Original Binary Image 0 0 0 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 0 0 0 1 1 1 1 0 0 0 1 1 1 1 0 0 0 1 1 0 0 0 1 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Equivalent Labels

CC =0 Scan across rows: If 1 and connected, 0 0 0 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 Propgate lowest label behind or above (4 0 0 0 1 1 1 1 0 0 0 1 1 1 1 0 0 0 1 1 or 8 connected). 0 0 0 1 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 Remember conflicts 0 0 0 1 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 If 1 and not connected 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 CC++ and label CC 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 If 0, label 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Relabel based on 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 table 0 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Equivalent Labels

CC =0 Scan across rows: If 1 and connected, 0 0 0 1 1 1 0 0 0 0 2 2 2 2 0 0 0 0 3 Propgate lowest label behind or above (4 or 8 connected). 0 0 0 1 1 1 1 0 0 0 2 2 2 2 0 0 0 3 3 Remember conflicts 0 0 0 1 1 1 1 1 0 0 2 2 2 2 0 0 3 3 3 If 1 and not connected 0 0 0 1 1 1 1 1 1 0 2 2 2 2 0 0 3 3 3 CC++ and label CC 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 3 3 3 If 0, label 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 3 3 3 Relabel based on 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 table 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1

1 ≡ 2 1 ≡ 3 Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Equivalent Labels

The Labeling Process

0 0 0 1 1 1 0 0 0 0 2 2 2 2 0 0 0 0 3 1 ≡ 2 0 0 0 1 1 1 1 0 0 0 2 2 2 2 0 0 0 3 3 1 ≡ 3 0 0 0 1 1 1 1 1 0 0 2 2 2 2 0 0 3 3 3 0 0 0 1 1 1 1 1 1 0 2 2 2 2 0 0 3 3 3 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 3 3 3 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 3 3 3 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology

Labeling shown as Pseudo-Color

connected components of 1’s from thresholded image

connected components of cluster labels

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Mathematical Morphology

Binary mathematical morphology consists of two basic operations

and

and several composite relations

and hit-or-miss transformation . . .

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Dilation

Dilation expands the connected sets of 1s of a binary image.

It can be used for

1. growing features

2. filling holes and gaps

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Erosion

Erosion shrinks the connected sets of 1s of a binary image.

It can be used for

1. shrinking features

2. Removing bridges, branches and small protrusions

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Structuring Elements

A is a shape mask used in the basic morphological operations.

They can be any shape and size that is digitally representable, and each has an origin.

box disk hexagon something

box(length,width) disk(diameter)

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Dilation with Structuring Elements

The arguments to dilation and erosion are 1. a binary image B 2. a structuring element S

dilate(B,S) takes binary image B, places the origin of structuring element S over each 1-pixel, and ORs the structuring element S into the output image at the corresponding position.

0 0 0 0 dilate 0 1 1 0 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 S B origin B ⊕ S Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Binary text example

1 1 1 1 1

Original SE S Dilated by S

Slide by Ioannis Ivrissimtzis CS 4495 Computer Vision – A. Bobick Morphology Erosion with Structuring Elements

erode(B,S) takes a binary image B, places the origin of structuring element S over every pixel position, and ORs a binary 1 into that position of the output image only if every position of S (with a 1) covers a 1 in B. (Can also use zeros and “don’t cares”)

origin 0 0 0 0 0 0 0 1 1 0 1 erode 0 0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 0 0 1 1 0 1 1 1 1 1 1 0 0 0 0 0 B S B S

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology Effect of disk size on erosion

Original image Erosion Erosion Erosion with a disk with a disk with a disk of radius 5 of radius 10 of radius 20

Slide by Ioannis Ivrissimtzis CS 4495 Computer Vision – A. Bobick Morphology Opening and Closing

• Opening is the compound operation of erosion followed by dilation (with the same structuring element) • Can show that the opening of A by B is the union of all translations of B that fit entirely within A. • Opening is idempotent: Repeated operations has no further effects!

• Closing is the compound operation of dilation followed by erosion (with the same structuring element) • Can show that the closing of A by B is the complement of union of all translations of B that do not overlap A. • Closing is idempotent: Repeated operations has no further effects!

CS 4495 Computer Vision – A. Bobick Morphology Opening

Binary image A Translations of The opening and structuring element B. B that fit of A by B is entirely within shown shaded. A.

Intuitively, the opening is the area we can paint when the brush has a footprint B and we are not allowed to paint outside A.

Slide by Ioannis Ivrissimtzis CS 4495 Computer Vision – A. Bobick Morphology Opening example – cell colony

• Use large structuring element that fits into the big objects • Structuring Element: 11 pixel disc

Slide by Thomas Moeslund CS 4495 Computer Vision – A. Bobick Morphology Closing

Binary image A Translations of The closing of and structuring element B. B that do not A by B is overlap A. shown shaded.

Intuitively, the closing is the area we can not paint when the brush has a footprint B and we are not allowed to paint inside A.

Slide by Ioannis Ivrissimtzis CS 4495 Computer Vision – A. Bobick Morphology Closing Example - Segmentation

• Simple segmentation: 1. Threshold 2. Closing with disc of size 20

Slide by Thomas Moeslund CS 4495 Computer Vision – A. Bobick Morphology Applications

• The opening of an image by a disk-like structuring element can be used to smooth contours, break narrow isthmuses, and eliminate small islands and sharp peaks.

• The closing of an image by a disk-like structuring element can be used to smooth contours, fuse narrow breaks and long thin gulfs and eliminate small holes.

Slide by Ioannis Ivrissimtzis CS 4495 Computer Vision – A. Bobick Morphology Toy example

Original Opening image

Closing Opening following by closing A 20x20 square structural element was used. Slide by Ioannis Ivrissimtzis CS 4495 Computer Vision – A. Bobick Morphology Real Example

Original Opening Opening image following by closing

A 3x3 square structural element was used.

Slide by Ioannis Ivrissimtzis CS 4495 Computer Vision – A. Bobick Morphology The Hit-or-Miss Transformation

• The morphological hit-or-miss transform is a basic tool for shape detection or pattern matching. • Let B denote the set composed of X and its background.

B = (B1, B2), where B1=X, B2=W-X.

• The match of B in A, denoted by A * B, is c A * B = (A X ) [A (W − X )]

36 CS 4495 Computer Vision – A. Bobick Morphology The Hit-or-Miss Transformation

Other interpretation: c A * B = (A B1) [A B2 ]

ˆ A * B = (A B1) − (A B2 )

37 CS 4495 Computer Vision – A. Bobick Morphology Some Basic Morphological Algorithms

• Boundary extraction • Region filling • Extraction of connected components • Convex Hull • Thinning • Skeletons • Pruning

CS 4495 Computer Vision – A. Bobick Morphology Boundary extraction

Let A ⊕ B denotes the dilation of A by B and let A - B denotes the erosion of A by B.

The boundary of A can be computed as

A - ( A - B )

where be is a 3x3 square structuring element.

That is, we subtract from A an erosion of it to obtain its boundary.

Slide by Ioannis Ivrissimtzis CS 4495 Computer Vision – A. Bobick Morphology Example of boundary extraction CS 4495 Computer Vision – A. Bobick Morphology Thinning

A⊗ B = A − (A∗ B) = A∩ (A∗ B)c CS 4495 Computer Vision – A. Bobick Morphology Thickening

A• B = A∪ (A∗ B)

42 CS 4495 Computer Vision – A. Bobick Morphology How powerful is morphology

• If depends…

• If almost “clean” binary images then very powerful to both clean up images and to detect variations from desired image.

• Example… CS 4495 Computer Vision – A. Bobick Morphology Gear Tooth Inspection

original binary image

How did they do it?

detected defects

Slide by Linda Shapiro CS 4495 Computer Vision – A. Bobick Morphology CS 4495 Computer Vision – A. Bobick Morphology Geometric and Shape Properties

• area • centroid • perimeter • perimeter length • circularity • elongation • mean and standard deviation of radial distance • bounding box • extremal axis length from bounding box • second order moments (row, column, mixed) • lengths and orientations of axes of best-fit ellipse

Slide by Linda Shapiro