Lab #3 Digital Images: A/D and D/A: Shafaq Tauqir 198292
Lab #3 Digital Images: A/D and D/A: Shafaq Tauqir 198292
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.
(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.
RESULT:
Observe that the range of signal values is between 0 and 255. Which values represent white and which
ones black? Can you identify the region where the 200th row crosses the fence?
Black values are represented by 0 and white values are represented by 1.
The region where 200th row crosses the fence is a very high frequency region due rapid change of colors.
RESULT:
EXPLANATION:
A sinusoid that goes from 1 to 0 then -1 and so on. When its 0, the color is black, when 1, its white.
How can you predict that width from the formula for xpix?
ANSWER: The dimensions of picture are 256*256. And we are keeping frequency 1/16. So there are a
total of 32 bands. Hence 256/32=8.
3.2.1.1 RESULT:
(c) Explain how you would produce an image with bands that are horizontal. Give the formula that
would create a 400*400 image with 5 horizontal black bands separated by white bands. Write the
MATLAB code to make this image and display it.
CODE:
>> ypix=cos(2*pi*(0:399)/80)'*ones(1,400);
>> imshow(ypix)
RESULT:
(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?
RESULT:
Notice the aliasing in the down-sampled image, which is surprising since no new values are
being created by the down-sampling process. Describe how the aliasing appears visually. 9 Which
parts of the image show the aliasing effects most dramatically?
ANSWER:
Highest frequency regions are most affected by aliasing.
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?
ORIGINAL DOWN-SAMPLED
EXPLANATION:
Some parts of the downsampled image are distorted due to their frequency domain spectrums
intercepting. The high frequency parts of the original image (white colors) are most affected by aliasing
as they are the ones experiencing the interceptions.
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.
RESULT:
1 1 1 1 2 2 2 2 3 3 3 3 4
4 4 4 5 5 5 5 6 6 6 6 7
7 7 7
If xr1holdis treated as an interpolated version of xr1, then what is the interpolation factor?
ANSWER: 4.
Your lab report should include an explanation for this part, but plots are optional—use them if they
simplify the explanation.
ANSWER:
(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.
CODE:
clear
Call the result xholdrows. Display xholdrows as an image, and compare it to the down sampled image
xx3
RESULT:
xholdrows Image:
Downsampled Image:
(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:
PART B
clear
clc
load lighthouse;
p=3;
xx3=lighthouse(1:p:end,1:p:end);
n=1:109;
L = length(xx3(n,:));
nn = ceil((0.999:1:3*L)/3); %<--Round up to the integer part
xholdrows = xx3(n,nn);
imshow(xholdrows)
PART C
clear
clc
load lighthouse;
p=3;
xx3=lighthouse(1:p:end,1:p:end);
n=1:109;
L = length(xx3(n,:));
nn = ceil((0.999:1:3*L)/3); %<--Round up to the integer part
xholdrows = xx3(n,nn);
m=1:426;
L = 109-(1/3);
nn = ceil((0.999:1:3*L)/3); %<--Round up to the integer part
xhold = xholdrows(nn,m);
imshow(xhold)
(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.
For the example above, what is the interpolation factor when converting xr1to xr1linear?
ANSWER: The interpolation factor is 10.
(e) 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 function,
because one call will only process all the columns of a matrix. 10 Name the interpolated output image
xxlinear. Include your code for this part in the lab report.
CODE:
load lighthouse
p=3;
xx3=lighthouse(1:p:end,1:p:end);
L=size(xx3);
Lrows=L(1);
Lcolumns=L(2);
for row=1:Lrows
nold=1:Lcolumns;
wp1=xx3(row,:);
nnew=(1/3):(1/3):Lcolumns;
wp1=im2double(wp1);
linear=interp1(nold,wp1,nnew);
xxlinear_rows(row,:) = linear;
end
for column=1:Lcolumns*3
nold=1:Lrows;
wp2=xxlinear_rows(:,column);
nnew=(1/3):(1/3):Lrows-(1/3);
wp2=im2double(wp2);
linear=interp1(nold,wp2,nnew);
xxlinear(:,column)=linear;
end
(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?
RESULT:
xxlinear: Original:
EXPLANATION:
● There is a big distortion at the edges after the interpolation.
● Also the high frequency areas are heavily distorted too.
● The high frequency region is highly distorted due to 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?
ANSWER:
● The built in linear interpolation uses a much better algorithm than the zero hold method as the
edges are much better due to the values not stopping suddenly.
● The fence problem is the same in both methods too. The fence problem could not be solved as the
high frequency region is distorted due to aliasing and data there is lost.