You are on page 1of 9

Image processing Lab

Lab # 4

Image Enhancement in
The Spatial Domain I

May, 2020
1. Objectives
Performing image enhancement using different type of intensity transformations.
Obtaining the histogram of an image.

2. Theory
Part1: Enhancement
The first part of this experiment will deal with enhancement. The principal objective of
enhancement is to process an image so that the result is more suitable than the original
image for a specific application. The enhancement approaches utilized in this experiment
are used for the sake of general-purpose contrast manipulation. These approaches are
referred to as point processing (because they manipulate still-pixel images).

Image enhancement is a very basic image processing task that defines us to have a better
subjective judgment over the images. And Image Enhancement in spatial domain (that is,
performing operations directly on pixel values) is the very simplistic approach.
Enhanced images provide better contrast of the details that images contain. Image
enhancement is applied in every field where images are ought to be understood and
analyzed, For example, Medical Image Analysis, Analysis of images from satellites, etc.

Image enhancement simply means, transforming an image f into image g using T. Where
T is the transformation. The values of pixels in images f and g are denoted by r and s,
respectively.
The general form of the enhancement approach is:

S = T(r)

where T is a transformation that maps a pixel value (intensity) r into a pixel value s.
The results of this transformation are mapped into the gray scale range as we are dealing
here only with gray scale digital images. So, the results are mapped back into the range
[0, L-1], where L=2k, k being the number of bits in the image being considered. So, for
instance, for an 8-bit image the range of pixel values will be [0, 255].

The gray-level transformation which to be studied here are:


Linear (negative and identity transformation)
Logarithmic (log and inverse-log transformation)
Power-law (nth power and nth root transformation)

The transformation map plot shown next depicts various curves that fall into the above
three types of enhancement techniques.

1
Figure A: Plot of various transformation functions

The Identity and Negative curves fall under the category of linear functions. Identity
curve simply indicates that input image is equal to the output image. The Log and
Inverse-Log curves fall under the category of Logarithmic functions, nth root and nth
power transformations fall under the category of Power-Law functions.

I. Image Negatives: assume the gray level range is [0, L-1]:

S = L-1-r

This expression results in reversing of the gray level intensities of the image
thereby producing a negative like image. The output of this function can be
directly mapped into the gray scale look-up table consisting values from 0 to L-1.

Useful for enhancing white and gray details embedded in black regions.

II. Log Transformations:

S = c log(1+r)

- Where c is a constant and it is assumed that r≥0.


- Stretch low gray levels and compress high gray level.

The shape of the log curve in fig. A tells that this transformation maps a narrow
range of low-level grey scale intensities into a wider range of output values. And
similarly maps the wide range of high-level grey scale intensities into a narrow
range of high level output values. The opposite of this applies for inverse-log
transform. This transform is used to expand values of dark pixels and compress
values of bright pixels.

III. Power-Law Transformations:

S = c rγ
- where c and γ are positive constants
- γ<1 T plays as log transformation.
- γ>1 T plays as inverse log transformation.
- c=γ=1 Identity function

This transformation function is also called as gamma correction. For various


values of γ different levels of enhancements can be obtained. This technique is
quite commonly called as Gamma Correction. If you notice, different display
monitors display images at different intensities and clarity. That means, every
monitor has built-in gamma correction in it with certain gamma ranges and so a
good monitor automatically corrects all the images displayed on it for the best
contrast to give user the best experience.

Gamma can take any value between 0 and infinity. If gamma is 1 (the default),
the mapping is linear. If gamma is less than 1, the mapping is weighted toward
higher (brighter) output values. And if gamma is greater than 1, the mapping is
weighted toward lower (darker) output values.

Fig.B illustrates the three transformation curves showing how values are mapped
when gamma is less than, equal to, and greater than 1. In each graph, the x-axis
represents the intensity values in the input image, and the y-axis represents the
intensity values in the output image.

Figure B: Transformation curves for gamma < 1, gamma = 1, and gamma > 1.

The difference between the log-transformation function and the power-law


functions is that using the power-law function a family of possible transformation
curves can be obtained just by varying the γ as shown in the following figure:
These are the three basic image enhancement functions for gray scale images that can be
applied easily for any type of image for better contrast and highlighting. Using the image
negation formula given above, it is not necessary for the results to be mapped into the
gray scale range [0, L-1]. Output of L-1-r automatically falls in the range of [0, L-1]. But
for the Log and Power-Law transformations resulting values are often quite distinctive,
depending upon control parameters like γ and logarithmic scales. So the results of these
values should be mapped back to the gray scale range to get a meaningful output image.
For example, Log function s = c log(1 + r) results in 0 and 2.41 for r varying between 0
and 255, keeping c=1. So, the range [0, 2.41] should be mapped to [0, L-1] for getting a
meaningful image.

Part2: Histogram
The second part of this experiment will deal with the histogram of a digital image. For
a gray-level image in the range [0, L-1], the histogram is a discrete function:

h(rk) = nk

Where rk is the kth gray level and nk is the number of pixels in the image having level r k.
It is a common practice to normalize the histogram by dividing each of its values by the
total number of pixels in the image, denoted by n.

Thus a normalized histogram is given by:

p(rk) = nk/n

p(rk) gives an estimate of the probability of occurrence of a gray level rk.


In Matlab to display histogram of image data use this function:

imhist(I)

imhist(I) displays a histogram for the intensity image I whose number of


bins are specified by the image type.
If I is a grayscale image, imhist uses 256 bins as a default value.
If I is a binary image, imhist uses only 2 bins.

3. Exercises
Exercise1: Image Negatives
%img_neg.m
close all;
clear all;
I=imread('Lab4_1.jpg');
I=im2double(I);
for i=1:size(I,1)
for
j=1:size(I,2)
I1(i,j)=1-I(i,j);
end
end
subplot(121),imshow(I),title('original image')
subplot(122),imshow(I1),title('enhanced image (image negative)')
Output:
Exercise2: Power-Law Transformations

%power_tr.m
close all;
clear all;
clc;

I=imread('Lab4_2.tif');
I=im2double(I);

c=input('Enter the value of the constant c=');


g=input('Enter the value of gamma g=');

for i=1:size(I,1)
for j=1:size(I,2)
I3(i,j)=c*I(i,j)^g;
end
end

subplot(121), imshow(I),title('original image')


subplot(122), imshow(I3),title('power-low transformation')

Output:

Enter the value of the constant c=1


Enter the value of gamma g=0.2 % for gamma value less than 1 u gets Bright image
Enter the value of the constant c=1
Enter the value of gamma g=1 % for gamma value equals to 1 the result will be the same image

Enter the value of the constant c=1


Enter the value of gamma g=5 % for gamma value greater than 1 u gets dark image

Exercise3: Image Histogram


%hist_ex.m
close all;
clear all;
i=imread('Lab4_4.jpg');
I=rgb2gray(i);
m=im2bw(I,1); %Black
subplot 121,imhist(I),title('Gray Image');
subplot 122,imhist(m),title('Binary Image');
Output:

BW = im2bw(I, level) converts the grayscale image I to a binary image.


The output image BW replaces all pixels in the input image with luminance
greater than level with the value 1 (white) and replaces all other pixels with the
value 0 (black).
Specify level in the range [0,1]. This range is relative to the signal levels possible
for the image's class. Therefore, a level value of 0.5 is midway between black and
white, regardless of class.
If you do not specify level, im2bw uses the value 0.5.

Homework

1. Enhance Lab4_1.jpg image using the three intensity transformations described


above. Let c =1 always. Use different values of γ (0.3, 0.4, 0.6, 1.0, 3.0, and 5.0)
for each image.

2. Comment on the results you have got.

3. Plot the histogram of the original and the enhanced imaged you got in section 1,
use γ=1.0.

4. Comment on the results

You might also like