You are on page 1of 80

DIGITAL IMAGE PROCESSING

SKM4213

CHAPTER 3

PRE-PROCESSING
2

Content:
• Region-of-Interest (Image Geometry)
• Crop, zoom, translation, rotation, shrinking

• Image Algebra
• Addition, subtraction, multiplication, division, Image masking,
AND, OR, NOT

• Image Quantization
• Gray level reduction
• Spatial quantization
3

CV SYSTEM MODEL

Image Analysis
4

INTRODUCTION
• Pre-processing:
• To make data reduction and image analysis
task easier.

• Pre-processing operations are:


• Extracting region of interest.
• Performing basic algebraic operations on
images.
• Enhancing specific image features.
• Reducing data in both resolution and
brightness.
5

REGION-OF-INTEREST
(ROI)
GEOMETRIC TRANSFORM (CROP, ZOOM, SHRINK,
TRANSLATE, ROTATE)
6

REGION-OF-INTEREST (ROI)
• To investigate more closely a specific area within an
image.

• Operations that modify the spatial coordinates of the


image (Image geometry operations).

• Examples of image geometry operations - crop, zoom,


enlarge, shrink, translate, rotate.
7

Crop
• Selecting a small portion of the image (a sub image),
and cutting it away from the rest of the image.
8

Zooming
• This photo is too small:

• Might need this for forensics:


http://www.youtube.com/watch?v=3uoM5kfZIQ0
9

Zooming (cont.)
• Zooming requires 2 steps:
1. The creation of new pixel locations.

2. The assignment of intensity values to these new locations.

• Various techniques for zooming:


1. Nearest Neighbor Interpolation (Zero-order Hold, pixel
replication)

2. Linear Interpolation (First-order Hold)

3. Bilinear Interpolation

4. Bicubic Interpolation

5. Polynomial Interpolation
10

Interpolation
• I(r,c) gives us the intensity values of an image at integer
locations.

• May need to estimate (interpolate) the intensity values of an


image at non-integer locations based on the intensity
values nearby.
Intensity values at integer locations
I(0,0) I(0,1) I(0,2)
8 4 8
I(1,0) I(1,1) I(1,2)
4 8 4
I(2,0) I(2,1) I(2,2)
8 2 8

What is the intensity value for I(1, 1.5)?


11

Nearest Neighbor Interpolation


• Replicate the intensity values of neighboring pixels n times, row wise
and column wise.

• Creating a checkerboard or blocky effect.


• Example:
Suppose a 2×2 image will be enlarged 2 times:
1. Replicate each of the pixel row wise.

2. Replicate each of the pixel column wise.

3. The new image will have a dimension of (original image rows *


zooming factor, original image cols * zooming factor).
50 50 100 100
50 50 100 100
50 100 50 50 100 100 180 180 250 250
180 250 180 180 250 250 180 180 250 250

Original Image Image with columns expanded Image with rows and columns
expanded
12
13
14

Nearest Neighbor Interpolation (cont.)

• Advantage: Simple and fast!

• Disadvantage: Generated zoomed image is very blurry.

Zooming factor , blurry

Can result in fully blurred image 


15

Nearest Neighbor Interpolation (cont.)

• Class Exercise:

Based on zero-order hold, enlarge the following 3×3 image, 3 times:

4 8

8 2
16

Linear Interpolation
• Perform linear interpolation between adjacent pixels.

• Finding the average value between two pixels.

• That value will be used to represent the pixel value


between those two.

• Do row wise and then column wise.

8 4 8 8 6 4 6 8 8 6 4 6 8
6 6 6 6 6
4 8 4 4 6 8 6 4
4 6 8 6 4
8 2 8 8 5 2 5 8 6 5.5 5 5.5 6
8 5 2 5 8

Original Image Image with rows expanded


Image with rows and columns
expanded
17

Linear Interpolation (cont.)


• The new image will have a dimension of (2N-1) × (2N-1).
Can be repeated as desired.

• Advantage: Zoomed image is less blurry in comparison to


Zero-order Hold method.

• Disadvantage: Can only run on the multiply by 2.


3×3 image will get zoomed image of the dimension 5×5.
Zoomed again and 9×9 image will be generated, and so
on.
18

Linear Interpolation (cont.)

• Class Exercise:

Based on First-order Hold, generate the zoomed image for the following
image:

6 3 6

3 5 5

6 4 6
19
20

Zero-order Hold Vs. First-order Hold


21

Convolution
• Previous interpolation methods could also be done
using convolution (filtering) with a convolution mask.

• Based on mathematical process to enlarge an image.


The process of sliding, multiplying and summing is
called convolution.

• Steps:
o Adding rows and columns of zeros between
existing rows and columns.

o Perform the convolution.


22

Convolution (cont.)

For zero-order hold convolution, result needs to be put in


the pixel location corresponding to the lower-right corner,
since there is no center pixel.

Note: Convolution masks for computer imaging applications are typically symmetric,
meaning if it is flipped about its center it will remain the same.
23

Convolution (cont.)
• Convolution process:
• Overlay the mask on the image, multiply the coincident values, and sum all
these results (equivalent to finding the vector inner product of the mask with
underlying sub image).

• The result is used to substitute the pixel value that coincide with the center of
the mask. Note that the output image must be put in a separate image array
called a buffer, so that the existing values are not overwritten during the
convolution process.

• Next, slide the convolution mask over by one pixel.

• Repeat the process until end of the row.

• Once the end of the row is reached, the convolution mask is moved down one
row and the process is repeated row by row for the entire image.
24

Convolution (cont.)
25

Convolution (cont.)
26

Convolution (cont.)
• Example: Convolution for first-order hold

• For example, if we put the mask over the upper-left corner of the image, we
obtain (from left to right, and top to bottom):
1/4(0) + 1/2(0) + 1/4(0) + 1/2(0) + 1(3) + 1/2(0) + 1/4(0) + 1/2(0) + 1/4(0) = 3

• The next step is to slide the mask over by on pixel and repeat the process, as
follows:
1/4(0) + 1/2(0) + 1/4(0) + 1/2(3) + 1(0) + 1/2(5) + 1/4(0) + 1/2(0) + 1/4(0) = 4
27

Convolution (cont.)

• Why use convolution instead of nearest neighbor


or linear interpolations?

Many computer imaging systems can perform calculation in


hardware in a very fast manner, typically much faster than
applying a faster algorithm in software.
28

K-times Zooming
• Linear Interpolation only allows image enlargement by a
factor of (2N-1).

• K-times Zooming allow us to enlarge an image by other


than a factor of (2N-1), where K is the zooming factor.

• The generated enlarged image will have the dimension of


K(N-1)+1. (approx. equal to KN). Typically N is larger
than K.
29

K-Times Zooming (cont.)


• Subtract the smaller from greater one of two adjacent pixels.

• Divide the result with the zooming factor K.

• Add the result to the smaller value, and keep adding the result from the
second step in a running total until all (K-1) intermediate pixel locations
are filled.

• Sort the inserted values in ascending order to remain the symmetry


between them.

• Do for every pair of adjacent pixels, first along the rows and then along
the columns.
30

K-Times Zooming (cont.)


• Example: 2×3 image, zoom it 3 times.

Determine zooming factor: K = 3


Determine number of intermediate pixels: (K-1) = 2
Don’t forget to sort the result for smoother enlarged image.

15 30 15

30 15 30

Original Image

15 20 25 30 25 20 15
20 21.67 23.34 25 23.34 21.67 20
25 23.34 21.67 20 21.67 23.34 25
30 25 20 15 20 25 30
Enlarged Image
31

K-Times Zooming (cont.)


• Advantage: Able to compute zooming of any factor.
Enlarged image is less blurry.

• Disadvantage: Needs to sort in the end (additional step),


thus increase the computation cost.
32

K-Times Zooming (cont.)


• Class Exercise:

Enlarge the following image up to three times its original size:

[ ]
3 15 9
12 3 15
9 3 1 8
33

Translation
• Process of moving a ROI from one position to another.

• Equations for translation are given as follows:

• r’ and c’ are the new coordinates.

• r and c are the original coordinates.

• r0 and c0 are the distances to move or translate the image.


34

Initial Point (0,0)


35
36

Rotation
• Process of rotating a ROI for a number of degrees.

• Equations for rotation are given as follows:

• r’ and c’ are the new coordinates.

• r and c are the original coordinates.

• θ is the angle of rotation of the image, defined in a clockwise direction from the
horizontal axis at the image origin in the upper left corner.
37

Translation + Rotation

• The translation and rotation process can be combined


into one set of equations.
38

Translation difficulty
• Created a leftover space. What to do with it?

• Two options to solve:


• Fill the top row with constant values, either black (0) or white (255).

• Wrap around by shifting the bottom row to the top.


39

Rotation difficulty
• Some part might be rotated off the screen (image plane).
40

Rotation difficulty : to solve

• Translate back to center.

• Leftover space in the corners can be filled with a constant.

• Extract the central, rectangular portion of the image and


enlarge it to the original image size.
41

Example of enlarged image plane


42

Exercise
• Translate Image 1 by 10 columns to the right and 5 rows
up with the initial point (0,0) is on the left top corner.
What are the new coordinates for the point (r,c) =(10,10)?

• Rotate Image 2 by 20o in the clockwise direction.


What are the new coordinates for the point (r,c) =(10,10)?
43

IMAGE ALGEBRA
ARITHMETIC (+, -, *, /) & LOGIC (AND, OR, NOT)
44

IMAGE ALGEBRA
• There are two categories of algebraic operations that can
be applied to images.

• Arithmetic operations:
• Addition, subtraction, division and multiplication.

• Logic operations:
• AND, OR, NOT

• To combine images in various ways.

• These operations (except the NOT logic) are performed on


two images and done on pixel-by-pixel basis.
45

Addition
• To combine information in two images.

• Applications: image morphing, image sharpening.


Example : Addition

+ =

46
Example : Addition (cont.)

+ =

47
48

Subtraction
• to detect motion. Background subtraction.
49

Multiplication
• Brighten the image.

• Multiplication of the pixel values by a constant


value greater than one.

a. Original Image b. Image multiplied by 2


50

Division
• Darken the image.

• Division of the pixel values by a constant value greater


than one.

a. Original Image b. Image divided by 2


51

‘AND’, ‘OR’
• To perform a masking operation.

• Logic operations operate in bit-wise fashion on pixel data.

• Application:
• Special effects.

• Masking operation in image analysis.

• Extract a ROI from an image.


‘AND’ example

52
53

‘AND’ example (cont.)

• White mask AND image = image


portion coincides with the white
mask appears with background
turned black.

• Black mask OR image = image


portion coincides with the black
mask appears with background
turned white.
54

‘NOT’
• creates a negative of the original image.
• Inverting each bit within each pixel value.

Original image Result of NOT operation


55

Example: Addition & Subtraction

Added by 128

Subtracted by 128
56

Example: Multiplication & Division

Multiplied by 2

Divided by 2
57

Comparison: Addition VS Multiplication


58

Comparison: Subtraction VS Division


59

Example: Complement
60

QUANTIZATION
61

Image Quantization
• Process of reducing image data by removing some of the
detail information by mapping groups of data to a single
point.

• Relates to image shrinking.

• There are two types:


1. Gray-level reduction
• Reduce the precision of pixel values.
• Map groups of values into one.

2. Spatial reduction:
• Reduce number of pixels.
• Map a group of pixels into one pixels.
62

Gray-level reduction
1. The simplest method of gray-level reduction is
thresholding.
• Any pixel above the threshold is set to 1, and the rest to 0.
• This turns a gray-level image into binary image.

2. A more versatile method of gray-level reduction is by


reducing the number of bits per pixel.
a. This can be done by masking the lower bits via an AND operation.
• The number of bits that are masked determines the number of gray-levels
available.
b. Alternatively, we can map the quantized gray-level values to the high
end of the range using an OR operation.
c. Note: OR mask can also be found by negating (NOT) the AND mask.
63

Gray-level reduction (cont.)

• The general rule goes like this. If we want to reduce 256


gray levels to n gray levels:
• Find k such that 256/2k = n.
• AND operation: Mask the lower k bits with zero.
• OR operation: Mask the lower k bits with one.

• We can reduce the number of gray-levels to any power of


2: 2, 4, 8, 16, 32, 64, or 128.
64
Gray-level reduction (cont.)
Example
• Original image has 256 gray-level values. We want to
quantize to 32 gray-level values.

• 256  28, 32  25

• Find k such that 256/2k = 32:


• K can be obtained by finding the bit difference (8 – 5 = 3-bit).

• Mask the lower k bits:


• AND: 111110002
• OR: 000001112
Gray-level reduction (cont.)
Example (cont.)
• 0 – 255:

0 255

• 0 – 255, with each range has 8 values:


8 16 24 32……

0 7 15 23 31 …255
256 - 32 AND (low end) OR (high end) New pixel value

0-7 0 7 0
8 – 15 8 15 1
16 – 23 16 23 2
24 - 31 24 31 3
: : : :
248 - 255 248 255 31
Gray-level reduction (cont.)
Example (cont.)
• Let say, we want to quantize an 8-bit value (250) to 5-bit value (what is the
value of 250 in 5-bit?):
1. Change the decimal value to binary:
• 25010  111110102

2. Perform either AND or OR operation:

AND operation OR operation


25010 111110102 25010 111110102
mask 111110002 mask 000001112
After masking 111110002 After masking 111111112

3. Shift result to the right k times


• Result based on AND operation: 00011111 2
• Result based on OR operation: 00011111 2
Gray-level reduction (cont.)
Example (cont.)
• Change the shifted binary result back to decimal:
• Based on AND operation: 000111112  3110
• Based on OR operation: 000111112  3110

8 16 24 32……

0 7 15 23 31 …255
69

Gray-level reduction (cont.)


• As the number of gray-level decreases, false contouring
increases.

• Contouring effect can be visually improved by using


Improved gray-scale (IGS) quantization method.

• It takes advantage of human visual system’s sensitivity to


edge by adding small random number to each pixel
before quantization.

• Results in a more visually pleasing appearance.


70

Gray-level reduction (cont.)


Gray-level reduction (cont.)
• The quantized values can also be mapped to the mid-point of the
range.

• This is done by an AND after an OR operation, or an OR after an


AND operation.

71
72

Gray-level reduction (cont.)


• Although AND/OR method is very efficient, it is not very
flexible since the size of the quantization bin is not
variable.

• There are also gray-level quantization methods that allow


for variable bin sizes.

• These methods are more complicated and the variable bin


size is application dependent.
73

Gray-level reduction (cont.)


74
75
76

Spatial reduction
• Quantization in spatial coordinates results in reducing the size
of the image.

• This is accomplished by taking groups of pixels that are


spatially adjacent and mapping them to one pixel.

• This can be done in one of three ways:


• Averaging: Find the grey-level average of a group of pixels (highest
visual quality).
• Median: Sort the group of pixels from lowest to highest and find the
median.
• Decimation (or subsampling): Eliminating some of the data (lowest visual
quality).
77

Decimation
78

Spatial reduction (cont.)


• The quality of a decimation quantization image can be
improved by preprocessing the image with an averaging,
or mean spatial filter.

• This type of filtering is called anti-aliasing filtering.


79

EXERCISE
2 4 6 8 5 8

4 5 7 3 7 9
3 4 5 6 7 8
4 2 1 7 3 5
6 7 2 5 4 3
7 5 3 7 6 6

Perform spatial reduction techniques of averaging and median to the following 6x6
image using 3x3 mask. From your observation, in term of image contrast, which one
give the best or both giving the same output?
80

Conclusion
• Region-of-Interest (Image Geometry)
• Crop, zoom, translation, rotation, shrinking
• Image Algebra
• Addition, subtraction, multiplication, division, Image masking, AND,
OR, NOT
• Image Quantization
• Gray level reduction
• Thresholding
• Reducing the number of bits per pixel
• Uniform quantization bins
• Variable quantization bins
• Spatial quantization
• Averaging
• Median
• Decimation

You might also like