Images Files and File Types 29
Data type Description Range
logical Boolean 0 or 1
int8 8-bit integer 128 127
uint8 8-bit unsigned integer 0 255
int16
16-bit integer 32768 32767
uint16 16-bit unsigned integer 0 65535
double or float Double precision real number Machine specific
TABLE 2.1: Some numeric data types
and Python uses “float.” There are others, but those listed will b e sufficient for all our work
with images. Notice that, strictly speaking,
logical is not a numeric type. However, we
shall see that some image use this particular type. These data types are also functions, and
we can convert from one type to another. For example:
MATLAB/Octave
>> a = 23;
>> b = uint8(a);
>> b
b =
23
>> whos a b
Name Size Bytes Class
a 1x1 8 double
b 1x1 1 uint8
Similarly in Python:
Python
In : a = 23
In : b = uint8(a)
In : %whos int uint8
Variable Type Data/Info
-----------------------------
a int 23
b uint8 23
Note here that %whos is in fact a magic function in the IPython shell. If you are using
another interface, this function will not be available.
Even though the variables
a and b have the same numeric value, they are of different
data types.
A grayscale image may consist of pixels whose values are of data type
uint8. These
images are thus reasonably efficient in terms of storage space, because each pixel requires
only one byte.
We can convert images from one image type to another. Table 2.2 lists all of MATLAB’s
functions for converting between different image types. Note that there is no
gray2rgb
30 A Computational Introduction to Digital Image Processing, Second Edition
Function Use Format
ind2gray Indexed to grayscale y=ind2gray(x,map);
gray2ind Grayscale to indexed [y,map]=gray2ind(x);
rgb2gray RGB to grayscale y=rgb2gray(x);
rgb2ind
RGB to indexed
[y,map]=rgb2ind;
ind2rgb Indexed to RGB y=ind2rgb(x,map);
TABLE 2.2: Converting images in MATLAB and Octave
function. But a gray image can be turned into an RGB image where all the R, G, and B
matrices are equal, simply by stacking the grayscale array three deep. This can done using
several different methods, and will be investigated in Chapter 13.
In Python, there are the
float and uint8 functions, but in the skimage library there
are methods from the
util module; these are listed in Table 2.3.
Function Use Format
img
_
as
_
bool Convert to boolean y=sk.util.img
_
as
_
bool(x)
img
_
as
_
float Convert to 64-bit floating point y=sk.util.img
_
as
_
bool(x)
img
_
as
_
bool Convert to 16-bit integer y=sk.util.img
_
as
_
int(x)
img
_
as
_
bool Convert to 8-bit unsigned integer y=sk.util.img
_
as
_
ubyte(x)
img
_
as
_
bool
Convert to 16-bit unsigned integer
y=sk.util.img
_
as
_
uint(x)}
TABLE 2.3: Converting images in Python
2.5 Image Files and Formats
We have seen in Section 1.8 that images may be classified into four distinct types: binary,
grayscale, colored, and indexed. In this section, we consider some of the different image file
formats, their advantages and disadvantages. You can use MATLAB, Octave or Python
for image processing very happily without ever really knowing the difference between GIF,
TIFF, PNG, and all the other formats. However, some knowledge of the different graphics
formats can be extremely useful to be able to make a reasoned decision as to which file type
to use and when.
There are a great many different formats for storing image data. Some have been
designed to fulfill a particular need (for example, to transmit image data over a network);
others have been designed around a particular operations system or environment.
As well as the gray values or color values of the pixels, an image file will contain some
header information. This will, at the very least, include the size of the image in pixels
(height and width); it may also include the color map, compression used, and a description
of the image. Each system recognizes many standard formats, and can read image data
from them and write image data to them. The examples above have mostly been images
with extension
.png, indicating that they are PNG (Portable Network Graphics) images.
This is a particularly general format, as it allows f or binary, grayscale, RGB, and indexed
color images, as well as allowing different amounts of transparency. PNG is thus a good
format for transmitting images between different operating systems and environments. The
Images Files and File Types 31
PNG standard only allows one image per file. Other formats, for example TIFF, allow for
more than one image per file; a particular image from such a multi-image file can be read
into MATLAB by using an optional numeric argument to the
imread
function.
Each system can read images f rom a large variety of file types, and save arrays to images
of those types. General image file types, all of which are handled by each system, include:
JPEG Images created using the Joint Photographics Experts Group
compression me thod. We will discuss this more in Chapter 14.
TIFF Tagged Image File Format: a very general format which sup-
ports different compression methods, multiple images per file,
and binary, grayscale, truecolor and indexed images.
GIF Graphics Interchange Format: This venerable format was de-
signed for data transfer. It is still popular and well supported,
but is somewhat restricted in the image types it can handle.
BMP Microsoft Bitmap: This format has become very popular, with
its use by Microsoft operating systems.
PNG Portable Network Graphics: This is designed to overcome some
of the disadvantages of GIF, and to become a replacement for
GIF.
We will discuss some of these briefly below.
A Hexadecimal Dump Function
To explore binary files, we need a simple function that will enable us to list the contents
of the file as hexadecimal values. If we try to list the contents of a binary file directly to
the screen, we will see masses of garbage. The trouble is that any file printing method will
interpret the file’s contents as ASCII characters, and this means that most of these will
either be unprintable or nonsensical as values.
A simple hexadecimal dump function, called “hexdump,” is given at the end of the
chapter. It is called with the name of the file, and the number of bytes to be listed:
MATLAB/Octave
>> hexdump(’backyard.tif’,64)
000010 4949 2a00 4c01 0400 ffd8 ffdb 0043 0008 II
*
.L........C..
000020 0606 0706 0508 0707 0709 0908 0a0c 140d ................
000030 0c0b 0b0c 1912 130f 141d 1a1f 1e1d 1a1c ................
000040 1c20 242e 2720 222c 231c 1c28 3729 2c30 . $.’ ",#..(7),0
and in Python:
Python
In : hexdump(’backyard.tif’,64)
000000: 4949 2a00 4c01 0400 ffd8 ffdb 0043 0008 |II
*
.L........C..|
000010: 0606 0706 0508 0707 0709 0908 0a0c 140d |................|
000020: 0c0b 0b0c 1912 130f 141d 1a1f 1e1d 1a1c |................|
000030: 1c20 242e 2720 222c 231c 1c28 3729 2c30 |. $.’
",#..(7),0|
The result is a listing of the bytes as hexadecimal characters in three columns: the first
gives the hexadecimal value of the index of the first byte in that row, the second column
consists of the bytes themselves, and the third column lists those that are representable as
ASCII text.
32 A Computational Introduction to Digital Image Processing, Second Edition
Vector versus Raster Images
We may store image information in two different ways: as a collection of lines or vec-
tors, or as a collection of dots. We refer to the former as vector images; the latter as
raster images. The great advantage of vector images is that they can be magnified to any
desired size without losing any sharpness. The disadvantage is that are not very good for
the representation of natural scenes, in which lines may be scarce. The standard vector
format is Adobe PostScript; this is an international standard for page layout. PostScript
is the format of choice for images consisting mostly of lines and mathematic ally describ ed
curves: architectural and industrial plans, font information, and mathematical figures. The
reference manual [21] provides all necessary information about PostScript.
The great bulk of image file formats store images as raster information; that is, as a
list of the gray or color intensities of each pixel. Images captured by digital means–digital
cameras or scanners–will be stored in raster format.
A Simple Raster Format
As well as containing all pixel information, an image file must contain some header infor-
mation; this must include the size of the image, but may also include some documentation,
a color map, and compression used. To show the workings of a raster image file, we shall
briefly describe the ASCII PGM format. This was designed to be a generic format used
for conversion between other formats. Thus, to create conversion routines between, say, 40
different formats, rather than have 40 ×39 = 1560 different conversion routines, all we need
is the 40 × 2 = 80 conversion routines between the formats and PGM.
P2
# CREATOR: The GIMP’s PNM Filter Version 1.0
256 256
255
41 53 53 53 53 49 49 53 53 56 56 49 41 46 53 53 53
53 41 46 56 56 56 53 53 46 53 41 41 53 56 49 39 46
FIGURE 2.4: The start of a PGM file
Figure 2.4 shows the beginning of a PGM file. The file begins with
P2; this indicates
that the file is an ASCII PGM file. The next line gives some information about the file:
any line beginning with a hash symbol is treated as a comment line. The next line gives the
number of columns and rows, and the following line gives the number of grayscales. Finally
we have all the pixel information, starting at the top left of the image, and working across
and down. Spaces and carriage returns are delimiters, so the pixel information could be
written in one very long line or one very long column.
Note that this format has the advantage of being very easy to write to and to read from;
it has the disadvantage of producing very large files. Some space can be saved by using
“raw” PGM; the only difference is that the header number is
P5, and the pixel values are
stored one per byte. There are corresponding formats for binary and colored images (PBM
and PPM, respectively); colored images are stored as three matrices; one for each of red,
green, and blue; either as ASCII or raw. The format does not support color maps.
Images Files and File Types 33
Binary, grayscale, or color images using these formats are collectively called PNM images.
MATLAB and Octave can read PNM images natively; Python can’t, but it is not hard to
write a file to read a PNM image to an ndarray.
Microsoft BMP
The Microsoft Windows BMP image format is a fairly simple example of a binary image
format, noting that here binary means non-ascii, as opposed to Boolean. Like the PGM
format ab ove, it consists of a header followed by the image information. The header is
divided into two parts: the first 14 bytes (bytes number 0 to 13), is the “File Header”, and
the following 40 bytes (bytes 14 to 53), is the “Information Header”. The header is arranged
as follows:
Bytes Information Description
0–1 Signature “BM” in ASCII = 42 4D in hexadecimal.
2–5 FileSize The size of the file in bytes.
6–9 Reserved All zeros.
10–13 DataOffset File offset to the raster data.
14–17 Size Size of the information header = 40 bytes.
18–21 Width Width of the image in pixels.
22–25 Height Height of the image in pixels.
26–27 Planes Number of image planes (= 1).
28–29 BitCount Number of bits per pixel:
1: Binary images; two colors
4: 2
4
= 16 colors (indexed)
8: 2
8
= 256 colors (indexed)
16: 16-bit RGB; 2
16
= 65,536 colors
24: 24-bit RGB; 2
24
= 17,222,216 colors
30–33 Compression Type of compression used:
0: no compression (most common)
1: 8-bit RLE encoding (rarely used)
2: 4-bit RLE encoding (rarely used)
34–37 ImageSize Size of the image. If compression is 0, then
this value may be 0.
38–41 HorizontalRes The horizontal resolution in pixels per meter.
42–45 VerticalRes The vertical resolution in pixels per meter.
46–49 ColorsUsed The number of colors used in the image. If
this value is zero, then the number of colors
is the maximum obtainable with the bits per
pixel, that is 2
BitCount
.
50–53 ImportantColors The number of important colors in the image.
If all the colors are important, then this value
is set to zero.
After the header comes the Color Table, which is only used if BitCount is less than or
equal to 8. The total number of bytes used here is 4 × ColorsUsed. This format uses the
Intel “least endian” convention for bytes ordering, where in each word of four bytes the least
valued byte comes first. To see an example of this, consider a simple example:
..................Content has been hidden....................

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