You are on page 1of 23

Lecture 06:

Operations on Binary Images


BME 234: Neuroimaging Data Analysis, Spring 2016, Course Code 14310

Frithjof Kruggel, M.D.


Department of Biomedical Engineering, University of California, Irvine
Office: REC 204, Phone: 4-3729, Email: fkruggel@uci.edu

First Prev Next Last Go Back Full Screen Close Quit

Operations on Binary Images


Binary images are digital images in which intensities have only two levels:
{0, 1}. Binary images are often obtained as intermediate results of image
processing chains. A set of special operators for binary images will be discussed in this lecture:

Thresholding
Edge detection
Detection of connected components
Morphological operators
Skeletonization
Distance transformation
Let us define foreground as I(x, y) = 1 (printed black) and background as:
I(x, y) = 0 (printed white).

First Prev Next Last Go Back Full Screen Close Quit

Connectivity
Discrete images have several definitions of neighboring voxels that depend
on the dimensionality. For 1D images, there is only N2 (the previous and
following voxel). For 2D images, we have N4 (edge-connected voxels) and
N8 (edge and corner-connected voxels). For 3D images, distinguish N6 , N18 ,
and N26 .

First Prev Next Last Go Back Full Screen Close Quit

Compatible Neighborhoods
For a continuous image we expect that a closed curve separates an inner from
an outer region. This is different for digital (discrete) images. Adopting the
same connectivity for foreground and background leads to a paradoxon:

This paradoxon is avoided if different definitions are used for foreground


and background. In 2D, typically an N8 neighborhood is adopted:
V81 (i, j) = {(h, k)kD8 ((i, j), (h, k)) 1},
while for the background, N4 is used:
V41 (i, j) = {(h, k)kD4 ((i, j), (h, k)) 1}
First Prev Next Last Go Back Full Screen Close Quit

Thresholding
A simple technique for obtaining a binary image from a grey-level image is
thresholding:
(
1, f (i, j) T,
g(i, j) =
0, f (i, j) < T,
where T is denoted as threshold. If objects do not touch each other, and their
intensity is well discriminated from the background, thresholding is a good
segmentation technique.
The choice of a suitable threshold is critical. It may be derived from the
image histogram.

First Prev Next Last Go Back Full Screen Close Quit

Thresholding - example
Thresholding for the test image, using T = 64 (middle) resp. T = 128
(right):

Thresholding for an MRI slice, using T = 60 (middle) resp. T = 140 (right):

First Prev Next Last Go Back Full Screen Close Quit

Edge Detection
Using the definition of connectivity, edge detection in binary images is
straightforward:
A pixel f (i, j) is called edge voxel if it belongs to the foreground and at least
one of its N4 neighbors belongs to the background.
for (int i = 1; i < N-1; i++) {
for (int j = 1; j < M-1; j++) {
if (f(i, j) == 0) continue;
// ignore background
if (f(i-1, j) == 0 || f(i+1, j) == 0 ||
f(i, j-1) == 0 || f(i, j+1) == 0) {
// a neighbor belongs to the background
// so mark current position as an edge pixel.
g(i, j) = 1; } }

First Prev Next Last Go Back Full Screen Close Quit

Connected Component Labeling


Connected components are regions in binary images. Algorithms for labeling connected components yield label images on output, in which each pixel
receives a label addressing it to a specific component (region).
Consider an image as a graph where pixels correspond to graph nodes and
neighborhood relationships to graph edges. Then, labeling can be formulated as a search problem on this graph:
1. Selected a foreground pixel. Address label l = 1 to this pixel.
2. Search recursively for all neighboring pixels in the foreground. Address
l to this pixel.
3. Increment l.
4. Select the next unlabeled pixel. Address l to this pixel.
5. Repeat steps 2-4 until all foreground pixel are labeled.

First Prev Next Last Go Back Full Screen Close Quit

Connected Components - Algorithm


label = 1;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (f(i, j) == 0) continue;
// ignore background
if (g(i, j) != 0) continue;
// already visited
stack.clear();
stack.push(i, j);
// save this pixel
g(i, j) = label;
// label this pixel
while (stack.empty() == false) {
stack.pop(h, k);
for (int ii = h-1; ii <= h+1; ii++) {
for (int jj = k-1; jj <= k+1; jj++) {
if (h == ii && k == jj) continue;
if (f(ii, jj) == 0) continue;
if (g(ii, jj) != 0) continue;
stack.push(ii, jj);
g(ii, jj) = label; } } }
label++; }
}

First Prev Next Last Go Back Full Screen Close Quit

Connected Components - Example

First Prev Next Last Go Back Full Screen Close Quit

Morphological Operators I
Mathematical morphology provides two basic operators, dilation and erosion), from which more complex operators are built.
Consider an n-dimensional binary image f () and s() a second, typically
smaller n-dimensional binary image. f () and s() are given in a set representation that contains all foreground pixels and their addresses.
Let us denote s() as structuring element. During dilation and erosion, this
structuring element is shifted over the whole image while performing an
operation on the set.
The structuring element is shifted over the image by a process called vector
translation. Addresses of all foreground pixels are shifted by a vector t:
f t = {p tkp f }.

First Prev Next Last Go Back Full Screen Close Quit

Morphological Operators II
Dilation of a binary image f () with a structuring element s():
f s =

f + t = {p + tkp f ,t s}.

ts

Erosion of a binary image f () with a structuring element s():


f s =

f t = {zk(s + z) f }.

ts

Results of erosion or dilation depend obviously on the shape of the structuring element. Often, round or spherical elements
areNext
used.
First Prev
Last Go Back Full Screen Close Quit

Morphological Operators III


An erosion followed by a dilation using the same structuring element yields
an opening:
f s = ( f s) s.
Likewise, a dilation followed by an erosion using the same structuring element yields a closing:
f s = ( f s) s.
Small bridges between objects are removed by opening. Contours are
smoothed by closing.

First Prev Next Last Go Back Full Screen Close Quit

Dilation - Implementation
Consider an image f and a structuring element s, given as a list of length K
and the coordinates of the foreground pixels. Dilation works like:
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (f(i, j) == 1) {
g(i, j) = 1; goto next; }
for (int k = 0; k < K; k++) {
ii = i + s[k].i;
if (ii < 0 || ii == N) continue;
jj = j + s[k].j;
if (jj < 0 || jj == M) continue;
if (f(ii, jj) == 1) {
g(ii, jj) = 1; goto next; } }
next: ; }
}

First Prev Next Last Go Back Full Screen Close Quit

Dilation - Example
Original image (top left), dilation (3x3, top right), closing (3x3, below left),
closing (5x5, below right).

First Prev Next Last Go Back Full Screen Close Quit

Erosion - implementation
Erosion works like:
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (f(i, j) == 0) {
g(i, j) = 0; goto next; }
for (int k = 0; k < K; k++) {
...
if (f(ii, jj) == 0) goto next; }
g(i, j) = 1;
next: ; }
}

First Prev Next Last Go Back Full Screen Close Quit

Erosion - Example
Original image (top left), erosion (3x3, top right), opening (3x3, below left),
opening (5x5, below right).

First Prev Next Last Go Back Full Screen Close Quit

Skeletonization
The skeleton of a region is obtained by repeated application of a pixelwise
thinning operation:
1. Denote f () a binary image, and p one of its a foreground pixels.
2. p is called a directed border pixel if one pixel in its N4 neighborhood
belongs to the background.
3. All directions (N, O, S, W) are stepped through sequentially.
4. All directed border pixel are candidates for removal, except their number
of foreground pixels in the N4 neighborhood is less than 2.
5. All deletable pixels in one direction are marked in one run and removed
in a second run.
6. This process is repeated until no removals are possible.

First Prev Next Last Go Back Full Screen Close Quit

Skeletonization - Implementation
pixel R[N];
// contains pixels of the input image
do {
ndel = 0;
for (dir = 0; dir < 4; dir++) { // for all directions
switch (dir) {
case 0: di = 0; dj = 1; break;
case 1: di = 1; dj = 0; break;
case 2: di = 0; dj = -1; break;
case 3: di = -1; dj = 0; break; }
// mark all removable pixels
for (int n = 0; n < N; n++) {
if (g(R[n].i, R[n].j) == 1) continue;
if (g(R[n].i+di, R[n].j+dj) == 1) continue;
if (neighbors(g(R[n].i, R[n].j) < 2) continue;
R[n].mark = 1; }
// remove all marked pixels
for (int n = 0; n < N; n++) {
if (R[n].mark == 1) {
g(R[n].i, R[n].j) = 0;
R[n].mark = 0;
ndel++; } } } }
while (ndel != 0);

First Prev Next Last Go Back Full Screen Close Quit

Skeletonization - Example
Intensity images (top row) and skeletons (below):

First Prev Next Last Go Back Full Screen Close Quit

Distance Transformation
The distance transform is used to determine distances between objects. Consider the one-dimensional case. In this example, 0 corresponds to a foreground pixel from which distances are measured, and some distance to be
determined. Let us define a local distance mask [1 0 1]. This local mask is
added to pixels at a specific position. The minimum of all sums is addressed
to the pixel at this position. The process is repeated in a waveform fashion:

2
2

1
1
1

0
0
0
0

1
1
1

2
2

2
2

1
1
1

0
0
0
0

1
1
1

First Prev Next Last Go Back Full Screen Close Quit

Distance Transformation - Implementation


for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (g(i, j) == 0) continue;
min = HUGE;
for (int ii = -1; ii <= 1; ii++) {
for (int jj = -1; jj <= 1; jj++) {
v = g(i+ii, j+jj)+fmask(ii, jj);
if (v < min) min = v; } }
g(i, j) = min; } }
for (int i = N-1; i >= 0; i--) {
for (int j = M-1; j >= 0; j--) {
if (g(i, j) == 0) continue;
min = HUGE;
for (int ii = -1; ii <= 1; ii++) {
for (int jj = -1; jj <= 1; jj++) {
v = g(i+ii, j+jj)+bmask(ii, jj);
if (v < min) min = v; } }
g(i, j) = min; } }

where:

b a b
f mask = a 0 0 ,
0 0 0

0 0 0
bmask = 0 0 a ,
b a b

with a = 0.95509 and b = 1.36930.

First Prev Next Last Go Back Full Screen Close Quit

Distance Transformation - Example


Consider a region (left) and its distance transformation (right). Distance
values increase from the region boundary to the image border.

A boundary (left) and its distance transformation (right). Distance values


increase from the boundary to the image center and to the border.

First Prev Next Last Go Back Full Screen Close Quit

You might also like