Digital Image Processing and Analysis Laboratory 4: Image Enhancement
Digital Image Processing and Analysis Laboratory 4: Image Enhancement
An example
>> [img, map] = imread(’detalj.png’); % Read the indexed image
>> imhist(img,map) % calculate and display the histogram
% with a corresponding color palette
>> [img, map] = imread(’salona.png’); % Read the grayscale image
>> imhist(img) % In this case, palette is not necessary
4.1.1 Problems
1. Select several images and display their first order histograms. What do the axis of the histograms
represent?
1
For more details on histeq() function call MATLAB’s built-in help:
4.2.1 Problems
1. Read the images uskoci1.png and salona.png. Display their first order histograms before and after
the histogram equalization.
An example
>> [img,map]=imread(’salona.png’); % Read the image
>> img=ind2gray(img,map); % Convert it to grayscale
>> imgEQ=histeq(img); % Equalize the histogram to obtain approx. uniform distrib
>> imgMEQ=imscale(img,[0 1]); % Rescale the values to [0 1]
>> imgMEQ=norminv(imgMEQ,0,10); % Model the histogram to a normal distribution with a mean
4.3.1 Problems
1. Choose an arbitrary image. Model its histogram to obtain a desired distribution function.
2. Read the image auto.ti f . Model its histogram until the number on the car’s registration plate becomes
visible. (Hint: Check the interval of pixel values in the register plate area, and determine the mapping
of this interval to a larger one.)
An example
>> [img, map] = imread(’medalja_dubrovnik.png’); % Read the image
>> imgGN = imnoise(img,’gaussian’); % Adding the white noise
>> imgSP = imnoise(img,’salt & pepper’); %Adding the salt and pepper / impulse noise
2
Figure 4.1: Scheme of the experiment.
Median filter is a statistical filter which is applied block-wise in image processing: for each input block
of a given size the filter results with a median value of this block. In MATLAB, median filtering is performed
using the function med f ilt2().
Averaging, which also can be viewed as a statistical filter, can be obtained with convolution.
>> imgMF = medfilt2(imgSP,[5 5]); % filtering the noise with a median filter, block size 5x5
>> maska=ones(5); % create the mask of the desired size (5x5)
>> maska=maska/sum2(maska); % averaging the mask
>> maska % display the mask values
% averaging filter is defined with
% a mask whos all elements are the same
maska =
0.0400 0.0400 0.0400 0.0400 0.0400
0.0400 0.0400 0.0400 0.0400 0.0400
0.0400 0.0400 0.0400 0.0400 0.0400
0.0400 0.0400 0.0400 0.0400 0.0400
0.0400 0.0400 0.0400 0.0400 0.0400
>> imgSF = conv2(imgSP,maska,’same’); % Filtering the image with an averaging filter
4.4.1 Problems
1. Choose an arbitrary image. Create 2 noisy images from it: one by adding the Gaussian noise and the
other one by adding the salt&pepper noise.
2. Try denoising these images with median and averaging filters. Comment on the results. Which filter
is more appropriate for which type of the noise?
3
The first applications of this method were registered in Germany in 1930s. However, we can observe
this procedure from another point of view, ie. instead of subtracting the blurred image from the original
one, we can add the image with the emphasized edges to the original one. If the image I consists of low and
high derivatives (I = Il f + Ih f ), then the Eq. 4.1 can be rewritten as:
Ihb (x, y) = AI(x, y) − Il f (x, y) = AIh f (x, y) + AIl f (x, y) − Il f (x, y) = (A − 1)Il f (x, y) + AIh f (x, y) (4.2)
From the obtained expression, we can see that we can increase the sharpness of a blurred image by
adding proper high frequencies to it. This procedure is also called high − boost filtering, therefore we
denote the output image as Ihb . The biggest problem of this procedure is to obtain the high frequencies of
the image. In this exercise we will estimate them by means of derivative estimations. The scheme of the
experiment is given in Fig. 4.2. The image is blurred with an averaging mask of an arbitrary size, ie. 3 × 3.
An example
[img,map]=imread(’blood1.tif’); % read the image
maska=ones(3);
maska=maska/sum(maska(:)); % impulse response of the averaging filter
imgZ=conv2(img,maska,’same’); % image averaging
In a case of one-dimensional signal, the derivative in a point x can be approximated by the following
expression:
f (x + h) − f (x)
∆(x) =
h
In a two-dimensional case, the derivative has it’s direction and the value, and can be estimated with some
of existing operators. In our experiment, we will estimate the first derivative, ie. the gradient, with a Sobel
operator, and the second derivative with a Laplace operator. For that purpose, we use the function f special
for creating the mask that approximates the Laplace operator:
An example
>> lap=fspecial(’laplacian’,0.2) % a mask that approximates the Laplace operator
ans =
0.1667 0.6667 0.1667
0.6667 -3.3333 0.6667
0.1667 0.6667 0.1667
4
4.5.1 Problems
1. Choose an arbitrary image and perform the procedure explained with a scheme in a Fig. 4.2. Display
the difference between the images. How satisfied are you with the increased sharpness (reconstruc-
tion)?
2. Read the image medalja dubrovnik.png and increase her sharpness by subtracting its blurred version
(according to the Eq. 4.1). What did you obtain with this procedure?
3. Read the image medalja dubrovnik.png and increase her sharpness by adding its first derivative esti-
mation, obtained by Sobel operator (according to the Eq. 4.2). What did you obtain?
4. Read the image 5.1.09.tiff from the USC-SIPI database and try to increase the sharpness by adding
the first derivative (gradient) and the second derivative (Laplace operator). Which of these methods
gives better results?
5. What are the results like if you use these masks as Laplacian operator approximations:
1 1 1 0 −1 0
1 8 1 , −1 4 −1 (4.3)
1 1 1 0 −1 0
>> lap1=fspecial(’laplacian’,0)
>> lap2=3*fspecial(’laplacian’,0.5)
6. Try both methods (adding the 1st and the 2nd derivative estimation) on several more images. What
is the relevant difference between adding the 1st and the 2nd derivative to an image? Which method
gives better results?
0 Explanation: While adding of the 2nd order derivative estimation, it is added if the central coefficient of the Laplace mask is
positive, and subtracted if the central coefficient of the Laplace mask is negative.