DSP - P4 Digital Images - A-D & D-A
DSP - P4 Digital Images - A-D & D-A
DSP - P4 Digital Images - A-D & D-A
Pre-Lab and Warm-Up: You should read at least the Pre-Lab and Warm-up sections of this lab assignment
and go over all exercises in the Pre-Lab section before going to your assigned lab session.
Verification: The Warm-up section of each lab must be completed during your assigned Lab time and
the steps marked Instructor Verification must also be signed off during the lab time. One of the laboratory
instructors must verify the appropriate steps by signing on the Instructor Verification line. When you have
completed a step that requires verification, simply demonstrate the step to the TA or instructor. Turn in the
completed verification sheet to your TA when you leave the lab.
Lab Report: It is only necessary to turn in a report on Section 3 with graphs and explanations. You are
asked to label the axes of your plots and include a title for every plot. In order to keep track of plots, include
your plot inlined within your report. If you are unsure about what is expected, ask the TA who will grade
your report.
The objective in this lab is to introduce digital images as a second useful signal type. 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 resultsa later
lab will revisit this issue and do a better job.
1 Pre-Lab
1.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 representing
the horizontal (t2 ) and vertical (t1 ) coordinates of a point in space.1 For monochrome images, the signal
x(t1 , t2 ) would be a scalar function of the two spatial variables, but for color images the function x(, )
would have to be a vector-valued function of the two variables.2 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 T1 and T2 are the sample spacings in the horizontal and vertical directions. Typical values of M and
N are 256 or 512; e.g., a 512 512 image which has nearly the same resolution as a standard TV image. In
M ATLAB 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 <
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.
show img.m
1. All image values must be non-negative for the purposes of display. Filtering may introduce negative
values, especially if differencing is used (e.g., a high-pass filter).
2. The default format for most gray-scale displays is eight bits, so the pixel values x[m, n] in the image
must be converted to integers in the range 0 x[m, n] 255 = 28 1.
3. The actual display on the monitor is created with the show img function.4 The show img function
will handle the color map and the true size of the image. The appearance of the image can be altered
by running the pixel values through a color map. In our case, we want grayscale display where all
three primary colors (red, green and blue, or RGB) are used equally, creating what is called a gray
map. In M ATLAB the gray color map is set up via
colormap(gray(256))
which gives a 2563 matrix where all 3 columns are equal. The function colormap(gray(256))
creates a linear mapping, so that each input pixel amplitude is rendered with a screen intensity pro-
portional to its value (assuming the monitor is calibrated). For our lab experiments, non-linear color
mappings would introduce an extra level of complication, so they will not be used.
4. When the image values lie outside the range [0,255], or when the image is scaled so that it only
occupies a small portion of the range [0,255], the display may have poor quality. In this lab, we will
use show img.m to automatically rescale the image: This requires a linear mapping of the pixel
values:5
xs [m, n] = x[m, n] +
The scaling constants and can be derived from the min and max values of the image, so that all
pixel values are recomputed via:
x[m, n] xmin
xs [m, n] = 255.999
xmax xmin
where bxc is the floor function, i.e., the greatest integer less than or equal to x.
Notice that unless the input parameter figno is specified, a new figure window will be opened.
(a) Load and display the 326 426 lighthouse image from lighthouse.mat. This image can be
find in the M ATLAB files link. The command load lighthouse will put the sampled image into
the array ww. Use whos to check the size of ww after loading. When you display the image it might
be necessary to set the colormap via colormap(gray(256)).
(b) Use the colon operator to extract the 200th 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
ones black? Can you identify the region where the 200th row crosses the fence?
2 Warm-up
The instructor verification sheet may be found at the end of this lab.
(b) In the previous part, which data value in xpix is represented by white? which one by black?
(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 M ATLAB
code to make this image and display it.
Instructor Verification (separate page)
1. In M ATLAB, use show img and trusize to put your images into separate figure windows at the
correct pixel resolution.
2. Use the Windows program called PAINT to assemble the different images onto one page. This pro-
gram can be found under Accessories.
3. For each M ATLAB figure window, press ALT and the PRINT-SCREEN key at the same time, which
will copy the active window contents to the clipboard.
4. After each window capture in step 3, paste the clipboard contents into PAINT.
6
5. Arrange the images so that you can make a comparison for your lab report.
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);
(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, youll find that it is not square. Now down-sample the lighthouse
image by a factor of 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. Describe how the aliasing appears visually.9 Which parts of the image show the aliasing
effects most dramatically?
Instructor Verification (separate page)
(a) Describe how the aliasing appears visually. Compare the original to the downsampled image. Which
parts of the image show the aliasing effects most dramatically?
(b) This part is challenging: explain why the aliasing happens in the lighthouse image by using a
frequency domain explanation. In other words, estimate the frequency of the features that are being
aliased. Give this frequency as a number in cycles per pixel. (Note that the fence provides a sort of
spatial chirp where the spatial frequency increases from left to right.) Can you relate your frequency
estimate to the Sampling Theorem?
You might try zooming in on a very small region of both the original and downsampled images.
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 M ATLAB 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.
x[m,n] y[m,n]
Repeat Along Repeat Down
the Rows the Columns
REPEAT
SAMPLES
EXPANDED
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 im-
age should be created and stored in the variable xx3. The objective will be to reconstruct an approximation
to the original lighthouse image, which is 256 256, 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 xr1hold is treated as an interpolated version
of xr1, then what is the interpolation factor? Your lab report should include an explanation for this
part, but plots are optionaluse 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 downsampled
image xx3; compare the size of the images as well as their content.
(c) Now process all the columns of xholdrows to fill in the missing points in each column and 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.
Its default mode is linear interpolation, which is equivalent to using the *linear option, but
interp1 can 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 indices
xr1linear = interp1(n1,xr1,tti); %-- function is INTERP-ONE
stem(tti,xr1linear)
For the example above, what is the interpolation factor when converting xr1 to xr1linear?
(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.
(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 recon-
struction (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 M ATLABs 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.
10
Use a matrix transpose in between the interpolation calls. The transpose will turn rows into columns.
3.3.2 Warnings
Images obtained from JPEG files might come in many different formats. Two precautions are necessary:
1. If M ATLAB loads the image and stores it as 8-bit integers, then M ATLAB 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 M ATLAB
will store it as a 3-D array. For example, the result of whos for a 545 668 color image would give:
In this case, you should use M ATLABs 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( ).
11
Optional means that you dont have to include this in a lab report. This section provided in case you are curious and want to
learn more on your own.
Part 2.1(c) Create a 400 400 image with 5 horizontal black bands separated by white bands. Write the
M ATLAB code to make this image and display it.
Verified: Date/Time:
Part 2.3(a) Downsample the lighthouse image to see aliasing. Describe the aliasing, and where it occurs
in the image.
Verified: Date/Time: