0% found this document useful (0 votes)
27 views11 pages

Dip 05

The document discusses digital image processing techniques for changing image resolution. It defines different types of image resolution including: - Gray level resolution, which refers to the number of distinct gray levels in a digital image. Lower gray level resolution leads to false contouring. - Spatial (pixel) resolution, which is determined by the number of pixels in an image. Higher spatial resolution means more image detail is preserved. The document describes experiments to change gray level resolution by reducing the number of gray levels from 256 to 2, and to change spatial resolution using nearest neighbor interpolation to enlarge images. Implementing these techniques in MATLAB code is demonstrated.

Uploaded by

Noor-Ul Ain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views11 pages

Dip 05

The document discusses digital image processing techniques for changing image resolution. It defines different types of image resolution including: - Gray level resolution, which refers to the number of distinct gray levels in a digital image. Lower gray level resolution leads to false contouring. - Spatial (pixel) resolution, which is determined by the number of pixels in an image. Higher spatial resolution means more image detail is preserved. The document describes experiments to change gray level resolution by reducing the number of gray levels from 256 to 2, and to change spatial resolution using nearest neighbor interpolation to enlarge images. Implementing these techniques in MATLAB code is demonstrated.

Uploaded by

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

UNIVERSITY OF ENGINEERING

SOFTWARE AND TECHNOLOGY


ENGINEERING , TAXILA
DEPARTMENT
FACULTY OFTELECOMMUNICATION AND INFORMATION ENGINEERING

DIGITAL IMAGE PROCESSING

Experiment 5
Implement the effects of changing the Gray level Resolution, Spatial
Resolution via Pixel replication, Nearest Neighbor interpolation

CLO 1: Construct the experiments/projects of varying complexities

CLO 2: Use modern tools and languages.

CLO 3: Demonstrate an original solution of problem under discussion.

CLO 4: Work individually as well as in teams.

Image resolution
Image resolution describes the detail an image holds. The term applies to digital images, film images,
and other types of images. Higher resolution means more image detail.

Digital Image Processing 6th Term-SE UET Taxila


Pixel resolution

The term resolution is often used for a pixel count in digital imaging, even though American,
Japanese, and international standards specify that it should not be so used, at least in the digital
camera field. An image of N pixels high by M pixels wide can have any resolution less than N
lines per picture height, or N TV lines. But when the pixel counts are referred to as resolution,
the convention is to describe the pixel resolution with the set of two positive integer numbers,
where the first number is the number of pixel columns (width) and the second is the number of
pixel rows (height), for example as 640 by 480. Another popular convention is to cite resolution
as the total number of pixels in the image, typically given as number of megapixels, which can be
calculated by multiplying pixel columns by pixel rows and dividing by one million.

Below is an illustration of how the same image might appear at different pixel resolutions, if the
pixels were poorly rendered as sharp squares (normally, a smooth image reconstruction from
pixels would be preferred, but for illustration of pixels, the sharp squares make the point better).

An image that is 2048 pixels in width and 1536 pixels in height has a total of 2048×1536 =
3,145,728 pixels or 3.1 megapixels. One could refer to it as 2048 by 1536 or a 3.1-megapixel
image.

Sampling
Sampling is the principal factor determining the spatial resolution of an image. Basically spatial
resolution is the smallest noticeable detail in an image.

As an example suppose we construct a chart with vertical lines of width W, and with space
between the lines also having width W. A line-pair consists of one such line and its adjacent
space.

Thus width of line pair is and there are line-pairs per unit distance. A widely used
definition of resolution is simply the smallest number of distinct line pairs per unit distance; for
example 100 line pairs/mm.

Gray level resolution


This refers to the smallest distinct change in gray level. The measurement of discernible (visible)
changes in gray level is a highly subjective process.

We have considerable preference regarding the number of Samples used to generate a digital
image. But this is not true for the number of gray levels. Due to hardware constraints, the
number of gray levels is usually an integer power of two. The most common value is 8 bits. It can
vary depending on application. When an actual measure of physical resolution relating pixels
and level of detail they resolve in the original scene are not necessary, it is not uncommon to
refer to an Llevel digital image of size as having a spatial resolution of pixels and a
gray level resolution of L levels.

1: Examining the effect of change in Gray level Resolution

The quality of an image depends on both spatial resolution and gray level resolution. The quality
of a gray-level image is significantly affected by its gray-level resolution. If you increase the
number of bits per pixel for image then it has a great effect in improving the quality of gray-level
images because higher number of gray levels would give a smooth transition along the details of
the image while decreasing the number of bits per pixel shows false boundaries in the image an
effect known as “False Contouring”

The figure below shows gray levels using 8 bits and 3 bits.

In the top section of the figure there are 256 levels of gray (8-bits) while in the bottom section
there are only 8 levels (3-bits) of gray.In this experiment, the gray level resolution of an image is
to be reduced from 256 to 2, each time by a factor of 2 (equivalently, reducing the number of bits
per pixel from 8 to 1).The change of the image quality and the false contouring effect are to be
observed.
In MATLAB, this can be performed by reducing the number of gray levels that are required to
display the image using a smaller number of rows in the colormap matrix. All of this will be
performed while keeping a constant spatial resolution for the image (256 X 256).

Example 1: Changing the number of gray Levels

%Conversion of color/grayscale image into binary [ 2 levels]


I = imread('cameraman.tif');
K= imfinfo('cameraman.tif');
if(K.BitDepth ==24) % An integer indicating the number of bits per pixel
I=rgb2gray(I);
end
[r,c] = size(I);
I2= uint8(zeros(r,c));
for i = 1:r
for j=1:c
if (I(i,j)>128)
I2(i,j) =255;
else
I2(i,j) =0;
end
end
end
figure,
subplot(121),imshow(I);
subplot(122),imshow(I2);
Results are:

2. Changing the spatial resolution (Image scaling)


Changing the spatial resolution of a digital image, by zooming or shrinking, is an operation of
great importance in a wide range of applications (i.e. in digital cameras, biomedical image
processing and astronomical images). In computer graphics, image scaling is the process of
resizing a digital image. Scaling is an important process that involves a trade-off between
efficiency, smoothness and sharpness. As the size of an image is increased, so the pixels which
comprise the image become increasingly visible, making the image appear "soft". Conversely,
reducing an image will tend to enhance its smoothness and apparent sharpness.Apart from
fitting a smaller display area, image size is most commonly decreased (or subsampled or
downsampled).

2.1Shrinking (Down scaling, resizing downward)


Image shrinking is done in the similar manner as zooming with one difference as now the
process of pixel replication is row column deletion. Now we can delete every second column and
row for shrinking
Example 2 : Reducing the Spatial Resolution
% Shrinking the image to 1/2
I = imread('cameraman.tif');
K= imfinfo('cameraman.tif');
if(K.BitDepth ==24)
I=rgb2gray(I);
end
[r,c] = size(I);
I2(1:r/2, 1:c/2) = I(1:2:r, 1:2:c);
figure,imshow(I);
figure,imshow(I);
Results are

Figure 1 Figure 2

2.2Zooming (up scaling, resizing upward)


Enlarging an image (upsampling or interpolating) is generally common for making smaller
imagery fit a bigger screen in fullscreen mode, for example. In “zooming” an image, it is not
possible to discover any more information in the image than already exists, and image quality
unavoidably suffers. However, there are several methods of increasing the number of pixels that
an image contains, which evens out the appearance of the original pixels. Zooming (up scaling,
resizing upward) requires two steps

 The Creation of new pixel locations


 Assignment of gray levels to new pixel locations

Zooming (up scaling, resizing upward) can be achieved by the following techniques ::

1. Nearest neighbor Interpolation


2. Pixel Replication (A variation of Nearest neighbor Interpolation)
3. Bilinear Interpolation
4. Bicubic Interpolation

1-Nearest neighbor Interpolation

For any zooming approach we have to create an imaginary grid of the size which is required over
the original image. In that case we will have a scenario where we have new locations void of
pixel values, now we have to insert new values on these empty locations.

In Nearest neighbor interpolation we look for the closest pixel in the original image and assign its
gray level to new pixel in the grid. When finished with all points in the grid, we can simply expand
it to the originally specified size to obtain the zoomed image.
Assume
• Image I1 has size M1xN1
• Image I2 has size M2xN2

Then
• x=Cv
• y = Cy w

Where C, CY is the scaling factors:


• C = N2/N1
• Cy = M2/M1

We actually want the inverse function


• v = x/ C
• w = y/ Cy

Algorithm:
 Read desired image and get its dimensions.

 Create the zoomed image.


o Width of the zoomed image = width * scaling factor

o Height of the zoomed image = height * scaling factor

 Traverse through the each element of the zoomed image matrix and copy the relevant value
from original image.

Example 3: implementation with code


I1 = imread('cameraman.tif');
M1 = size(I1,1); % Number of rows in I
N1 = size(I1,2); % Number of columns in I
% Pick size of output image
M2 = 300;
N2 = 100;
I2 = zeros(M2,N2); % Allocate output
cx = N2/N1; % Scale in x
cy = M2/M1; % Scale in y
for x=1:N2
for y=1:M2
% Calculate position in input image
v = x/cx;
w=y/cy;
% We'll just pick the nearest neighbor to (v,w)
v = round(v);
% rounds each element of v to the nearest integer
w = round(w);
I2(y,x) = I1(w,v);
end
end
subplot(1,2,1); imshow(I1);
subplot(1,2,2); imagesc(I2);

Implementation with MATLAB built in function


This method is the simplest technique that resamples the pixel values present in the input vector
or a matrix. In MATLAB, ‘imresize’ function is used to interpolate the images.

Example :

The pictorial representation depicts that a 3x3 matrix is interpolated to 6x6 matrix. The values in
the interpolated matrix are taken from the input matrix (i.e) no new value is added.
Example 4:
%3x3 Matrix
A = zeros(3,3);
A(:)=[10,2,9,4,18,14,22,7,25];
display(A); %Before Interpolation
C = imresize(A,[6,6],'nearest');
display(C); %After Interpolation

EXPLANATION:
The result as shown in the pictorial representation can be achieved using the MATLAB function
‘imresize’
Example 5:
% Read image into the workspace.
I = imread('rice.png');
% Resize the image, specifying scale factor and the interpolation method.
J = imresize(I, 0.5, 'nearest');
% Display the original and the resized image.
figure imshow(I) title('Original Image')
figure imshow(J) title('Resized Image Using
Nearest Neighbor')

2-Pixel Replication
Pixel replication is applicable when we want to increase the size of an image an integer number of
times. For example to double the size of an image we can duplicate each column, this doubles the size
of image in horizontal direction. Then we duplicate each row of the enlarged image to double the size
in the vertical direction.
Example 6: Enlarge the image 3 times with pixel replication
a =imread('cameraman.tif');
c =size(a);
for i = 1:1:c(1)
b=0;
for j = 1:1:c(2)
for k = 1:1:3
bbb(i,j+b)=a(i,j);
b=b+1;
end
b=b-1;
end
end
cc=size(bbb);
for i = 1:1:cc(2)
b=0;
for j = 1:1:cc(1)
for k = 1:1:3
bb(j+b,i)=bbb(j,i);
b=b+1;
end
b=b-1;
end
end
size(bb);
figure imshow(a)
figure imshow(bb)

LAB TASKS
TASK 1: Use nearest neighbour interpolation to get the resultant matrix.(implement with both
code and matlab builtin function “imresize”)

Input matrix
3 9
7 12

Resultant matrix
3 3 9 9
3 3 9 9
7 7 12 12
7 7 12 12

TASK 2
Take any built-in image as an input and Reduce its spatial resolution to ½ and 1/4 of the
original image .
I. Display the original image,
II. Displqay ½ of original image
III. Display 1/4 of original image .

Note : (Implement with code and MATLAB built in function)

You might also like