IUIUIUIUIUIUI (1)
IUIUIUIUIUIUI (1)
MUHAMMAD
IBRAHIM
AFNAN MIRZA
NORAIZ
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
(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)
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?
% 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:
Lab Task 3:
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.
(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)
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:
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?
(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;
% Perform interpolation
interpolated_image = interp2(1:col, 1:row, double(lighthouse), X, Y, 'nearest');
subplot(1, 2, 2);
imshow(uint8(interpolated_image));
title('Interpolated 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?
(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?
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:
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( ).
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.