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

IUIUIUIUIUIUI (1)

The document outlines a lab focused on digital images in a Digital Signal Processing course, detailing objectives, instructions, and tasks related to image sampling, aliasing, and reconstruction using MATLAB. Students are required to perform various lab tasks, including displaying images, down-sampling, and applying interpolation techniques, while documenting their findings in a lab report. Key concepts such as the representation of images as matrices, the effects of down-sampling, and the limitations of reconstruction methods are emphasized throughout the lab activities.

Uploaded by

afnanmirza106
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

IUIUIUIUIUIUI (1)

The document outlines a lab focused on digital images in a Digital Signal Processing course, detailing objectives, instructions, and tasks related to image sampling, aliasing, and reconstruction using MATLAB. Students are required to perform various lab tasks, including displaying images, down-sampling, and applying interpolation techniques, while documenting their findings in a lab report. Key concepts such as the representation of images as matrices, the effects of down-sampling, and the limitations of reconstruction methods are emphasized throughout the lab activities.

Uploaded by

afnanmirza106
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Syed Zain ul Hassan

PLO4-CLO4 PLO5- PLO8- PLO9-


CLO5 CLO6 CLO7

Name Reg. No Viva / Analysis Modern Ethics Individual


Quiz / Lab of data Tool and and Team
Performa in Lab Usage Safety Work
nce Report

5 Marks 5 Marks 5 Marks 5 Marks 5 Marks

MUHAMMAD
IBRAHIM
AFNAN MIRZA

NORAIZ

EE330 Digital Signal Processing Page 1


Objectives
The objective in this lab is to introduce digital images as a second useful type of signal. We will
show how the A-to-D sampling and the D-to-A reconstruction processes are carried out for digital
images. In particular, we will show a commonly used method of image zooming (reconstruction)
that gives “poor” results.

 Familiarization with digital images.


 Working with images in Matlab.
 Sampling of images
 Familiarization with reconstruction of images

Lab Instructions
 The students should perform and demonstrate each lab task separately for step-wise evaluation
(please ensure that course instructor/lab engineer has signed each step after ascertaining its
functional verification)
 Each group shall submit one lab report on LMS within 6 days after lab is conducted. Lab report
submitted via email will not be graded.
. Students are however encouraged to practice on their own in spare time for enhancing their
Lab Report Instructions
All questions should be answered precisely to get maximum credit. Lab report must ensure following
items:
 Lab objectives
 MATLAB codes
 Results (graphs/tables) duly commented and discussed
 Conclusion

EE330 Digital Signal Processing Page 2


3 Digital Images: A/D and D/A

3.1 Digital Images


In this lab we introduce digital images as a signal type for studying the effect of sampling, aliasing and
reconstruction. An image can be represented as a function x(t1,t2) of two continuous variables
1
representing the horizontal (t2) and vertical (t1) coordinates of a point in space. For monochrome images,
the signal x(t1,t2)would be a scalar function of the two spatial variables, but for color images the function
2
x(: , :) would have to be a vector-valued function of the two variables. Moving images (such as TV)
would add a time variable to the two spatial variables. Monochrome images are displayed using black and
white and shades of gray, so they are called gray-scale images. In this lab we will consider only sampled
gray-scale still images. A sampled gray-scale still image would be represented as a two-dimensional array
of numbers of the form
x[m,n]=x(mT1,nT2) 1≤ m≤M, and 1 ≤n≤ N
Where T1and T2 are the sample spacing in the horizontal and vertical directions. Typical values of M and
N are 256 or 512 for e.g. a 512x512 image which has nearly the same resolution as a standard TV image.
In MATLAB we can represent an image as a matrix, so it would consist of M rows and N columns. The
matrix entry at (m,n)is the sample value x[m,n]—called a pixel (short for picture element).An important
property of light images such as photographs and TV pictures is that their values are always non-negative
and finite in magnitude; i.e.
0 ≤ x[m,n] ≤ Xmax<∞
This is because light images are formed by measuring the intensity of reflected or emitted light which
must always be a positive finite quantity. When stored in a computer or displayed on a monitor, the values
of x[m,n]have to be scaled relative to a maximum value Xmax. Usually an eight-bit integer representation
8
is used. With 8-bit integers, the maximum value (in the computer) would be Xmax=2 -1=255, and there
8
would be 2 = 256 different gray levels for the display, from 0 to 255.

3.1.1 MATLAB Function to Display Images


You can use imshow() to display image type “help imshow” in command window of Matlab to
see how to use imshow

EE330 Digital Signal Processing Page 3


Lab Task 1:

3.1.2 Get Test Images


In order to probe your understanding of image display, do the following simple displays:

(a) Load and display the 326* 426 “lighthouse” image from lighthouse.mat. This image can be find
in the MATLAB files link. The command “ load lighthouse” will put the sampled image into the
array ww/xx. Use whos to check the size of ww after loading.
Code:
clear all;
load lighthouse;
imshow(lighthouse);

Image:

th
(b) Use the colon operator to extract the 200 row of the “lighthouse” image, and make a plot of that
row as a 1-D discrete-time signal.
ww200 = ww(200,:);

Observe that the range of signal values is between 0 and 255. Which values represent white and which
th
ones black? Can you identify the region where the 200 row crosses the fence?
Code:
clear all;
load lighthouse;
imshow(lighthouse);

lighthouse_200 = lighthouse(200,:);
plot(lighthouse_200)

EE330 Digital Signal Processing Page 4


3.2 LAB TASKS: Sampling, Aliasing and

Reconstruction Lab Task 2:

3.2.1 Sampling of Images


Images that are stored in digital form on a computer have to be sampled images because they are stored in
an MxN array (i.e., a matrix). The sampling rate in the two spatial dimensions was chosen at the time the
image was digitized (in units of samples per inch if the original was a photograph). For example, the
image might have been “sampled” by a scanner where the resolution was chosen to be 300 dpi (dots per
7
inch). If we want a different sampling rate, we can simulate a lower sampling rate by simply throwing
away samples in a periodic way. For example, if every other sample is removed, the sampling rate will be
halved (in our example, the 300 dpi image would become a 150 dpi image). Usually this is called sub-
8
sampling or down-sampling.

Down-sampling throws away samples, so it will shrink the size of the image. This is what is done by the
following scheme wp = ww(1:p:end,1:p:end); when we are down sampling by a factor of p.

(a) One potential problem with down-sampling is that aliasing might occur. This can be illustrated in a
dramatic fashion with the lighthouse image.
Load the lighthouse.mat file which has the image stored in a variable called ww. When you check
the size of the image, you’ll find that it is not square. Now down sample the lighthouse image by
factor 2.What is the size of the down-sampled image? Notice the aliasing in the down-sampled
image, which is surprising since no new values are being created by the down-sampling process.
9
Describe how the aliasing appears visually. Which parts of the image show the aliasing effects
most dramatically?

EE330 Digital Signal Processing Page 5


Code:
clear all;
load lighthouse;
imshow(lighthouse);

% lighthouse_200 = lighthouse(200,:);
lighthouse_down = lighthouse(1:2:end,1:2:end);
plot(lighthouse_down)

Graph:

Code:
clear all;
load lighthouse;
imshow(lighthouse);

% lighthouse_200 = lighthouse(200,:);
lighthouse_down = lighthouse(1:100:end,1:100:end);
plot(lighthouse_down)
Graph:

EE330 Digital Signal Processing Page 6


3.2.1.1 Down-Sampling
For the lighthouse picture, down sampled by two in the warm-up section:
(a) Describe how the aliasing appears visually. Compare the original to the down sampled image. Which
parts of the image show the aliasing effects most dramatically?

Lab Task 3:

3.2.2 Reconstruction of Images


When an image has been sampled, we can fill in the missing samples by doing interpolation. For images,
this would be analogous to the sine-wave interpolation which is part of the reconstruction process in a D-
to-A converter. We could use a “square pulse” or a “triangular pulse” or other pulse shapes for the
reconstruction.

Figure 1: 2-D Interpolation broken down into row and column operations: the gray dots indicate repeated
data values created by a zero-order hold; or, in the case of linear interpolation, they are the interpolated
values.

For these reconstruction experiments, use the lighthouse image, down-sampled by a factor of 3 (similar to
what you did in Section 2.3). You will have to generate this by loading in the image from lighthouse.mat
to get the image which is in the array called xx. A down-sampled lighthouse image should be created and
stored in the variable xx3. The objective will be to reconstruct an approximation to the original lighthouse
image, which is 256x256, from the smaller down-sampled image.

(a) The simplest interpolation would be reconstruction with a square pulse which produces a “zero-order
hold.” Here is a method that works for a one-dimensional signal (i.e., one row or one column of the
image), assuming that we start with a row vector xr1, and the result is the row vector xr1hold.
xr1 = (-2).ˆ(0:6);
L = length(xr1);
nn = ceil((0.999:1:4*L)/4); %<--Round up to the integer part
xr1hold = xr1(nn);

Plot the vector xr1hold to verify that it is a zero-order hold version derived from xr1. Explain what values
are contained in the indexing vector nn. If xr1holdis treated as an interpolated version of xr1, then what is
EE330 Digital Signal Processing Page 7
the interpolation factor? Your lab report should include an explanation for this part, but plots are
optional—use them if they simplify the explanation.

EE330 Digital Signal Processing Page 8


Code:
clear all;
close all;
xr1 = (-2).^(0:6);
L = length(xr1);
nn = ceil((0.999:1:4*L)/4); %<--Round up to the integer part
xr1hold = xr1(nn);
plot (xr1hold)
Graph:

(b) Now return to the down-sampled lighthouse image, and process all the rows of xx3 to fill in the
missing points. Use the zero-order hold idea from part (a), but do it for an interpolation factor of
3. Call the result xholdrows. Display xholdrows as an image, and compare it to the down sampled
image xx3; compare the size of the images as well as their content.
Code:

clear all;
load lighthouse;
imshow(lighthouse);

% lighthouse_200 = lighthouse(200,:);
lighthouse_down = lighthouse(1:3:end,1:3:end);
%plot(lighthouse_down)
[row,col] = size (lighthouse_down)

for i = 1:row
x = (i - 1) * 3 + 1;
resampled_lighthouse(x:x+2, :) = lighthouse_down(i, j);
end
imshow (resampled_lighthouse)

EE330 Digital Signal Processing Page 9


Image:

Code:

clear all;
load lighthouse;
imshow(lighthouse);

% lighthouse_200 = lighthouse(200,:);
lighthouse_down = lighthouse(1:3:end,1:3:end);
%plot(lighthouse_down)
[row,col] = size (lighthouse_down)

for i = 1:col
y= (i - 1) * 3 + 1;
resampled_lighthouse(:,y:y+2) = lighthouse_down(i, j);
end
imshow (resampled_lighthouse)

Image:

EE330 Digital Signal Processing Page 10


(c) Now process all the columns of xholdrows to fill in the missing points in each column and call the
result xhold. Compare the result (xhold) to the original image lighthouse. Include your code for
parts (b) and (c) in the lab report.
Code:
clear all;
load lighthouse;
imshow(lighthouse);

% Downsample the image


lighthouse_down = lighthouse(1:3:end, 1:3:end);
[row, col] = size(lighthouse_down);

for i = 1:row
for j = 1:col
x = (i - 1) * 3 + 1;
y = (j - 1) * 3 + 1;
resampled_lighthouse(x:x+2, y:y+2) = lighthouse_down(i, j);
end
end

imshow(resampled_lighthouse);

Image:

(d) Linear interpolation can be done in MATLAB using the interp1function (that’s “interp-one”).When
unsure about a command, use help. Its default mode is linear interpolation, which is equivalent to using
the ’*linear’ option, but interp1can also do other types of polynomial interpolation. Here is an example on
a 1-D signal:
n1 = 0:6;
xr1 = (-2).ˆn1;
tti = 0:0.1:6; %--locations between the n1 indicesxr1
linear = interp1(n1,xr1,tti); %--function is INTERP-ONE
stem(tti,xr1linear)
For the example above, what is the interpolation factor when converting xr1to xr1linear?

EE330 Digital Signal Processing Page 11


Graph:

(d) In the case of the lighthouse image, you need to carry out a linear interpolation operation on both
the rows and columns of the down-sampled image xx3. This requires two calls to the interp1
10
function, because one call will only process all the columns of a matrix. Name the interpolated
output image xxlinear. Include your code for this part in the lab report.
Code:
clear all;
load lighthouse;

% Downsample the image


lighthouse_down = lighthouse(1:3:end, 1:3:end);

% Determine the size of the downsampled image


[row_down, col_down] = size(lighthouse_down);

% Generate row and column indices for the original image


[row, col] = size(lighthouse);
[X, Y] = meshgrid(1:col_down, 1:row_down);
X = (X - 1) * 3 + 1;
Y = (Y - 1) * 3 + 1;

% Perform interpolation
interpolated_image = interp2(1:col, 1:row, double(lighthouse), X, Y, 'nearest');

% Display the original and interpolated images


subplot(1, 2, 1);
imshow(lighthouse);
title('Original Image');

subplot(1, 2, 2);
imshow(uint8(interpolated_image));
title('Interpolated Image');

EE330 Digital Signal Processing Page 12


Image:

(f) Compare xxlinear to the original image lighthouse. Comment on the visual appearance of the
“reconstructed” image versus the original; point out differences and similarities. Can the reconstruction
(i.e., zooming) process remove the aliasing effects from the down-sampled lighthouse image?

The "reconstructed" image using xxlinear (linear interpolation) will appear


smoother but blurrier compared to the original lighthouse image, with less
sharpness and detail, especially around edges. While linear interpolation can
reduce some aliasing effects, it cannot completely remove them, as the loss of
high-frequency details during down-sampling can't be fully recovered. The result
is a softer, less detailed version of the original, with potential lingering artifacts
from aliasing.

(g) Compare the quality of the linear interpolation result to the zero-order hold result. Point out regions
where they differ and try to justify this difference by estimating the local frequency content. In other
words, look for regions of “low-frequency” content and “high-frequency” content and see how the
interpolation quality is dependent on this factor. A couple of questions to think about: Are edges low
frequency or high frequency features? Are the fence posts low frequency or high frequency features? Is
the background a low frequency or high frequency feature?

Linear interpolation performs better in smooth, low-frequency regions (like the


background), providing a smoother result. Zero-order hold preserves sharpness
in high-frequency regions (like edges and fence posts) but introduces pixelation.
Linear interpolation blurs sharp features, while zero-order hold keeps them
sharp but with blockiness. The choice of method depends on the content: linear
interpolation is smoother in low-frequency areas, while zero-order hold better
preserves high-frequency details, but with visible artifacts.

Comment: You might use MATLAB’s zooming feature to show details in small patches of the output
image. However, be careful because zooming does its own interpolation, probably a zero-order hold.

Warnings
Images obtained from JPEG files might come in many different formats. Two precautions are necessary:

EE330 Digital Signal Processing Page 13


1. If MATLAB loads the image and stores it as 8-bit integers, then MATLAB will use an internal data
type called uint8. The function show img( )cannot handle this format, but there is a conversion function
called double( )that will convert the 8-bit integers to double-precision floating-point for use with filtering
and processing programs.
yy = double(xx);

You can convert back to 8-bit values with the function uint8().

2. If the image is a color photograph, then it is actually composed of three “image planes” and MATLAB
will store it as a 3-D array. For example, the result of whosfor a 545x668 color image would give:

In this case, you should use MATLAB’s image display functions such as imshow( )to see the color image.
Or you can convert the color image to gray-scale with the function rgb2gray( ).

EE330 Digital Signal Processing Page 14


Notes:

1. The variables t1 and t2 do not denote time, they represent spatial dimensions. Thus, their units would
be inches or some other unit of length.
2. For example, an RGB color system needs three values at each spatial location: one for red, one for
green and one for blue.
3. If you have the MATLAB Image Processing Toolbox, then the function imshow.m can be used
instead.
4. If the MATLAB function imagesc.m is used to display the image, two features will be missing:
(1) the color map may beincorrect because it will not default to gray, and (2) the size of the image
will not be a true pixel-for-pixel rendition of the image on the computer screen.
5. The MATLAB function show_img has an option to perform this scaling while making the image
display.
6. An alternative is to use the free program called IRFANVIEW, which can do image editing and also
has screen capture capability.It can be obtained from
http://www.irfanview.com/english.htm.
7. For this example, the sampling periods would be T1 = T2 = 1/300 inches.
8. The Sampling Theorem applies to digital images, so there is a Nyquist Rate that depends on the
maximum spatial frequency in the image.
9. One difficulty with showing aliasing is that we must display the pixels of the image exactly. This
almost never happens because most monitors and printers will perform some sort of interpolation to
adjust the size of the image to match the resolution of the device. In MATLAB we can override these
size changes by using the function truesize which is part of the Image Processing Toolbox. In the
SP First Toolbox, an equivalent function called trusize.m is provided.
10. Use a matrix transpose in between the interpolation calls. The transpose will turn rows into columns.
11. Optional means that you don’t have to include this in a lab report. This section provided in case you
are curious and want to learn more on your own.

Linear interpolation is better for smooth, low-frequency areas, offering a smoother,


more continuous result. However, it tends to blur sharp details in high-frequency
regions, like edges and fine textures. Zero-order hold (nearest-neighbor
interpolation) preserves sharpness in high-frequency areas, but introduces blockiness
and pixelation, especially in smooth regions. The best method depends on the content:
use linear interpolation for smoother, less detailed areas, and zero-order hold when
sharpness in high-frequency features is more important, despite the pixelation.

EE330 Digital Signal Processing Page 15

You might also like