Image Processing
Chapter(3)
Part 2:Intensity Transformation and
spatial filters
Prepared by: Hanan Hardan
Hanan Hardan 1
Image Enhancement?
Enhancement تحسين الصورة: is to process an image
so that the result is more suitable than the
original image for a specific application.
Enhancement techniques are so varied, and use
so many different image processing approaches
The idea behind enhancement techniques is to
bring out details that are hidden, or simple to
highlight certain features of interest in an image.
Hanan Hardan 2
Image Enhancement?
If we used 256 intensities of grayscale then:
- In dark image the most of pixels value <128
- In bright image the most of pixels value>128
Let x:old image
S:new image
S=x+c where c is constant value
>> S=imadd(x,50); -----------------brighter image
S=x-c where c is constant value
>> S=imsubtract(x,50); -------------darker image
We can add 2 image :
S=x+y (where x and y 2 image have same size)
Hanan Hardan 3
Image Enhancement Methods
Spatial Domain Methods (Image Plane)
Techniques are based on direct manipulation of
pixels in an image
Frequency Domain Methods
Techniques are based on modifying the Fourier
transform of the image.
Combination Methods
There are some enhancement techniques based on
various combinations of methods from the first two
categories
In this chapter, we are going to discuss spatial
domain techniques
Hanan Hardan 4
Spatial domain
Spatial domain processes:
1. intensity transformation(Point operation)
- g depends only on the value of f at(x,y)
1. spatial filter (or mask ,kernel, template or
window)
Hanan Hardan 5
Intensity (Gray-level)transformations
functions
Here, T is called intensity transformation function or (mapping,
gray level function)
g(x,y) = T[f(x,y)]
s r
s= T(r)
s,r : denote the intensity of g and f at any point (x,y) .
In addition, T can operate on a set of input images
Hanan Hardan 6
Intensity transformations functions
Intensity transformation functions fall into 2 approaches:
1) Basic intensity transformations
•Linear Functions:
- Negative Transformation
- Identity Transformation
•Logarithmic Functions:
- Log Transformation
- Inverse-log Transformation
•Power-Law Functions:
- nth power transformation
- nth root transformation
Hanan Hardan 7
Intensity transformations functions
2) piecewise Linear transformation functions.
a) Contrast stretching, thresholding
b) Gray-level slicing
c) Bit-plane slicing
Hanan Hardan 8
Basic intensity transformations
a) Linear ( negative and identity).
b) logarithmic ( Log and Inverse Log) .
c) Power( nth power and nth root).
بالصورة بعد التعديلgray levels التدرجات اللونية
256 سيكون هناك8 bit ان كان في البكسل,فمثال
)L=256( تدرج
Hanan Hardan 9
بالصورة قبل التعديلgray levels التدرجات اللونية
Identity Function
- Output intensities are identical to input
intensities
- This function doesn’t have an effect on an
image, it was included in the graph only for
completeness
- Its expression:
s=r
Hanan Hardan 10
Image Negatives (Negative Transformation)
The negative of an image with gray level in the range
[0, L-1], where L = Largest value in an image, is
obtained by using the negative transformation’s
expression:
s=L–1–r
Which reverses the intensity levels of an input
image , in this manner produces the equivalent of a
photographic negative.
The negative transformation is suitable for enhancing
white or gray detail embedded in dark regions of an
image, especially when the black area are dominant
in size Hanan Hardan 11
Image Negatives (Negative Transformation)
s=L–1–r
Output gray level
Input gray level
Hanan Hardan 12
Image Negatives (Negative Transformation)
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.
Hanan Hardan 13
Image Negatives (Negative Transformation)
Note how much clearer the tissue is in the negative image
Hanan Hardan 14
Image Negatives (Negative Transformation)
Example 1:
the following matrix represents the pixels values of
an 8-bit image (r) , apply negative transform and
find the resulting image pixel values.
Image (r)
100 110 90 95
solution:
L= 28 = 256 98 140 145 135
89 90 88 85
s=L-1-r
102 105 99 115
s =255-r
Apply this transform to Image (s)
each pixel to find the negative 155 145 165 160
157 115 110 120
166 165 167 170
153 150 156 140
Hanan Hardan 15
Image Negatives (Negative Transformation)
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
Hanan Hardan 16
Image Negatives (Negative Transformation)
The negative of an image can be obtained also with IPT
function imcomplement:
g = imcomplement (f);
Hanan Hardan 17
Log Transformations function
The general form of the log transformation:
s = c log (1+r)
Where c is a constant, and r ≥ 0
Log curve maps a narrow range of low gray-level values
in the input image into a wider range of the output
levels.
Used to expand the values of dark pixels in an image
while compressing the higher-level values.
It compresses the dynamic range of images with large
variations in pixel values.
Log functions are particularly useful when the input grey
level values may have an extremely large range of
values
Hanan Hardan 18
Log Transformations function
Logarithmic transformations are implemented using the
expression:
g = c * log (1 + double (f))
Hanan Hardan 19
Log Transformations function
But this function changes the data class of the image to
double, so another sentence to return it back to uint8
should be done:
gs = im2uint8 (mat2gray(g));
Use of mat2gray brings the values to the range [0 1] and
im2uint8 brings them to the range [0 255]
Hanan Hardan 20
Log Transformations function
Example:
>> g = log(1 + double(f));
>> gs = im2uint8(mat2gray(g));
>> imshow(f), figure, imshow (g), figure, imshow(gs);
f g gs
Hanan Hardan 21
Inverse Logarithm Transformation
Do opposite to the log transformations
Used to expand the values of high pixels in
an image while compressing the darker-level
values.
Hanan Hardan 22
Logarithmic Transformations
InvLog Log
Hanan Hardan 23
Power-Law Transformations
Power-law(Gamma) transformations
have the basic form of:
s = c.rᵞ
Where c and ᵞ are positive constants
Map a narrow range of dark input
values into a wider range of output
values or vice versa
Hanan Hardan 24
Power-Law Transformations
Different transformation curves are
obtained by varying ᵞ (gamma)
Hanan Hardan 25
Power-Law Transformations
If gamma <1 :the mapping is weighted
toward brighter output values.
If gamma =1 (default):the mapping is
linear.
If gamma >1 :the mapping is weighted
toward darker output values.
Hanan Hardan 26
Power Law Example
Hanan Hardan 27
Power Law Example
Hanan Hardan 28
Power Law Example
Hanan Hardan 29
Power Law Example
Hanan Hardan 30
Power Law Example
•The images to the right
show a magnetic resonance
(MR) image of a fractured
human spine
•Different curves highlight
different detail
Hanan Hardan 31
Power Law Example
Hanan Hardan 32
Function imadjust
Function imadjust is the basic IPT tool for
intensity transformations of gray-scale
images. It has the syntax:
g = imadjust (f, [low_in high_in], [low_out high_out], gamma)
Hanan Hardan 33
Function imadjust
As illustrated in figure 3.2 (above), this function maps the
intensity values in image f to new values in g, such that
values between low_in and high_in map to values
between low_out and high_out.
Values below low_in and above high_in are clipped; that
is values below low_in map to low_out, and those above
high_in map to high_out.
Hanan Hardan 34
Function imadjust
The input image can be of class uint8, uint16, or double,
and the output image has the same class as the input.
All inputs to function imadjust, other than f, are specified
as values between 0 and 1, regardless of the class of f. If f
is of class uint8, imadjust multiplies the value supplied by
255 to determine the actual values to use; if f is of class
uint16, the values are multiplied by 65535.
Using the empty matrix ([ ]) for [low_in high_in] of for
[low_out high_out] results in the default values [0 1].
If high_out is less than low_out, the output intensity is
reversed.
Hanan Hardan 35
Function imadjust
Parameter gamma specifies the shape of the curve that
maps the intensity values of f to create g. If gamma is
less than 1, the mapping is weighted toward higher
(brighter) output values, as fig 3.2 (a) shows. If gamma is
greater than 1, the mapping is weighted toward lower
(darker) output values. If it is omitted from the function
arguments, gamma defaults to 1 (linear mapping).
Hanan Hardan 36
Function imadjust
Example1:
>> f = imread ('baby-BW.jpg');
>> g = imadjust (f, [0 1], [1 0]);
>> imshow(f), figure, imshow (g);
>> imshow(f), figure, imshow (g);
This Obtaining the negative image
Hanan Hardan 37
f g
Function imadjust
Example2:
>> g = imadjust (f, [0.5 0.75], [0 1], .5);
>> imshow(f), figure, imshow (g);
f g
Hanan Hardan 38
Function imadjust
Example3:
>> g = imadjust (f, [0.5 0.75], [0.6 1], 0.5);
>> imshow(f), figure, imshow (g);
f g
Hanan Hardan 39
Function imadjust
Example4:
>> g = imadjust (f, [ ], [ ], 2);
>> imshow(f), figure, imshow (g);
f g
Hanan Hardan 40
Piecewise-Linear Transformation Functions
Principle Advantage: Some important
transformations can be formulated only as a
piecewise function.
Principle Disadvantage: Their specification
requires more user input that previous
transformations
Types of Piecewise transformations are:
Contrast Stretching
Gray-level Slicing
Bit-plane slicing
Hanan Hardan 41
Contrast Stretching
One of the simplest piecewise linear
functions is a contrast-stretching
transformation, which is used to enhance
the low contrast images.
Low contrast images may result from:
Poor illumination
Wrong setting of lens aperture during image
acquisition.
Hanan Hardan 42
Contrast Stretching
If T(r) has the form as shown in the figure below,
the effect of applying the transformation to every
pixel of f to generate the corresponding pixels in g
would:
Produce higher contrast than the original
image, by:
Darkening the levels below m in the original
image
Brightening the levels above m in the
original image
So, Contrast Stretching: is a simple image
enhancement technique that improves the
contrast in an image by ‘stretching’ the range of
intensity values it contains to span a desired
Hanan Hardan 43
range of values.
Contrast Stretching
(r2, s2)
(r1, s1)
Contrast stretching
Assume that
a: rmin,
b:rmax,
Contrast stretching: (r1,s1)=(rmin,0) , (r2,s2)=(rmax,L-1)
Hanan Hardan 44
Contrast Stretching
Remember that: Example: in the graph, suppose we have the
g(x,y) = T[f(x,y)] following intensities : a=90, b=180, m=100
Or
s= T(r) if r is above 180 ,it becomes 255 in s.
If r is below 90 , it becomes 0,
If r is between 90, 180 , T applies as follows:
Pixels above 180 become 255 when r < 100 , s closes تقتربto zero (darker)
255 when r>100 , s closes to 255 (brighter)
brighter
If r >180; s =255
T= If r <180 and r<90; s=T(r)
If r <90; s =0
darker
This is called contrast stretching, which means that
0 255 the bright pixels in the image will become brighter
and the dark pixels will become darker, this means :
Hanan Hardan 45
higher contrast image.
Pixels less than 90 become 0
Contrast-Stretching Transformation
The function takes the form of:
Where r represents the intensities of the input
image, s the corresponding intensity values in
the output image, and E controls the slope of
the function.
Hanan Hardan 46
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,
Hanan Hardan
this is called 47
contrast stretching>
Contrast-Stretching Transformation
This equation is implemented in MATLAB for the
entire image as
Note the use of eps to prevent overflow if f has
any 0 values.
Hanan Hardan 48
Contrast-Stretching Transformation
Example1:
>>g = 1 ./ (1+ (100 ./(double(f) + eps)) .^ 20);
>> imshow(f), figure, imshow(g);
Hanan Hardan 49
Contrast-Stretching Transformation
Example2:
>> g = 1 ./ (1+ (50 ./(double(f) + eps)) .^ 20);
>> imshow(f), figure, imshow(g);
Hanan Hardan 50
Contrast-Stretching Transformation
Example3:
>> g = 1 ./ (1+ (150 ./(double(f) + eps)) .^ 20);
>> imshow(f), figure, imshow(g);
Hanan Hardan 51
Thresholding
Is a limited case of contrast stretching, it produces a two-
level (binary) image.
Some fairly simple, yet powerful, processing approaches
can be formulated with grey-level transformations. Because
enhancement at any point in an image depends only on the
gray level at that point, techniques in this category often
are referred to as point processing.
Hanan Hardan 52
Thresholding
Assume that (r2, s2)
a: rmin,
b:rmax,
k : intensity
Contrast stretching:
(r1,s1)=(rmin,0) , (r2,s2)=(rmax,L-1)
Thresholding: (r1, s1)
(r1,s1)=(k,0) , (r2,s2)=(k,L-1)
Thresholding:
Hanan Hardan 53
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
Hanan HardanIf r <150; s =0 a binary image!
54
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.
Hanan Hardan 55
Contrast Stretching
Hanan Hardan 56
Application on 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 : meanHanan
intensity
Hardan
level in the image 57
Contrast Stretching
Figure 3.10(a) shows a typical transformation used for
contrast stretching. The locations of points (r1, s1) and (r2,
s2) control the shape of the transformation function.
If r1 = s1 and r2 = s2, the transformation is a linear
function that produces no changes in gray levels.
If r1 = r2, s1 = 0 and s2 = L-1, the transformation
becomes a thresholding function that creates a binary
image.
Intermediate values of (r1, s1) and (r2, s2) produce
various degrees of spread in the gray levels of the output
image, thus affecting its contrast.
In general, r1 ≤ r2 and s1 ≤ s2 is assumed, so the function
is always increasing.
Hanan Hardan 58
Contrast Stretching
Figure 3.10(b) shows an 8-bit image with low contrast.
Fig. 3.10(c) shows the result of contrast stretching,
obtained by setting (r1, s1) = (rmin, 0) and (r2, s2) =
(rmax,L-1) where rmin and rmax denote the minimum and
maximum gray levels in the image, respectively. Thus, the
transformation function stretched the levels linearly from
their original range to the full range [0, L-1].
Finally, Fig. 3.10(d) shows the result of using the
thresholding function defined previously, with r1=r2=m,
the mean gray level in the image.
Hanan Hardan 59
piecewise Linear transformation functions.
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
82 96 85 90
Hanan Hardan 60
solution in matlab:
function a2(x,s)
y=x;
[m n]=size(x);
for i=1:m
for j=1:n
if x(i,j)>= s
y(i,j)=255;
else y(i,j)=0;
end
end
end
figure, imshow(x);
figure, imshow(y);
Hanan Hardan 61