Sunteți pe pagina 1din 6

Lab #1 - Digital Image Processing

Cristhian Camilo Delgado Fajardo


Faculty of Engineering
Pontificia Universidad Javeriana
Cali, Colombia
ccdelgado@javerianacali.edu.co

AbstractThe present report lists the procedure, programs, Here, filename is a string containing the complete name of the
demonstrations, performances, responses, justifications and the image file (including any applicable extension). For example,
results obtained. Also include conclusions, observations, and the command line
literature Consulted according to the course requirements.
Index TermsMatlab, digital image processing >> i= imread(rice.tif);

I. I NTRODUCTION reads the Portable Network Graphics (PNG) image rice into
image array i.
Digital image processing methods are divided into two
main areas of application: improvement of pictorial infor- Images are displayed on the MATLAB desktop using func-
mation for human interpretation; And image data processing tion imshow, which has the basic syntax:
for storage, transmission and representation for autonomous
machine perception. To meet the objectives of these areas imshow(f, G)
of knowledge it is important to start knowing basic aspects where f is an image array, and G is the number of intensity
about how to represent and perform mathematical operations levels used to display it. If G is omitted, it defaults to 256
in digital images. This document shows the development of levels. Using the syntax:
some MATLAB scripts designed for the visualization and
manipulation of digital images covering the following topics of imshow(f, [low high])
interest: Levels of intensity, zoom and shrinkage, average for
displays as black all values less than or equal to low, and as
reduction of noise, negative, reflexion and rotation of images.
white all values greater than or equal to high. The values in
II. L AB P ROCEDURES between are displayed as intermediate intensity values using
the default number of levels. For these reason, the command
A. Reading and viewing images proposed in the assigment
Figure 1 is generated by running the MATLAB script imshow(f, [0 100])
code32.m in the attachments.
overexpose the original rice.png showed in figure 2.

Fig. 1: Image generated by code32.m script


Fig. 2: Original rice.png image
Images are read into the MATLAB environment using
function imread, whose syntax is: Finally, the syntax
imread(filename) imshow(f, [])
sets variable low to the minimum value of array f and high
to its maximum value. This form of imshow is useful for
displaying images that have a low dynamic range or that have
positive and negative values.

In order to get an idea of the image file details, we can use


function imfinfo, which has the syntax:
imfinfo filename
where filename is the complete file name of the image stored (a) 256 gray levels (b) 128 gray levels
in disk. For example,
>>imfinfo(rice.png)
Outputs image information of rice.png like file name
(rice.png), file size (44607), width (256), Height (256), Bit
depth (8), Color type (grayscale) and other data. Similarly,
the whos function displays additional information about an
array. For instance, the statement:
>> whos i (c) 64 gray levels (d) 32 gray levels
gives size (256x256), bytes (65536) and class image informa-
tion (uint8). All of these valuable metadata can be captured
into a structure variables that can be used for subsequent
computations.

B. Gray intensity levels


(Re-)quantizing an image can be accomplished using the
equation (). The matlab implementation of this process can be
seen in the attachments.
(e) 16 gray levels (f) 8 gray levels
& '
X(x, y)
Y (x, y) = f (1)
f

Where X(x, y) and Y (x, y) represent the original


and quantized images respectively.The parameter f
is quantization parameter defined by the relation
newmaxval/currentmaxval for instance, if is desired
re quantize one image from 256 to 128 gray values the f
value must be 2.
(g) 4 gray levels (h) 2 gray levels
Figure 3 shows the effects of quantization (gray) levels on Fig. 3: Image Re-quantization
image quality for an image with 256 256 pixels, starting with
256 gray levels and reducing it by a factor of 2 several times,
until arriving at a binary version of the original image. A close
inspection of the results will show that the quality loss between both useful image processing operations and often rely on
the original image and its 32 gray levels equivalent is not very the same underlying algorithms. The main difference lies in
noticeable (Figure ad), but the quality of the last four images the fact that zooming and shrinking are usually performed
can be unacceptable for most purposes due to the presence of interactively (with a tool such as imshow or imtool) and their
false contouring and loss of relevant detail. results last for a brief moment, whereas resizing is typically
accomplished in a noninteractive way (e.g., as part of a
C. Image resizing MATLAB script) and its results are stored for longer term
One of the most common geometric operations is the resize use.
operation. It is necessary to distinguish between true image
resizing where the resulting image size (in pixels) is changed the MATLAB Image Processing Toolbox has a function
and resizing an image for human viewing which we will refer for resizing images, imresize. The imresize function allows
to as zooming (in) and shrinking (or zooming out). They are the user to specify the interpolation method used (nearest-
median) value of the original n n block in the input image to
determine the value of each output pixel in the shrunk image
also produce low-quality results and are bound to fail in
some cases. These limitations call for improved interpolation
methods, which will be briefly described next:

1) Zero-Order (Nearest-Neighbor) Interpolation: This


baseline interpolation scheme rounds off the calculated
coordinates (x,y) to their nearest integers. Zero-order (or
nearest-neighbor) interpolation is simple and computationally
fast, but produces low-quality results, with artifacts such as
blockiness effects which are more pronounced at large-scale
factors and jagged straight linesparticularly after rotations by
angles that are not multiples of 90 degrees.

2) First-Order (Bilinear) Interpolation: First-order (or


bilinear) interpolation calculates the gray value of the
interpolated pixel (at coordinates (x, y)) as a weighted
function of the gray values of the four pixels surrounding
the reference pixel in the input image. Bilinear interpolation
produces visually better results than the nearest-neighbor
(a) shrinking 32x32 (b) zooming 1024x11024
interpolation at the expense of additional CPU time.

Fig. 4: Image Resizing Note that in the case of using bilinear interpolation, the
imresize function automatically applies a low-pass filter to
the image (whose default size is 11 11), slightly blurring
neighbor, bilinear, or higher order interpolations). When using it before the image is interpolated. This helps to reduce the
the imresize function, a scale factor larger than 1 will produce effects of aliasing during resampling.
an image larger than the original, and a scale factor smaller
than 1 will result in an image smaller than the original. III. I MAGE AVERAGING
Figure 4a contain the original image and that Which is after
Noise reduction is achieved by adding the corresponding
the operation of sub-sample the image to a size of 32 x 32
pixel values of all the noisy images and then dividing them
pixels. In contrast, figure 4b shows re-scaling process of the
with the number of noisy images to get the averaged intensity.
32 x 32 to the original size (1024x1024 px).
If the noise at each pixel is uncorrelated then it gets canceled
out during averaging. Better results can be obtained as the
To write code to enlarge or reduce an image by a certain
number of noisy images are increased As shown in the
factor (e.g., a factor of 32 in both directions), we need to
following mathematical demonstration:
deal with the problem of removing pixels (in the case of
shrinking) by subsampling the original image by a factor of
Consider a noisy image g(x, y) formed by the addition of
32 in both dimensions, that is, skipping every other pixel
noise n(x, y) to an original image f (x, y),
along each row and column. Conversely, for the task of
enlarging the image by a factor of 32 in both dimensions, we
g(x, y) = f (x, y) + n(x, y) (2)
would opt for copying each original pixel to an n n block in
the output image. These simple interpolation schemes (pixel where the assumption is that at every pair of coordinates
removal and pixel duplication, respectively) are fast and easy (x, y) the noise is uncorrelated and has zero average value.The
to understand, but suffer from several limitations, such as the objective of the following procedure is to reduce the noise
following: content by adding a set of noisy images, { gi (x, y) } as
follows:
The blockiness effect that may become noticeable when
M M M
enlarging an image. 1 X 1 X 1 X
The possibility of removing essential information in the g= gi = fi + ni (3)
M i=1 M i=1 M i=1
process of shrinking an image.
The difficulty in extending these approaches to arbitrary, . Then
noninteger, resizing factors.
M M
1 X 1 X
E{g} = E{fi } + E{ni } (4)
Other simplistic methods such as using the mean (or M i=1 M i=1
But all the fi are the same image, so E{fi } = f . Also, it subplot to place each of the nine images (the original along
is given that the noise has zero mean, so E{ni } = 0 then it with the eight calculated above) into nine different subplots
follows that within the same figure as showed in figure 5. For more details,
please refer to code36.m script in the attachments.
E{g} = f (5)
On the other hand, It is known from random-variable theory
that the variance of the sum of uncorrelated random variables
is the sum of the variances of those variables (Papoulis
[1991]). Because it is given that the elements of f are constant
and the n are uncorrelated, then
1
g2 = f2 + [ 2 + n2 2 + ... + n2 M ] (6)
M 2 n1
The first term on the right side is 0 because the elements
of f are constants. The various n2 i are simply samples of the
noise, which is has variance n2 .Thus, n2 i = n2 and we have:
M 2 1 2
g2 = = (7)
M2 n M n
where E{g} is the expected value of g, and g2 and n2 are
the variances of g and n respectively, all at image coordinates
(x, y). The standard deviation (square root of the variance) at
any point in the average image is:
1
g = n (8)
M
As M increases, equation 7 and 8 indicate that the variabil-
Fig. 5: cameraman bit plane slicing
ity (as measured by the variance or the standard deviation)
of the pixel values at each location (x, y) decreases. Because
Note that the higher-order bits (especially the top four)
E{g} = f , this means that g approaches f as the number of
contain the majority of the visually significant data. The other
noisy images used in the averaging process increases.
bit planes contribute to more subtle details in the image.
IV. B IT P LANE S LICING Separating a digital image into its bit planes is useful for
For the next discussion we use cameramanimage, the analyzing the relative importance played by each bit of the
grayscale image is 8-bit (unsigned integer), then we can image, a process that aids in determining the adequacy of the
determine the first bit (least-significant bit and so is right most number of bits used to quantize each pixel.
bit in the 8-bit pixel) by using bitand operation as follows:
V. I MAGE F LIPPING
singleBitImg = bitand(cameraManImg, mask); Flipping an image upside down or leftright can be easily
accomplished re-organizing the rows and columns of the
In the above, we are applying a mask of 1 (or, in binary, matrices that represent the image channels. For example,
00000001) which will zero all but the first bit from each pixel HF = I(:, end : 1 : 1, :) performs a horizontal flip while
in our image. We can then plot the pixel1Img and move to the V F = I(end : 1 : 1, :, :) do it vertically. Figures 6 and 7
second bit. Since the binary 00000001 zeroed all bits but the represents the output of the Matlab script code37.m in the
first, then we want to zero all bits but the second, so we need attachments for two different images (grayscale,RGB).
a binary mask of 00000010. We can easily get this using the
VI. I MAGE ROTATION
bitshift function which shifts the bits to the left (or right). In
this case, we want to shift the bits of our mask one bit to the In this section, we discuss about how to rotate an image
left. without using built in MATLAB functions. You can find the
MATLAB implementation in script code38.m in the attach-
mask = 1; ments. The script was designed based on the next pseudocode:
mask = bitshift(mask,1);
1) Find the midpoints of the image.
singleBitImg = bitand(cameraManImg, mask);
2) Convert each pixel coordinate to polar coordinate.
3) Convert the angle which is in radians into degree.
We can do the same for the remaining six bits, shifting the 4) Add the degree value to be rotated to the value obtained
mask one bit to the left on each iteration of a loop. We use in the above step.
45 degrees. Figure 8 shows a problem called aliasing. Its
the same reason that we see jagged staircases on lines drawn
on low resolution screens at angles other than horizontal and
vertical. Raster screens are digital, and pixel boundaries are
at quantized locations. Also, multiplying by sines and cosines
on the integer coordinates of the source image gives real
number results, and these have to be rounded back to integers
again to be plotted. Sometimes this number rounding means
the same destination location is addressed more than once,
and sometimes certain pixels are missed completely. When
the pixels are missed, the background shows through. This is
why there are holes.There are a variety of solutions for this
problem. One of them is interpolate the pixel value using the
nearest neighbor like its showing in figure 9.

Fig. 6: gray scale image flipping

Fig. 8: Image rotated 45 degrees - Image Gaps

Fig. 7: RGB image flipping

5) Now again convert the degree to radian by using


rad2deg.
6) convert to Cartesian co-ordinate by using the function
pol2cart (theta,radius).
7) Re-scale the image window for displaying. Fig. 9: Gaps filled by Nearest Neighbor technique
8) We got holes in between when the image its rotated at
angles different to 90,180,270 and 360 degrees. This
gaps are filled with the nearest neighbor. VII. C ONCLUSIONS
Image processing is a method to perform some operations
Figure 8 and 9 present the result for a image rotation of on an image, in order to get an enhanced image or to extract
some useful information from it.
The choice of sampling and quantization parameters (total
number of pixels and the number of gray levels per pixel)
has impact on the visual representation but also in the image
file size.
The images are two-dimensional arrays so they can be treated
as matrices to perform any logic or arithmetic operations
to obtain basic results as shown in the development of this
practice.

R EFERENCES
[1] afael Gonzalez and Richard Woods. Digital image processing. Prentice
Hall, 2001.
[2] afael Gonzalez and Richard Woods. Digital image processing using
Matlab. Prentice Hall, 2004.
[3] ARQUES, Oge. Practical image and video processing using MATLAB.
John Wiley Sons, 2011.

S-ar putea să vă placă și