0% found this document useful (0 votes)
70 views

Chapter 3 - Intensity Transformation and Spatial Filtering (Updated)

This document discusses intensity transformation and spatial filters for image enhancement. It describes how intensity transformation involves modifying pixel intensities individually without considering neighboring pixels, while spatial filtering uses a neighborhood or mask to modify pixel values based on neighboring pixels. The key intensity transformation techniques covered are linear transformations like negative and log transformations, as well as power-law and piecewise linear transformations like contrast stretching and thresholding. Spatial filtering techniques are also introduced.

Uploaded by

saif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Chapter 3 - Intensity Transformation and Spatial Filtering (Updated)

This document discusses intensity transformation and spatial filters for image enhancement. It describes how intensity transformation involves modifying pixel intensities individually without considering neighboring pixels, while spatial filtering uses a neighborhood or mask to modify pixel values based on neighboring pixels. The key intensity transformation techniques covered are linear transformations like negative and log transformations, as well as power-law and piecewise linear transformations like contrast stretching and thresholding. Spatial filtering techniques are also introduced.

Uploaded by

saif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 199

Image Processing

Ch3: Intensity Transformation


and spatial filters
Image Enhancement?
Enhancement : is to process an image so that
the result is more suitable than the original image
for a specific application.

Enhancing an image provides better contrast and a more detailed


image as compare to non enhanced image. It is used to enhance
medical images, images captured in remote sensing, images from
satellite e.t.c

Enhancement techniques fall into 2 types:


 Spatial domain: direct manipulation of pixels in the image plane
 Frequency domain: modifying frequency spectrum of the image.

 In this chapter, we are going to discuss spatial domain


techniques
Enhancement in Spatial Domain
Spatial domain: aggregate of pixels
composing an image
Spatial domain processes:
g(x,y) = T[f(x,y)]
Input image
Processed (output) image
Operator T defined on some
neighborhood of f(x,y)
Defining a Neighborhood (T)
Operator T is applied at
This rectangle is each location (x,y) to
called neighborhood
or mask produce the output g at
that location.

types of neighborhood:

1. intensity transformation: neighborhood of size 1x1


2. spatial filter (or mask ,kernel, template or window): neighborhood of
larger size , like in the above example.
Intensity Transformations Functions
 Operation deals with pixel intensity values individually
 The transformed output pixel value does not depend on any of
the neighboring pixel value of the input image
 The intensity values are altered using particular transformation
techniques
 Here, T is called intensity transformation function or (mapping,
gray level function)
g(x,y) = T[f(x,y)]
s r
s= T(r)
where r is the pixels of the input image and s is the pixels of the
output image
Spatial Filtering
 Mask is a small matrix useful for blurring,
sharpening, edge-detection and more.

 New image is generated by applying the mask


matrix on the input image.

 The output pixel values thus depend on the


neighboring input pixel values.

 The mask may be of any dimension 3X3 4X4 ….


Spatial Filtering
Intensity transformations functions
Intensity transformation functions fall into 2 approaches:

1) Basic intensity transformations


a) Linear ( negative and identity).
b) logarithmic ( Log and Inverse Log) .
c) Power( nth power and nth root).

2) piecewise Linear transformation functions.


a) Contrast stretching, thresholding
b) Gray-level slicing
c) Bit-plane slicing
Basic Intensity (gray level) Transformations
a) Linear ( negative and identity).
b) logarithmic ( Log and Inverse Log) .
c) Power( nth power and nth root).

New gray level

Old gray level before enhancement


Identity Transformation Function

 In Identity transformation, each value of the


input image is directly mapped to each other
value of output image.
 Thus, the output intensities are identical to the
input intensities. It has been shown below:
Negative Transformation Function
 Negative images are useful for enhancing white
or grey detail embedded in dark regions of an
image.
 The negative of an image with gray levels in the
range [0,L-1] is obtained by using the expression
s = L -1 - r
(L-1) = Maximum pixel value where L is the number of gray
levels in the input image
r = input Pixel value of an original image
Negative Transformation Function
The negative of an image with intensity levels in the range [0,L-1]
is obtained by using the negative transformation :
s= L-1-r

Image (r) Image (s ) after applying T (negative)

Advantages of negative :
 Produces an equivalent of a photographic negative.
 Enhances white or gray detail embedded in dark regions especially when the black
areas are dominant in size..
Negative Transformation Function
The negative of an image with intensity levels in the range [0,L-1]
is obtained by using the negative transformation :
s= L-1-r
Image (r)
Example
100 110 90 95
the following matrix represents the pixels values of
an 8-bit image (r) , apply negative transform and 98 140 145 135
find the resulting image pixel values. 89 90 88 85

102 105 99 115


solution:
L= 28 = 256
Image (s)

s=L-1-r 155 145 165 160


s =255-r
157 115 110 120

Apply this transform to 166 165 167 170

each pixel to find the negative 153 150 156 140


s
Negative Transformation Function
Exercise:

the following matrix represents the pixels values of a


5-bit image (r) , apply negative transform and find Image (r)
21 26 29 30
the resulting image pixel values.
19 21 20 30
solution: 16 16 26 31
Image (s)
19 18 27 23
Image Negation (Example)
Log Transformation Function
 The general form is:
s=c log(1+r) r>=0
 (1+r): All the input intensity values have been incremented
by 1 because the input values vary from 0 to 255 and the
log(0) is not defined.

 r varies from (0-255), log(1+r) ranges from (0-2.4).

 The role of the multiplier C is to make the log-transformed


pixel values span the entire range of 256 grayscale levels
available for the output image.
Log Transformation Function
Log Transformation Function
 The general form is:
S=c log(1+r) r>=0
where c is a constant, and it is assumed that
 It maps a narrow range of low gray-level values in the input image
into a wider range of output levels.
 The opposite is true of higher values of input levels.
 Used to expand the values of dark pixels in an image while
compressing the higher-level values.
 The opposite is true of the inverse log.
 The power-law transformations are much more versatile for this
purpose than the log transformation
Log Transformation Function
Log Transformation Function
Input range

Output range after applying log transformation function

Intensity values of 0 and 15 in the input are


mapped to 0 and 127 in the output.

The exact opposite phenomenon takes place at the


other end of the spectrum. For example, pixels with
intensities of 205 and 255 are mapped to 245 and
255 by the log transform.
Exponential or Inverse-Log Transformation
 The general form is:

 Typically, b is chosen to lie close to 1.

Input range

Output range after applying inverse log transformation function


Inverse Log Transformation Function
Example: Log Trans. Function

This example proves that the log transform can be effective in editing pictures
that have been captured against the light source by digging out contrast
information from the darker regions of an image at the cost of the brighter
segments
Power-Law Transformation
 Power-law transformations have the basic form
S=cry

 Where c and gamma are positive constants.


 Plots of s versus r for various values of gamma are shown in Fig.
3.6.
 As in the case of the log transformation, power-law curves with
fractional values of gamma map a narrow range of dark input
values into a wider range of output values, with the opposite
being true for higher values of input levels.
19/07/2020 25
Power-law transformation
Power-Law Transformation
Example: Power-Law Transformation

19/07/2020 28
Example: Power-Law Transformation
Piecewise Linear
Transformation Functions
Contrast Stretching and
Thresholding
Contrast Stretching
Contrast can be simply explained as the difference between maximum
and minimum pixel intensity in an image.

The following example shows three 8-bit images with different contrast
level

100 100 100 100 0 10 20 30 100 105 107 100

100 100 100 100 40 60 80 90 120 125 120 130

100 100 100 100 100 120 150 180 115 117 112 118

100 100 100 100 200 220 240 255 130 140 145 150

Contrast level Contrast level Contrast level


100 -100 = 0 255 - 0 = 255 150 - 100 = 50
(No contrast) (High-contrast) Low-contrast
Contrast Stretching
 One of the simplest piecewise linear functions is a
contrast-stretching transformation.
 Low-contrast images can result from
 poor illumination,
 lack of dynamic range in the imaging sensor,
 or even wrong setting of a lens aperture during image
acquisition.
 The idea behind contrast stretching is to increase
the dynamic range of the gray levels in the image
being processed.

19/07/2020 32
Contrast Stretching
Contrast stretching is also known as normalization. The
quality of image is enhanced by stretching the range of
intensity values.

 Is a process that
expands the range of
intensity levels in an
image so that it spans
the full intensity range
of the display device.
Contrast Stretching
Contrast stretching, which means that the bright
pixels in the image will become brighter and the dark
pixels will become darker, this means : higher
contrast image.
Contrast Stretching
 Example:
Input Image: 8-bit image have range [80 – 150]
After Contrast Stretching: the new range becomes [0 – 255]
using the following equation:

Where I is the input intensity, Inew is the output intensity after


contrast stretching, NewMax and NewMin are the new
intensity range (0 – 255), Max and Min are the input intensity
range (80 – 150)
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:
(255-0)/(151-100)]
255/51 = 5

100 105 107 100 0


After contrast
stretching
120 125 120 130

115 117 112 118

130 140 145 151

Input Image Output Image


I = 100 : (100-100) * [(255-0)/(151-100)] +0
I = 100  Inew = 0
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25


After contrast
stretching
120 125 120 130

115 117 112 118

130 140 145 151

Input Image Output Image


I = 105 : (105-100) * 5 +0
I = 105  Inew = 5 * 5 = 25
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35


After contrast
stretching
120 125 120 130

115 117 112 118

130 140 145 151

Input Image Output Image


I = 107 : (107-100) * 5 +0
I = 107  Inew = 7 * 5 = 35
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130

115 117 112 118

130 140 145 151

Input Image Output Image


I = 100 : (100-100) * 5 +0
I = 100  Inew = 0 * 5 = 0
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100

115 117 112 118

130 140 145 151

Input Image Output Image


I = 120 : (120-100) * 5 +0
I = 120  Inew = 20 * 5 = 100
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100 125

115 117 112 118

130 140 145 151

Input Image Output Image


I = 125 : (125-100) * 5 +0
I = 125  Inew = 25 * 5 = 125
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100 125 100

115 117 112 118

130 140 145 151

Input Image Output Image


I = 120 : (120-100) * 5 +0
I = 120  Inew = 20 * 5 = 100
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100 125 100 150

115 117 112 118

130 140 145 151

Input Image Output Image


I = 130 : (130-100) * 5 +0
I = 130  Inew = 30 * 5 = 150
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100 125 100 150

115 117 112 118 75

130 140 145 151

Input Image Output Image


I = 115 : (115-100) * 5 +0
I = 115  Inew = 15 * 5 = 75
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100 125 100 150

115 117 112 118 75 85

130 140 145 151

Input Image Output Image


I = 117 : (117-100) * 5 +0
I = 117  Inew = 17 * 5 = 85
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100 125 100 150

115 117 112 118 75 85 60

130 140 145 151

Input Image Output Image


I = 112 : (112-100) * 5 +0
I = 112  Inew = 12 * 5 = 60
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100 125 100 150

115 117 112 118 75 85 60 90

130 140 145 151

Input Image Output Image


I = 118 : (118-100) * 5 +0
I = 118  Inew = 18 * 5 = 90
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100 125 100 150

115 117 112 118 75 85 60 90

130 140 145 151 150

Input Image Output Image


I = 130 : (130-100) * 5 +0
I = 130  Inew = 30 * 5 = 150
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100 125 100 150

115 117 112 118 75 85 60 90

130 140 145 151 150 200

Input Image Output Image


I = 140 : (140-100) * 5 +0
I = 140  Inew = 40 * 5 = 200
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100 125 100 150

115 117 112 118 75 85 60 90

130 140 145 151 150 200 225

Input Image Output Image


I = 145 : (145-100) * 5 +0
I = 145  Inew = 45 * 5 = 225
Contrast Stretching
 Example:
Input Image: 8-bit image have range [100 – 151]
After Contrast Stretching: the new range becomes [0 – 255] using the
following equation:

100 105 107 100 0 25 35 0


After contrast
stretching
120 125 120 130 100 125 100 150

115 117 112 118 75 85 60 90

130 140 145 151 150 200 225 225

Input Image Output Image


I = 151 : (151-100) * 5 +0
I = 151  Inew = 51 * 5 = 225
Contrast Stretching and Thresholding
(r2, s2)
(r2, s2)

(r1, s1)
(r1, s1)

Contrast stretching Thresholding:


Assume that
a: rmin,
b:rmax,
k : intensity

Contrast stretching: (r1,s1)=(rmin,0) , (r2,s2)=(rmax,L-1)


Thresholding: (r1,s1)=(k,0) , (r2,s2)=(k,L-1)
Contrast Stretching and Thresholding
(r2, s2)
(r2, s2)

(r1, s1)
(r1, s1)

Contrast stretching Thresholding:


Assume that
a: rmin,
bright pixels in the image will become brighter and the dark pixels
b:rmax, will become darker, this means : higher contrast image.
k : intensity

Contrast stretching: (r1,s1)=(rmin,0) , (r2,s2)=(rmax,L-1)


Thresholding: (r1,s1)=(k,0) , (r2,s2)=(k,L-1)
Example: Contrast Stretching
Image (s ) after applying T
Image (r) (contrast stretching)

Notice that the intensity transformation function T,


made the pixels with dark intensities darker and the
bright ones even more brighter, this is called
contrast stretching>
Thresholding
 Remember that:
Or
g(x,y) = T[f(x,y)]
Example: suppose m= 150 (called threshold),
s= T(r)
if r (or pixel intensity in image f ‫ ) اﻟﺻورة اﻻﺻﻠﯾﺔ‬is
above this threshold it becomes 1 in s (or pixel
intensity in image g ‫) اﻟﺻورة ﺑﻌد اﻟﺗﻌدﯾل‬, otherwise it
Pixels above 150 become 1 becomes zero.
255

If f(x,y)>=150; g(x,y)=1
T= If f(x,y)<150; g(x,y)=0

Or simply…

This is called
thresholding,
0 255
T= If r >=150; s =1 and it produces
If r <150; s =0 a binary image!

Pixels less than 150 become 0


Thresholding
Image (s ) after applying T
Image (r) (Thresholding)

Notice that the intensity transformation function T, convert the


pixels with dark intensities into black and the bright pixels into
white. Pixels above threshold is considered bright and below it is
considered dark, and this process is called thresholding.
Example: Contrast Stretching and Thresholding

8-bit image with low contrast

After contrast stretching


(r1,s1)=(rmin,0) , (r2,s2)=(rmax,L-1)

Thresholding function
(r1,s1)=(m,0) , (r2,s2)=(m,L-1)
m : mean intensity level in the image
Exercise on Contrast Stretching and Thresholding

Exercise:

the following matrix represents the pixels values of a 8-bit image


(r) , apply thresholding transform assuming that the threshold
m=95, find the resulting image pixel values.
Image (r)

solution: 110 120 90 130

Image (s) 91 94 98 200

90 91 99 100
1 1 0 1

0 0 1 1 82 96 85 90

0 0 1 1

0 1 0 0
Piecewise Linear
Transformation Functions
Intensity-Level Slicing and
Bit-Plane Slicing
Intensity-Level Slicing (gray level slicing)
Highlighting a specific range of intensities in an image.
In other words, we segment certain gray level regions from the
rest of the image.
Approach 1 Approach 2

display in one value(e.g white) all Brightens or darkens the desired


the values in the range of interest , range of intensities but leaves all
and in another (e.g black) all other other intensity levels in the image
intensities unchanged. It preserves the
background
Intensity-Level Slicing: Approach 1
In the first approach, we display the desired range of intensities in white
and suppress all other intensities to black or vice versa. This results in a
binary image. The transformation function for both the cases is shown
below.
Intensity-Level Slicing: Approach 2
In the second approach, we brighten or darken the desired range of
intensities(a to b as shown below) and leave other intensities unchanged or
vice versa. The transformation function for both the cases, first where the
desired range is changed and second where it is unchanged, is shown below.
Intensity-Level Slicing (gray level slicing)
example: apply intensity level slicing in Matlab to read
cameraman image , then If the pixel intensity in the old image
is between (100  150) convert it in the new image into 255
(white). Otherwise convert it to 0 (black).
Solution:
x=imread('cameraman.tif');
y=x;
[w h]=size(x);
for i=1:w
for j=1:h
if x(i,j)>=100 && x(i,j)<=150
y(i,j)=255;
else
y(i,j)=0;
end
end
end
figure, imshow(x);
figure, imshow(y);
Intensity-Level Slicing (gray level slicing)
example: apply intensity level slicing in Matlab to read
cameraman image , then If the pixel intensity in the old image
is between (100  150) convert it in the new image into 255
(white). Otherwise convert it to 0 (black).
Intensity-Level Slicing (gray level slicing)
example: apply intensity level slicing in Matlab to read
cameraman image , then If the pixel intensity in the old image
is between (100  150) convert it in the new image into 255
(white). Otherwise it leaves it the same.
Solution:
x=imread('cameraman.tif');
y=x;
[w h]=size(x);
for i=1:w
for j=1:h
if x(i,j)>=100 && x(i,j)<=200
y(i,j)=255;
else
y(i,j)=x(i,j);
end
end
end
figure, imshow(x);
figure, imshow(y);
Intensity-Level Slicing (gray level slicing)
example: apply intensity level slicing in Matlab to read
cameraman image , then If the pixel intensity in the old image
is between (100  150) convert it in the new image into 255
(white). Otherwise it leaves it the same.
Exercise: Intensity-Level Slicing (gray level slicing)

Example: apply intensity level slicing to the following image,


where:
a. If the intensity in the image is between (50  100) convert it in the new
image into 255 and preserve the background
b. If the intensity in the image is between (50  100) convert it in the new
image into 255, else convert it to 0.

110 120 130 135 110 120 130 135 0 0 0 0

100 94 98 200 100 255 255 200 0 255 255 0

30 60 70 30 30 255 255 30 0 255 255 0

28 29 25 27 28 29 25 27 0 0 0 0

Original Image Sliced Image (a) Sliced Image (b)


approach 2 approach 1
Bit-Plane Slicing
 Remember that pixels are digital numbers composed of 8 bits. Instead
of highlighting gray-level range, we could highlight the contribution
made by each bit.

8-bit Image composed of 8 1-bit planes


Bit-Plane Slicing
Bit-plane slicing :
 Suppose that each pixel in an image is represented by 8 bits.
 Imagine that the image is composed of eight 1-bit planes,
ranging from bit-plane 0 to bit-plane 7
 plane 0 contains all the lowest order bits and plane 7 contains
all the high-order bits.
 The higher-order bits (especially the top four) contain the
majority of the visually significant data.
 The other bit planes contribute to more subtle details in the
image.
 Separating a digital image into its bit planes is useful for
analyzing the relative importance played by each bit of the
image, a process that aids in determining the adequacy of the
number of bits used to quantize each pixel. Also, this type of
decomposition is useful for image compression.
3. Bit-Plane Slicing
3. Bit-Plane Slicing
3. Bit-Plane Slicing (example)
100

We have to use bit get and bit set to extract 8 images;

01100100

Image of bit1: Image of bit2: Image of bit3:


00000000 00000000 00000100 Image of bit4:
00000000
0 0 4 0

Image of bit5: Image of bit6: Image of bit7: Image of bit8:


00000000 00100000 01000000 00000000
0 32 64 0
Bit-Plane Slicing in MATLAB
example: apply bit-plane slicing in Matlab to read cameraman
image , then extract the image of bit 6.
Solution:
x=imread('cameraman.tif');
y=x*0;
[w h]=size(x);
for i=1:w
for j=1:h
b=bitget(x(i,j),6);
y(i,j)=bitset(y(i,j),6,b);

end
end

figure, imshow(x);
figure, imshow(y);
ENHANCEMENT USING
ARITHMETIC/LOGIC
OPERATIONS
ENHANCEMENT USING
ARITHMETIC/LOGIC OPERATIONS
And/Or Masks
Logical Operations for Masking
Consider the following is a pixel in the image:

10101011 10101011
AND AND Anding with 0’s
Anding with 1’s will 11111111 00000000 will give all zero ,
give same pixel ----------- ----------- so it will hide the
pixel
10101011 00000000

10101011 10101011
OR Oring with 1’s
OR
Oring with 0’s will will give all ones,
00000000 11111111
give same pixel so it will hide the
----------- ----------- pixel
10101011 11111111
Image Subtraction: Example
Image Subtraction: Motion Tracking
HISTOGRAM EQUALIZATION
Histogram
Ch3, lesson 4: histogram

Histogram of the image:


Dark
image

Bright
image

Low
contract
image

High
contrast
image
Histogram
Histogram of a digital image
h(rk) = nk
Where:
rk : kth gray level
nk : # of pixels with having gray level rk

 We manipulate Histogram for image enhancement.


 Histogram data is useful in many applications like
image compression and segmentaion.
Histogram of the image:

histogram

h(rk) = nk gray levels ‫ھو ﺗﻣﺛﯾل ﻟﻌدد اﻟﺑﻛﺳل ﻓﻲ ﻛل ﻗﯾﻣﺔ ﻟوﻧﯾﺔ ﻣن درﺟﺎت‬
Where:
rk : kth gray level
nk : # of pixels with having gray level rk
Histogram of the image:
For example: we have 700 pixels having
the intensity value ≈ 160
Histogram Equalization
 Histogram Equalization is an image processing technique
used to improve contrast in images .
 It accomplishes this by effectively spreading out the most
frequent intensity values, i.e. stretching out the intensity
range of the image.
Histogram equalization
We have this image in matlab called
pout.tif, when we plot its histogram it
is showed like this:

Notice that the


pixels intensity
values are
concentrated on
the middle
(low contrast)
Ch3, lesson 5: histogram equalization

Histogram equalization
histogram equalization : is the process for adjusting image
intensities to enhance contrast.
Im matlab : we use histeq function

Histogram
produces
pixels having
values that are
distributed
throughout
the range
Example: Histogram equalization
Histogram equalization of the image:
Notice that histogram
equalization does not
always produce a
good result
Equalization (mathematically)
g(x) = [(T(X)/n) * L] -1
g(x) = (L/n). T(X) -1
Where,
G(X) : the new image after equalization
L: No of gray levels 2K
n: No of pixels
T(x): cumulative sum of each gray level
Example: Histogram Equalization
 Apply histogram equalization to the following
3-bit image: 0 1 1 2 2 3 4 4
n=4*8
L= 23=8 =32 0 1 1 2 2 3 4 4
L/n = 8/32 0 1 1 2 2 3 4 4
= 0.25
0 1 1 2 2 3 4 4

r Freq.(r) T(r) S= (L/n) * T(r) -1


0 4 4 0.25* 4 – 1 = 0
1 8 12 0.25* 12 – 1 = 2
2 8 20 0.25* 20 – 1 = 4
3 4 24 0.25* 24 – 1 = 5
4 8 32 0.25* 32 – 1 = 7
0 1 2 3 4 5 6 7
Example: Histogram Equalization

0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7

Before histogram After histogram


equalization equalization
Equalization
L ‫ ﻋدد اﻟﺑﻛﺳل ﻟﻛل‬X ‫ﻣﺟﻣوع ﺗراﻛﻣﻲ‬T(X) G(x)
grayl Graylevel ‫ﻟﻠﺑﻛﺳل‬ Assume that we have
evels (3bits per pixels) or 8
levels of grayscale,
0 1 1 0 and we want to
equalize the following
1 3 4 0 image example.
2 5 9 1
3 6 15 2 G(x)=(L/n). T(X) -1
=(8/32). T(x) -1
4 6 21 4
5 6 27 5
6 2 29 6
7 3 32 7

8 ‫ﻋدد ال‬
‫ ﻋدد اﻟﺑﻛﺳﻼت اﻟﻛﻠﻲ‬No of pixels
graylevel
Example
Spatial filters
Remember that types of neighborhood:

 intensity transformation: neighborhood of size 1x1


 spatial filter (or mask ,kernel, template or window):
neighborhood of larger size , like 3*3 mask

 It is the procedure of moving the location of neighbourhood and


performing a predefined operation. so the spatial filter mask is moved
from point to point in an image. At each point (x,y), the response of the
filter is calculated
Spatial Filtering
Spatial
Filters
Spatial filters
Spatial filters

10
19/07/2020 4
Spatial filters
1)Smoothing filters - low pass
2)Sharpening filters – high pass
Spatial filters : Smoothing ( low pass)
Use for:
1)blurring
2) noise reduction.

How it works? The value of every pixel is replaced


by the average of the gray levels in the
neighborhood.

Type of smoothing filters:


1. Standard average linear
2. weighted average.

3. Median filter Order statistics


Spatial filters : Smoothing
linear smoothing : averaging kernels
The output (response) of a smoothing, linear spatial filter is simply the
average of the pixels contained in the neighborhood of the filter mask.

Standard average weighted average.


Desirable effect: the most
application of smoothing is noise
reduction, because random noise
typically consists of sharp
transitions in gray levels,

Undesirable effect: the


undesirable side effect is blur
edges. edges (which almost
always are desirable features of an
image) also are characterized by
sharp transitions in gray levels.
General implementation:
General implementation for filtering an
MxN image with a weighted averaging filter of size
mxn (m and n odd) is given by the expression:

10
19/07/2020 8
Spatial filters : Smoothing
Standard and weighted Average- example

110 120 90 130 The mask is moved


from point to point in
91 94 98 200
an image. At each
90 91 99 100 point (x,y), the
response of the filter
82 96 85 90 is calculated

Standard averaging filter:


(110 +120+90+91+94+98+90+91+99)/9 =883/9 = 98.1

Weighted averaging filter:


(110 +2 x 120+90+2 x 91+4 x 94+2 x 98+90+2 x 91+99)/16 = 97.8
The difference between the std. average and
the weighed average
The following image represent the gray values of an edge. The
following example will explain the effect of standard average on it:

10 10 200 200 10 73 137 200

10 10 200 200 1 1 1 10 73 137 200


1
× 1 1 1
10 10 200 200 9 10 73 137 200
1 1 1
10 10 200 200 10 73 137 200

After applying weighted average:

10 10 200 200 10 58 153 200

10 10 200 200 1 2 1 10 58 153 200


1 2 4 2
10 10 200 200 × 10 58 153 200
16 1 2 1
10 10 200 200 10 58 153 200

As we note applying weighted average reduce the amount of blurring on


the edges
What happens when the Values of the Kernel Fall
Outside the Image??!
First solution :Zero padding,

-ve: black border


border padding
Example on the standard average filter
with zero padding
1 1 1 1/9 1/9 1/9

1
1/9 1/9 1/9
× 1 1 1
≡ 1/9 1/9 1/9
13 12 10 11 12 9
1/9 1/9 1/9
1 1 1 1/9 1/9 1/9
10 11 12 10 11
1/9 1/9 1/9

11 13 200 15 14

10 12 13 13 14
5

10 11 12 13 11

13*1/9 + 12*1/9+ 10*1/9 + 11*1/9 = 5


OR

(13*1 + 12*1+ 10*1 + 11*1)/9 = 5


Example on the standard average filter
with zero padding

1/9 1/9 1/9

13 12 10 11 12 5 8
1/9 1/9 1/9

10 11 12 10 11
1/9 1/9 1/9

11 13 200 15 14

10 12 13 13 14

10 11 12 13 11

13*1/9 +12*1/9+ 10*1/9+10*1/9+11*1/9+12*1/9 = 8

OR

(13*1 + 12*1+ 10*1 +10*1+ 11*1+12*1)/9 = 8


Example on the standard average filter
with zero padding

1/9 1/9 1/9

13 12 10 11 12 5 8 7
1/9 1/9 1/9

10 11 12 10 11
1/9 1/9 1/9

11 13 200 15 14

10 12 13 13 14

10 11 12 13 11

12*1/9+ 10*1/9+11*1/9+11*1/9+12*1/9+10*1/9= 7

OR

(12*1+ 10*1 +11*1+ 11*1+12*1+10*1)/9 = 7


Example on the standard average filter
with zero padding

1/9 1/9 1/9

13 12 10 11 12 5 8 7 7
1/9 1/9 1/9

10 11 12 10 11
1/9 1/9 1/9

11 13 200 15 14

10 12 13 13 14

10 11 12 13 11

(10*1 +11*1+ 12*1+12*1+10*1+11*1)/9 = 7


Example on the standard average filter
with zero padding

1/9 1/9 1/9

13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9

10 11 12 10 11
1/9 1/9 1/9

11 13 200 15 14

10 12 13 13 14

10 11 12 13 11

(11*1+12*1+10*1+11*1)/9 = 5
Example on the standard average filter
with zero padding

13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9

10 11 12 10 11 8
1/9 1/9 1/9

11 13 200 15 14
1/9 1/9 1/9

10 12 13 13 14

10 11 12 13 11

(13*1+12*1+10*1+11*1+11*1+11*1+13*1)/9 = 8
Example on the standard average filter
with zero padding

13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9

10 11 12 10 11 8 32
1/9 1/9 1/9

11 13 200 15 14
1/9 1/9 1/9

10 12 13 13 14

10 11 12 13 11

(13*1+12*1+10*1+10*1+11*1+12*1+11*1+13*1/9+200*1)/9 = 32
Example on the standard average filter
with zero padding

13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9

10 11 12 10 11 8 32 33
1/9 1/9 1/9

11 13 200 15 14
1/9 1/9 1/9

10 12 13 13 14

10 11 12 13 11

(12*1+10*1+11*1+11*1+12*1+10*1+13*1+200*1/9+15*1)/9 = 33
Example on the standard average filter
with zero padding

13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9

10 11 12 10 11 8 32 33 33
1/9 1/9 1/9

11 13 200 15 14
1/9 1/9 1/9

10 12 13 13 14

10 11 12 13 11

(10*1+11*1+12*1+12*1+10*1+11*1+200*1+15*1/9+14*1)/9 = 33
Example on the standard average filter
with zero padding

13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9

10 11 12 10 11 8 32 33 33 8
1/9 1/9 1/9

11 13 200 15 14
1/9 1/9 1/9

10 12 13 13 14

10 11 12 13 11

(11*1+12*1+10*1+11*1+15*1+14*1)/9 = 8
Example on the standard average filter
with zero padding
You have to apply same process till to the end of the image

13 12 10 11 12
5 8 7 7 5
10 11 12 10 11
8 32 33 33 8
11 13 200 15 14
7 32 33 34 9
10 12 13 13 14
1/9 1/9 1/9
7 32 34 34 9
10 11 12 13 11
1/9 1/9 1/9
5 8 8 8 6

1/9 1/9 1/9


As you can see, we successfully
(13*1+14*1+13*1+11*1)/9 = 6 remove the noise (200) and the gray
values are very close to each other
except the pixels on boundary it have
low gray value because of zero
padding and this will produce black
border and we can use replicate border
to eliminate black border.
Example on the standard average filter
with replicate border
Spatial filtering with replicate border

13 12 10 11 12
12 11 11 11 11

10 11 12 10 11
12 32 33 33 12

11 13 200 15 14
11 32 33 34 13

10 12 13 13 14
11 32 34 34 13

10 11 12 13 11
10 11 12 12 12

As you can see, the gray values of


boundary’s pixel are not affected too
much with averaging filter
Spatial filters : Smoothing
Averaging effects: blurring + reducing noise
Original image 3 x 3 averaging

5 x 5 averaging 9 x 9 averaging

15 x 15 averaging 35 x 35 averaging
Spatial filters : Smoothing
order statistics: Median filter
the median filter is based on
ordering (ranking) the pixels ,
110 120 90 130 then replacing the value of a pixel
becomes
by the median of the gray levels in
91 94 98 200 95 the neighborhood of that pixel.
90 95 99 100
Median filters are quite popular
82 96 85 90 because, for certain types of
random noise, they provide
excellent noise reduction +
Steps: less blurring than linear
smoothing filters of similar size.
1. Sort the pixels in ascending order:
90,90, 91, 94, 95, 98, 99, 110, 120 Median filters are particularly
effective in the presence of
2. replace the original pixel value by the median : impulse noise, also called salt-
and-pepper noise because of its
95 appearance as white and black
dots superimposed on an image.
Spatial filters : Smoothing
order statistics: Median filter
use : blurring + reduce salt and pepper noise

The original The smoothed The smoothed image


image with salt image using using median
and pepper noise averaging
Example on a Median Filter with replicate
border
Apply 3 × 3 median filter on the following image.

Salt noise

150 151 155 150 151

152 255 153 150 152 Pepper


noise
153 154 155 0 153

157 158 159 157 155


Example on a Median Filter with replicate
border

151
150 151 155 150 151

152 255 153 150 152

153 154 155 0 153

157 158 159 157 155

With replicate border


{ 150, 150, 151, 150, 150, 151, 152, 152, 255}
After sorting:
{ 150, 150, 150, 150, 151, 151, 152, 152, 255}
The median value is 151
Example on a Median Filter with replicate
border

151 152
150 151 155 150 151

152 255 153 150 152

153 154 155 0 153

157 158 159 157 155

With replicate border


{150, 151, 155, 150, 151, 155, 152, 255, 153}
After sorting:
{ 150, 150, 151, 151, 152, 153, 155, 155, 255}
The median value is 152
Example on a Median Filter with replicate
border

150 151 155 150 151 151 152 151

152 255 153 150 152

153 154 155 0 153

157 158 159 157 155

With replicate border


{151, 155, 150, 151, 155, 150, 255, 153, 150}
After sorting:
{ 150, 150, 150, 151, 151, 153, 155, 155, 255}
The median value is 151
Example on a Median Filter with replicate
border

150 151 155 150 151 151 152 151 151

152 255 153 150 152

153 154 155 0 153

157 158 159 157 155

With replicate border


{155, 150, 151, 155, 150, 151, 153, 150, 152}
After sorting:
{150, 150, 150, 151, 151, 152, 153, 155, 155}
The median value is 151
Example on a Median Filter with replicate
border

150 151 155 150 151 151 152 151 151 151

152 255 153 150 152

153 154 155 0 153

157 158 159 157 155

With replicate border


{150, 151, 151, 150, 151, 151, 150, 152, 152}
After sorting:
{150, 150, 150, 151, 151, 151, 151, 152, 152}
The median value is 151
Example on a Median Filter with replicate
border

150 151 155 150 151 151 152 151 151 151

152 255 153 150 152 152

153 154 155 0 153

157 158 159 157 155

With replicate border


{150, 150, 151, 152, 152, 255, 153, 153, 154}
After sorting:
{150, 150, 151, 152, 152, 153, 153, 154, 255}
The median value is 152
Example on a Median Filter with replicate
border

150 151 155 150 151 151 152 151 151 151

152 255 153 150 152 152 153

153 154 155 0 153

157 158 159 157 155

With replicate border


{150, 151, 155, 152, 255, 153, 153, 154, 155}
After sorting:
{150, 151, 152, 153, 153, 154, 155, 155, 255}
The median value is 153
Example on a Median Filter with replicate
border

150 151 155 150 151 151 152 151 151 151

152 255 153 150 152 152 153 153

153 154 155 0 153

157 158 159 157 155

With replicate border


{151, 155, 150, 255, 153, 150, 154, 155, 0}
After sorting:
{0, 150, 150, 151, 153, 154, 155, 155, 255}
The median value is 153
Example on a Median Filter with replicate
border

150 151 155 150 151 151 152 151 151 151

152 255 153 150 152 152 153 153 152

153 154 155 0 153

157 158 159 157 155

With replicate border


{155, 150, 151, 153, 150, 152, 155, 0, 153}
After sorting:
{0, 150, 150, 151, 152, 153, 153, 155, 155}
The median value is 152
Example on a Median Filter with replicate
border

150 151 155 150 151 151 152 151 151 151

152 255 153 150 152 152 153 153 152 151

153 154 155 0 153

157 158 159 157 155

With replicate border


{150, 151, 151, 150, 152, 152, 0, 153, 153}
After sorting:
{0, 150, 150, 151, 151, 152, 152, 153, 153}
The median value is 151
Example on a Median Filter with replicate
border

151 152 151 151 151


150 151 155 150 151

152 255 153 150 152 After applying 152 153 153 152 151
3×3 Median filter
154 155 155 153 153
153 154 155 0 153

157 157 157 155 155


157 158 159 157 155

 As we note, the salt and pepper noise is removed completely


from this image without burring effect
Spatial filters : Sharpening ( high pass)
3.6 Sharpening Spatial Filters

 The principal objective of sharpening is to highlight


transition in intensity.
 Sharpening is accomplished by spatial differentiation.
 Image differentiation enhances edges and other
discontinuities (such as noise) and deemphasizes areas
with slowly varying gray-level values

14
19/07/2020 1
Spatial filters : Sharpening ( high pass)
Example on a Sharpening:

= +
50 60 90 100 -10 -20 20 10 40 40 110 110

50 60 90 100 -10 -20 20 10 40 40 110 110

50 60 90 100 -10 -20 20 10 40 40 110 110

50 60 90 100 -10 -20 20 10 40 40 110 110


3.6.1 Foundation
 The derivatives of a digital function are defined in
terms of differences
 Definitions of the first and 2nd-order derivatives of a
1-D function f(x) are the differences:

50 50 60 90 100 100 100

0 10 30 10 0 0

10 20 -20 -10 0

19/07/2020
3.6.2 Using the Second Derivative for Image Sharpening–The
Laplacian
 The Laplacian, for a function (image) f(x, y) of two variables, is
defined and given below:

14
19/07/2020 4
Ex: apply the second derivative
20 20 20 20 on the shaded pixel
20 10 10 10
= 20 + 20 + 10 + 10 − 4 ∗ 10
20 10 10 10
= 20
20 10 10 10

= 10 + 10 + 10 + 10 − 4 ∗ 10
=0
Spatial filters : Sharpening ( high pass)

1.LAPLACE
2.Unsharp
3.High boost
4.sobel
2nd Derivative for Image Sharpening–The
Laplacian
2nd Derivative for Image Sharpening–The
Laplacian
Spatial filters : Sharpening
1) LAPLACE (2nd derivative)
The Laplacian (Better Implementation)
Composite Laplacian Filter

( , )= , − + , − − , − , + − ( , − )

, = , + ( ,

, = , + , − + , − − , − , + − ( , − )

, = , − + , − − , − , + − ( , − )
The Laplacian
(more practical
masks)
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

= +
50 60 90 100 Laplacian -10 -20 20 10 40 40 110 110

50 60 90 100 0 -1 0 -10 -20 20 10 40 40 110 110

50 60 90 100 -1 4 -1 -10 -20 20 10 40 40 110 110

50 60 90 100 0 -1 0 -10 -20 20 10 40 40 110 110

Composite
50 60 90 100 Laplacian 40 40 110 110

50 60 90 100 0 -1 0 40 40 110 110

50 60 90 100 -1 5 -1 40 40 110 110

50 60 90 100 0 -1 0 40 40 110 110


Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

0 -1 0
= +
50 60 90 100 -10 40
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100

50 60 90 100

= 4 ∗ 50 − 50 − 50 − 60 − 50 , = 50 − 10
= 200 − 210
, = 40
= −10
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

0 -1 0
= +
50 60 90 100 -10 -20 40 40
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100

50 60 90 100

= 4 ∗ 60 − 50 − 60 − 60 − 90 , = 60 − 20
= 240 − 260
, = 40
= −20
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

0 -1 0
= +
50 60 90 100 -10 -20 20 40 40 110
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100

50 60 90 100

= 4 ∗ 90 − 90 − 90 − 60 − 100 , = 90 + 20
= 360 − 340
, = 110
= 20
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

0 -1 0
= +
50 60 90 100 -10 -20 20 10 40 40 110 110
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100

50 60 90 100

= 4 ∗ 100 − 100 − 100 − 100 − 90 , = 100 + 10


= 400 − 390
, = 110
= 10
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

= +
50 60 90 100 -10 -20 20 10 40 40 110 110
0 -1 0
50 60 90 100 -10 40
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100

= 4 ∗ 50 − 50 − 50 − 50 − 60 , = 50 − 10
= 200 − 210
, = 40
= −10
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

= +
50 60 90 100 -10 -20 20 10 40 40 110 110
0 -1 0
50 60 90 100 -10 -20 40 40
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100

= 4 ∗ 60 − 50 − 60 − 60 − 90 , = 60 − 20
= 240 − 260
, = 40
= −20
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

= +
50 60 90 100 -10 -20 20 10 40 40 110 110
0 -1 0
50 60 90 100 -10 -20 20 40 40 110
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100

= 4 ∗ 90 − 90 − 90 − 60 − 100 , = 90 + 20
= 360 − 340
, = 110
= 20
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

= +
50 60 90 100 -10 -20 20 10 40 40 110 110
0 -1 0
50 60 90 100 -10 -20 20 10 40 40 110 110
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100

= 4 ∗ 100 − 100 − 100 − 100 − 90 , = 100 + 10


= 400 − 390
, = 110
= 10
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

= +
50 60 90 100 -10 -20 20 10 40 40 110 110

50 60 90 100 -10 -20 20 10 40 40 110 110

50 60 90 100 -10 -20 20 10 40 40 110 110


0 -1 0
50 60 90 100 -10 -20 20 10 40 40 110 110
-1 4 -1

0 -1 0

= 4 ∗ 100 − 100 − 100 − 100 − 90 , = 100 + 10


= 400 − 390
, = 110
= 10
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

0 -1 0
50 60 90 100 40
-1 5 -1
50 60 90 100
0 -1 0
50 60 90 100

50 60 90 100

( , ) = 5 ∗ 50 − 50 − 50 − 60 − 50
= 250 − 210
= 40
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

0 -1 0
50 60 90 100 40 40
-1 5 -1
50 60 90 100
0 -1 0
50 60 90 100

50 60 90 100

( , ) = 5 ∗ 60 − 50 − 60 − 60 − 90
= 3000 − 260
= 40
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

0 -1 0
50 60 90 100 40 40 110
-1 5 -1
50 60 90 100
0 -1 0
50 60 90 100

50 60 90 100

( , ) = 5 ∗ 90 − 90 − 90 − 60 − 100
= 450 − 340
= 110
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge

50 60 90 100 40 40 110 110

50 60 90 100 40 40 110 110

50 60 90 100 40 40 110 110


0 -1 0
50 60 90 100 40 40 110 110
-1 5 -1

0 -1 0

( , ) = 5 ∗ 100 − 100 − 100 − 100 − 90


= 500 − 390
= 110
Spatial filters : Sharpening
LAPLACE – 2st derivative
Use: for highlighting fine detail or
enhancing detail that has been blurred.
Example: apply the following
laplace on the highlighted pixel

153 157 156 153 155

159 156 158 156 159 154*4 – 158- 156-158-158 = -14


155 158 154 156 160
So the value after filter = -14
154 157 158 160 160
We call the resultant image: sharpened image.
157 157 157 156 155

Filtered image=original +sharpened image


The value in the filter image=154-14 =140
Sharpening Spatial filters

Ramp
edge

Step
edge
Ramp
edge 5 5 5 6 7 8 9 9 9

1st 0 0 1 1 1 1 0 0
derivative

2nd
derivative
0 0 1 0 0 0 -1 0
Spatial filters : Sharpening
1st VS 2nd derivative sharpening
1st derivative sharpening produces thicker edges in an image
1st derivative sharpening has stronger response to gray level
change

2nd derivative sharpening has stronger response to fine


details, such as thin lines and isolated points.

2nd derivative sharpening has double response to gray level


change
1st vs 2nd
derivative
Unsharp Masking
Unsharp Masking
To sharpen images by subtracting a blurred or unsharp
(smoothed) version of an image from the original image
itself.
 This process, called unsharp masking, consists of:
1. Blur the original image.

2. Subtract the blurred image from the original image (the


resulting difference is called the mask).

3. Add the mask to the original.


17
19/07/2020 4
Unsharp masking and Highboost filtering expressions:

 Let ̅( , ) denote the blurred image.


 The mask is obtained as:
( , ) = ( , ) − ̅( , )
 A weighted portion of mask is added to original:
( , )= ( , )+ ∗ ( , )
 Unsharp Making: when k=1
 Highboost filtering: when > 1

17
19/07/2020 5
17
19/07/2020 6
Example
Example: Apply Unsharp filter on the following blurred edge

= −
50 60 90 100 Std. avg 53 67 83 97 -3 -7 7 3

50 60 90 100 1/9 1/9 1/9 53 67 83 97 -3 -7 7 3

50 60 90 100 1/9 1/9 1/9 53 67 83 97 -3 -7 7 3

50 60 90 100 1/9 1/9 1/9 53 67 83 97 -3 -7 7 3

_ ℎ = + As we note, the difference _ℎ ℎ = +3∗


between the resulted edge
47 53 97 103 (53,97) is larger than the 41 39 111 109

original edge (60,90), but if


47 53 97 103 41 39 111 109
this sharpening is not enough
47 53 97 103 we will try to enlarge the 41 39 111 109
value of the mask using high-
47 53 97 103 boost filter by multiplying 41 39 111 109
with value >1
1st Derivatives of Enhancement – The
Gradient
1st Derivatives of Enhancement – The
Gradient
1st Derivatives of Enhancement – The
Gradient
1st Derivatives of Enhancement – The
Gradient
1st Derivatives of Enhancement – The
Gradient
Robert’s Operators
Example: Apply the Robert’s operators on the pixel Z5

Z1 Z2 Z3 Z1 Z2 Z3

Z4 Z5 Z6 Z4 Z5 Z6
-1 0 0 -1
Z7 Z8 Z9 Z7 Z8 Z9
0 1 1 0

Gx = Z5*-1 +Z6 *0+Z8*0+Z9*1 Gy = Z5*0+Z6 *-1+Z8*1+Z9*0

Gx = Z9 -Z5 Gy = Z8 –Z6

Gradient = |Z9 -Z5| + |Z8 –Z6|


Sobel Operators
Example: Apply the Sobel operators on the pixel Z5

Z1 Z2 Z3 Z1 Z2 Z3
-1 -2 -1 -1 0 1

Z4 Z5 Z6 Z4 Z5 Z6
0 0 0 -2 0 2
Z7 Z8 Z9 Z7 Z8 Z9
1 2 1 -1 0 1

Gx = Z1*-1 + Z2 *-2+ Z3*-1 + Gx = Z1*-1 + Z4 *-2+ Z7*-1 +


Z4*0+ Z5 *0 + Z6*0 + Z7*1 + Z8 Z2*0+ Z5 *0 + Z8*0 + Z3*1 + Z6
*2+ Z9*1 *2+ Z9*1

Gx = -Z1-2Z2-Z3+Z7+2Z8+Z9 Gx = -Z1-2Z4-Z7+Z3+2Z6+Z9

Gx =( Z7+2Z8+Z9) –( Z1+2Z2+Z3) Gx =( Z3+2Z6+Z9) –( Z1+2Z4+Z7)

Gradient=|(Z7+2Z8+Z9)–( Z1+2Z2+Z3)|+|(Z3+2Z6+Z9)–(Z1+2Z4+Z7)|
Sobel Operators

Detects horizontal edges Detects vertical edges


Sobel operators

we can apply the sobel horizontal kernel or the


sobel vertical kernel or both and adding them
together.
Example: Find the gradient of the following shaded pixels:
P2 P4 Gx Gy
-1 -2 -1 -1 0 1
10 10 10 200 200 200
P1 0 0 0 -2 0 2
10 10 10 200 200 200 1 2 1 -1 0 1

10 10 10 200 200 200


P3
100 100 100 20 20 20
Gx(P1) = -10-20-10+0+0+0+10+20+10
=0
100 100 100 20 20 20 10 10 10 200 200 200
-1 -2 -1

100 100 100 20 20 20 10 10 10 200 200 200


0 0 0
10 10 10 200 200 200
1 2 1
100 100 100 20 20 20
10 10 10 200 200 200
-1 0 1
100 100 100 20 20 20
10 10 10 200 200 200
-2 0 2
100 100 100 20 20 20
10 10 10 200 200 200
-1 0 1

100 100 100 20 20 20


Gy(P1) = -10+0+10-20+0+20-10+0+10
100 100 100 20 20 20 =0 1 = +
100 100 100 20 20 20 1 =0
The gradient is zero because P1 is
located within a constant area
Example: Find the gradient of the following shaded pixels:

Gx(P2) Gy(P2)

10 10 10 200 200 200 10 10 10 200 200 200


-1 -2 -1 -1 0 1
10 10 10 200 200 200 10 10 10 200 200 200
0 0 0 -2 0 2
10 10 10 200 200 200 10 10 10 200 200 200
1 2 1 -1 0 1
100 100 100 20 20 20 100 100 100 20 20 20

100 100 100 20 20 20 100 100 100 20 20 20

100 100 100 20 20 20 100 100 100 20 20 20

Gx(p2) = -10-20-200+0+0+0+10+20+200 = 0

Gy(p2) = -10+0+200-20+0+400-10+0+200 = 760


2 = +
2 = 0 + 760 = 760
The Gy is non-zero because P2 is located on a vertical edge
Example: Find the gradient of the following shaded pixels:

Gx(P3) Gy(P3)

10 10 10 200 200 200 10 10 10 200 200 200

10 10 10 200 200 200 10 10 10 200 200 200


-1 -2 -1 -1 0 1
10 10 10 200 200 200 10 10 10 200 200 200
0 0 0 -2 0 2
100 100 100 20 20 20 100 100 100 20 20 20
1 2 1 -1 0 1
100 100 100 20 20 20 100 100 100 20 20 20

100 100 100 20 20 20 100 100 100 20 20 20

Gx(P3) = -10-20-10+0+0+0+100+200+100 = 360


Gy(P3) = -10+0+10-20+0+20-100+0+100 = 0

3 = +
3 = 0 + 360 = 360
The Gx here is non-zero because P3 is located on a horizontal edge
Example: Find the gradient of the following shaded pixels:

Gx(P4) Gy(P4)

10 10 10 200 200 200 10 10 10 200 200 200

10 10 10 200 200 200 10 10 10 200 200 200


-1 -2 -1 -1 0 1
10 10 10 200 200 200 10 10 10 200 200 200
0 0 0 -2 0 2
100 100 100 20 20 20 100 100 100 20 20 20
1 2 1 -1 0 1
100 100 100 20 20 20 100 100 100 20 20 20

100 100 100 20 20 20 100 100 100 20 20 20

Gx(P4) = -10-20-200+0+0+0+100+200+20 = 90
Gy(P4) = -10+0+200-20+0+400-100+0+20 = 490

4 = +
4 = 90 + 490 = 580
The Gx and Gy here are non-zero because P4 is located on a
horizontal and vertical edges
1st Derivatives of Enhancement – The
Gradient
Combining spatial Enhancement
Methods
Combining spatial Enhancement
Methods
Combining spatial Enhancement
Methods
 Our objective is to enhance the image by sharpening it and by
bringing out more of the skeletal detail.
 The narrow dynamic range of the gray levels and high noise
content make this image difficult to enhance.
 The strategy we will follow is:
 to utilize the Laplacian to highlight fine detail, and
 the gradient to enhance prominent edges.
 a smoothed version of the gradient image will be used to
mask the Laplacian image
 Finally, we will attempt to increase the dynamic range of
the gray levels by using a gray-level transformation.
19
4
Thank
you
Conclusions

19
6
MATLAB (average +sobel+laplace)
Fspecial : for choosing the filter:
X=fspecial(‘average’,[3 3])
p=fspecial(‘laplacian’, 0)
v=fspecial(‘sobel’)  horizontal sobel
Y=v’  vertical sobel

Imfilter : for applying filter.


m= imread(‘cameraman.tif‘);
f= imfilter(m,x)  this command will apply average filter on image
Fp=imfilter(m,p)  this command will apply laplace filter on image
Imshow(fp)  this command will show the laplace sharpened image
imshow(m+fp)  this command will show the filtered image after applying
la place
MATLAB – cont. (unsharp +
highboost)
Applying unsharp:.
X=imread(‘cameraman.tif‘)
p=fspecial(’average’,[3 3])
xblur=imfilter(x,p)  for bluring

Mask= xblur-x  for subtracting the mask

Y=x+mask  for unsharp the image

Revise slide 57 + 58

For highboost , only multiply the mask any any constant in the
last step: y=x+3*mask for example
MATLAB – cont. (border padding)
Ex.
X=imread(‘cameraman.tif‘)
p=fspecial(‘laplacian’, 0)
Xp=imfilter(x,p, ‘replicate‘)

This command will apply border padding


instead of zero padding

You might also like