Chapter 2
Images Files and File Types
We shall see that matrices can be handled very efficiently in MATLAB, Octave and Python.
Images may be considered as matrices whose elements are the pixel values of the image.
In this chapter we shall investigate how the matrix capabilities of each system allow us to
investigate images and their properties.
2.1 Opening and Viewing Grayscale Images
Suppose you are sitting at your computer and have started your system. You will have
a prompt of some sort, and in it you can type:
MATLAB/Octave
>> w = imread(’wombats.png’);
or from the io module of skimage
Python
In : import skimage.io as io
In : w = io.imread(’wombats.png’)
This takes the gray values of all the pixels in the grayscale image wombats.png and puts
them all into a matrix
w. This matrix w is now a system variable, and we can perform
various matrix operations on it. In general, the
imread function reads the pixel values from
an image file, and returns a matrix of all the pixel values.
Two things to note about this command:
1. If you are using MATLAB or Octave, end with a semicolon; this has the effect of not
displaying the results of the command to the screen. As the result of this particular
command is a matrix of size 256 ×256, or with 65,536 elements, we do not really want
all its values displayed. Python, however, does not automatically display the results
of a computation.
2. The name
wombats.png is given in quotation marks. Without them, the system
would assume that
wombats.png was the name of a variable, rather than the name
of a file.
Now we can display this matrix as a grayscale image. In MATLAB:
MATLAB
>> figure,imshow(w),impixelinfo
19
20 A Computational Introduction to Digital Image Processing, Second Edition
This is really three commands on one line. MATLAB allows many commands to be entered
on the same line, using commas to separate the different commands. The three commands
we are using here are:
figure
, which creates a figure on the screen. A figure is a window in which a graphics
object can be placed. Objects may include images or various types of graphs.
imshow(g), which displays the matrix g as an image.
impixelinfo, which turns on the pixel values in our figure. This is a display of the gray
values of the pixels in the image. They appear at the bottom of the figure in the form
Pixel info: (c, r) p
where c is the column value of the given pixel; r is its row value, and p is its gray value.
Since
wombats.png is an 8-bit grayscale image, the pixel values appear as integers in
the range 0255.
This is shown in Figure 2.1.
FIGURE 2.1: The wombats image with impixelinfo
In Octave, the command is:
Octave
>> figure,imshow(w)
and in Python one possible command is:
Python
In: io.imshow(w)
Images Files and File Types 21
However, an interactive display is possible using the
ImageViewer method from the module
with the same name:
Python
In : from skimage.viewer import ImageViewer as IV
In : viewer = IV(w)
In : viewer.show()
This is shown in Figure 2.2.
FIGURE 2.2: The wombats image with ImageViewer
At present, Octave does not have a built-in method for interactively displaying the pixel
indices and value comparable to MATLAB’s
impixelinfo or Python’s ImageViewer.
If there are no figures open, then in Octave or MATLAB an
imshow command, or any
other command that generates a graphics object, will open a new figure for displaying the
object. However, it is good practice to use the
figure command whenever you wish to
create a new figure.
We could display this image directly, without saving its gray values to a matrix, with
the command
MATLAB/Octave
>> imshow(’wombats.png’)
or
Python
In: io.imshow(’wombats.png’)
22 A Computational Introduction to Digital Image Processing, Second Edition
However, it is better to use a matrix, seeing as these are handled very efficiently in each
system.
2.2 RGB Images
As we shall discuss in Ch apter 13, we need to define colors in some standard way, usually
as a subset of a three-dimensional coordinate system; such a subset is called a color model.
There are, in fact, a number of different methods for describing color, but for image display
and storage, a standard mo del is RGB, for which we may imagine all the colors sitting inside
a “color cube” of side 1 as shown in Figure 2.3. The colors along the black-white diagonal,
R
B
G
(0, 0, 0)
Black
(1, 0, 0)
Red
(0, 1, 0)
Green
(0, 0, 1)
Blue
(1, 0, 1)
Magenta
(1, 1, 0)
Yellow
(0, 1, 1)
Cyan
(1, 1, 1)
White
FIGURE 2.3: The color cube for the RGB color model
shown in the diagram as a dotted line, are the points of the space where all the R, G, B
values are equal. They are the different intensities of gray. We may also think of the axes
of the color cube as being discretized to integers in the range 0–255.
RGB is the standard for the display of colors on computer monitors and TV sets. But it
is not a very good way of describing colors. How, for example, would you define light-brown
using RGB? As we shall see also in Chapter 13, some colors are not realizable with the RGB
model in that they would require negative values of one or two of the RGB components. In
general, 24-bit RGB images are managed in much the same way as grayscale. We can save
the color values to a matrix and view the result:
MATLAB
>> b = imread(’backyard.png’);
>> figure,imshow(b),impixelinfo
Note now that the pixel values consist of a list of three values, giving the red, green, and
blue components of the color of the given pixel.
Images Files and File Types 23
An important difference between this type of image and a grayscale image can be seen
by the command
MATLAB/Octave
>> size(b)
which returns three values: the number of rows, columns, and “pages” of b, which is a
three-dimensional matrix, also called a multidimensional array. Each system can handle
arrays of any dimension, and
b is an example. We can think of b as being a stack of three
matrices, each of the same size.
In Python:
Python
In: b.shape
again returns a triplet of values.
To obtain any of the RGB values at a given location, we use indexing methods similar
to above. For example,
MATLAB/Octave
>> b(100,200,2)
ans =
126
returns the second color value (green) at the pixel in row 100 and column 200. If we want
all the color values at that point, we can use
MATLAB/Octave
>> b(100,200,1:3)
ans(:,:,1) =
114
ans(:,:,2) =
126
ans(:,:,3) =
58
However, MATLAB and Octave allow a convenient shortcut for listing all values along a
particular dimension just using a colon on its own:
MATLAB/Octave
>> b(100,200,:)
This can be done similarly in Python:
Python
In: print b[99,199,:]
[114 126 58]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.21.159