Sie sind auf Seite 1von 9

Lab 06: Digital Images: A/D and D/A

Yuqian Hu
EE224 Section C
Introduction:
This lab shows how the A-to-D sampling and the D-to-A reconstruction processes are carried out
for digital images. Plus, I learned the method of image zooming that gives poor results.
Pre-lab
2.1 Synthesize a Test Image
In this part, we are required to generate a synthetic image from a mathematical formula.
(a) First I need to generate a 256256 test image in which all of the columns are identical by
using:
xpix=ones(256,1)*cos(2*pi*(0:255)/16);

Here is the code:


close all;
clear all;
clc;
fignum=1;
xpix=ones(256,1)*cos(2*pi*(0:255)/16);
show_img(xpix);
trusize(fignum);
title('256*256 Test Image');
xlabel('The Pixels on the x-axis');
ylabel('The Pixels on the y-axis');

Below is the graph I got when I ran the code:

(b) When x-pixel equals to 0, the brand is white; when x-pixel equals to 8-9 the brand is black.
So when xpix value is cos(0)=1, the graph displays white, when xpix equals cos(2*pi*(8/16)) = 1 the graph shows black. The sum of black band's length and white bands length is 16, so the
number of black bands is 16.
(c) For this part we need to create a 400400 image with 5 horizontal black bands separated by
white bands. See the code below:
close all;
clear all;
clc;
fignum=1;
xpiy=cos(2*pi*(0:399)/80).'*ones(1,400);
show_img(xpiy);
trusize(fignum);
title('400*400 Test Image');
xlabel('The Pixels on the x-axis');
ylabel('The Pixels on the y-axis');

Below is the graph I got when I ran the code:

3. Sampling, Aliasing and Reconstruction


3.1 Down-Sampling
(a) An image is stored in an M x N array, and after down-sampling the original image with
original scale M x N, a new image was stored in M/2 x N/2 array, so the new image is 1/4 of the
original images size. See code below:

close all;
clear all;
clc;
load('IPimages.mat')
[endx,endy]=size(shadow);
wp=shadow(1:2:endx,1:2:endy);
show_img(wp);
title('Downsample the shadow');
xlabel('The Pixels on the x-axis');
ylabel('The Pixels on the y-axis');

Comparing the original image and new downsaple image. After zooming the new graph to be the
same as the original images size, I obtained the graphs as above. The shadow part of new image
is less clear than the original shadow part, and the most ambiguous part is a place where shadows
are intensive.
(c) By using frequency domain, I found that the more concentrated the shadows are, the less
clear the shadows are. The spatial frequency is made to be smaller and may be smaller than 2*fo
(maximum frequency), and so the aliased phenomenon happens.

3.2 Reconstruction of Images


In this part, we will use our down-sampled graph, and try to reconstruct it to the original size.

(a) Use the code provided by lab instruction:


n1 = 0:6; % Define a function with integer values from 0 to 6
xr1 = 8*cos(pi*n1/3); % Define the function to be interpolated
p = 10; % set the interpolation factor
tti = 0:1/p:6; %-- locations between the n1 indices
xr1zoh = interp1(n1,xr1,tti,'nearest'); %-- function is INTERP-ONE
subplot(211);stem(n1,xr1);
subplot(212);stem(tti,xr1zoh);

Below is the graph we obtained:

xrlzoh' has more component than xr1. In graph xrlzoh, we got


X[1(1: (p/2))/p]=X[n]
Here n is integer, and for example, the value around x[1],
x[0.5],x[0.6],x[0.7],x[0.8],
x[0.9],x[1.0],x[1.1],x[1.2],
x[1.3],x[1.4],x[1.5] are all equals to x[1], and which is similar to other points. In that way our
xrlzoh will be 10 times size as the xr1 if we make them in same xpix unit.
(b) and (c)
Part b, hold x and build a x * newy image. For part c, hold y and built a newx * newy image. For
these two problems, well first down-sampled images by a factor of 4. So in part b and c, our
interpolar factor is both 4.
loadIPimages;

[endx,endy]= size(chainlink);
cl4 = chainlink(1:4:endx,1:4:endy); % set cl4 as the image that down-sampled
at factor at 4
show_img(cl4);
% display cl4 to compare with xhold and yhold's graph
%(b)
[x,y]=size(cl4);
%set x & y equals to the length of cl4's xpix and ypix
n1=1:x; %define our xhold from 1 to x.
p = 4;%interpolar is 4
tti = 1:1/p:x;%location between the n1 indices
xholdrows = interp1(n1,cl4,tti,'nearest'); % use interp1 function to get our
xhold graph.
show_img(xholdrows); %display xhold
%(c)
[x1,y1] = size(xholdrows); % begin to handle yhold, set x1 & y1 equals to the
length of xhold'sxpix and ypix
n2=1:y1;%define our yhold from 1 to y1
p=4;%interpolar is 4
ttj=1:1/p:y1;%location between the n2 indices
yholdcols = interp1(n2,xholdrows',ttj,'nearest');% use interp1 function to
get our yhold graph.
show_img(yholdcols');%display yhold

Graphs above from left to right are cl4, xhold, yhold.


Firstly, comparing cl4 and xhold by zooming in the right corner of both image twice.

The horizontal part(x-pix) stays the same but y-pix changed, and the xpix size is 170 which is the
same as the one of c14.

Comparing the final image yhold to cl4, we can see these two graphs have the same clarity.
Then I compared the yhold with original chainlink.

Our original Chainlink image has the same size as yhold, but much more clear than it.
(d) n1 = 0:6; % Define a function with integer values from 0 to 6
xr1 = 8*cos(pi*n1/3); % Define the function to be interpolated
p = 8; % set the interpolation factor
tti = 0:1/p:6; %-- locations between the n1 indices
xr1linear = interp1(n1,xr1,tti,'linear'); %-- function is INTERP-ONE
subplot(211);stem(n1,xr1); %plot original and new graph in same page
subplot(212);stem(tti,xr1linear);

From this graph we can find that different to nearest one, in linear reconstruction, our
new graph(xr1linear) take the near value for x[n], and treat them like linear equation, and
full the empty part by that linear equation.
(e) and (f)
Similarly as what we did in part b and c, we need to reconstruct our cl4 to original size,
by using interp1(with linear).
loadIPimages;
[endx,endy]= size(chainlink);
cl4 = chainlink(1:4:endx,1:4:endy);
[x,y]=size(cl4);
show_img(cl4);
xlabel('xpixel');
ylabel('ylabel');
title('cl4');
n1=1:x;
p = 4;
tti = 1:1/p:x;
xholdrows = interp1(n1,cl4,tti,'linear');
show_img(xholdrows);
xlabel('xpixel');
ylabel('ylabel');
title('xholdrows');
[x1,y1]=size(xholdrows);
n2=1:y1;
ttj=1:1/p:y1;
yholdcols = interp1(n2,xholdrows',ttj,'linear');
xxlinear=yholdcols'
show_img(xxlinear);
xlabel('xpixel');
ylabel('ylabel');
title('yholdcols');

This code is almost the same with what we did in part 2 and 3, the only difference is here we use
linear for interp1, and we need to take the transpose of yholdcols to get xxlinear.

From left to right, cl4, xholdrows, yholdcols,


Then I compared xxlinear with original chainlink graph

Its still less clear than the original graph, Then also compare the xxlinear, with
yholdcols(nearest)

(g) Its easy to see that linear way will result in a better graph view than nearest way.
The reason I thought should be, since the image was built by a bunch of sine-wave, if we simply
choose some max, min value, and fill their near position with theirs value, the output image will
change a lot, but if we choose the linear way, the output still different with original graph, but
will be better than nearest way, we replace sine-wave by many triangles connect the peak to zero,
and zero to min.
So the linear way is much better than the nearest way when we reconstruct images.

Das könnte Ihnen auch gefallen