24 A Computational Introduction to Digital Image Processing, Second Edition
(Recall that indexing in Python starts at zero, so to obtain the same results as with MAT-
LAB and Octave, the indices need to be one less.)
A usef ul function for obtaining RGB values is
impixel; the command
MATLAB/Octave
>> impixel(b,200,100)
ans
=
114 126 58
returns the red, green, and blue values of the pixel at column 200, row 100. Notice that
the order of indexing is the same as that which is provided by the
impixelinfo command.
This is opposite to the row, column order for matrix indexing. This command also applies
to grayscale images:
MATLAB/Octave
>> impixel(w,100,200)
ans =
189 189 189
Again, three values are returned, but since w is a single two-dimensional matrix, all three
values will be the same.
Note that in Octave, the command
impixel will in fact produce a 3×3 matrix of values;
the three columns however will be the same. If we just want the values once, then:
Octave
impixel(b,200,100)(:,1)’
ans =
114 126 58
2.3 Indexed Color Images
The command
MATLAB/Octave
>> figure,imshow(’emu.png’),impixelinfo
produces a nice color image of an emu. However, the pixel values, rather than being three
integers as they were for the RGB image above, are three fractions between 0 and 1, for
example:
Pixel info: (61, 109) 37 [0.58 0.54 0.51]
What is going on here?
If we try saving to a matrix first and then displaying the result:
Images Files and File Types 25
MATLAB/Octave
>> em = imread(’emu.png’);
>> figure,imshow(em),impixelinfo
we obtain a dark, barely distinguishable image, with single integer gray values, indicating
that
em
is being interpreted as a single grayscale image.
In fact the image
emu.png is an example of an indexed image, consisting of two matrices:
a color map, and an index to the color map. Assigning the image to a single matrix picks
up only the index; we need to obtain the color map as well:
MATLAB/Octave
>> [em,emap] = imread(’emu.png’);
>> figure,imshow(em,emap),impixelinfo
MATLAB and Octave store the RGB values of an indexed image as values of type
double
,
with values between 0 and 1. To obtain RGB values at any value:
MATLAB/Octave
>> v = em(109,61)
v =
37
To find the color values at this point, note that the color indexing is done with eight bits,
hence the first index value is zero. Thus, index value 37 is in fact the 38th row of the color
map:
MATLAB/Octave
>> >> emap(38,:)
ans =
0.5801 0.5410 0.5098
Python automatically converts an indexed image to a true-color image as the image file is
read:
Python
In: em = io.imread(’emu.png’)
In: em.shape
Out: (384, 331, 3)
To obtain values at a pixel : :
Python
In : print em[108,60,:]
[148, 138, 130]
In : np.set
_
printoptions(precision = 4)
In : print em[108,60,:].astype(float)/255.0
[ 0.5804 0.5412 0.5098]
This last shows how to obtain information comparable to that obtained with MATLAB and
Octave.
26 A Computational Introduction to Digital Image Processing, Second Edition
Information About Your Image
Using MATLAB or Octave, a great deal of information can be obtained with the
imfinfo
function. For example, suppose we take our indexed image
emu.png
from above. The
MATLAB and Octave outputs are in fact very slightly different; here is the Octave output:
Octave
>> imfinfo(’emu.png’)
ans =
scalar structure containing the fields:
Filename = /home/amca/Images/emu.png
FileModDate = 2-Jan-2015 15:12:52
FileSize = 71116
Format = PNG
FormatVersion =
Width = 331
Height = 384
BitDepth = 8
ColorType = indexed
DelayTime = 0
DisposalMethod =
LoopCount = 0
ByteOrder = undefined
Gamma = 0
Chromaticities = [](1x0)
Comment =
Quality = 75
Compression = undefined
Colormap =
(For saving of space, the colormap, which is given as part of the output, is not listed here.)
Much of this information is not useful to us; but we can see the size of the image in
pixels, the size of the file (in bytes), the number of bits per pixel (this is given by
BitDepth),
and the color type (in this case “indexed”).
For comparison, let’s look at the output of a true color file (showing only the first few
lines of the output), this time using MATLAB:
Images Files and File Types 27
MATLAB
>> imfinfo(’backyard.png’)
ans
=
Filename: ’backyard.png’
FileModDate: ’02-Jan-2015
05:45:17’
FileSize: 264103
Format: ’png’
FormatVersion: []
Width: 482
Height: 643
BitDepth: 24
ColorType: ’truecolor’
FormatSignature: [73 73 42 0]
ByteOrder: ’little-endian’
NewSubFileType: 0
BitsPerSample: [8 8 8]
Compression: ’JPEG’
Now we shall test this function on a binary image, in Octave:
Octave
>> imfinfo(’circles.png’)
ans =
scalar structure containing the fields:
Filename = /home/amca/Images/circles.png
FileModDate = 2-Jan-2015 21:37:54
FileSize = 1268
Format = PNG
FormatVersion =
Width = 256
Height = 256
BitDepth = 1
ColorType = grayscale
In MATLAB and Octave, the value of ColorType would be GrayScale.
What is going on here? We have a binary image, and yet the color type is given as either
“indexed” or “grayscale.” The fact is that the
imfinfo command in both MATLAB nor
Octave has no value such as “Binary”, “Logical” or “Boolean” for
ColorType. A binary image
is just considered to be a special case of a grayscale image which has only two intensities.
However, we can see that
circles.png is a binary image since the number of bits per pixel
is only one.
In Chapter 3 we shall see that a binary image matrix, as distinct from a binary image
file, can have the numerical data typ e
Logical.
To obtain image information in Python, we need to invoke one of the libraries that
supports the metadata from an image. For TIFF and JPEG images, such data is known as
EXIF data, and the Python
exifread library may be used:
28 A Computational Introduction to Digital Image Processing, Second Edition
Python
In : import exifread
In : f = open(’backyard.tif’)
In : tags = exifread.process
_
file(f,details=False)
In : for x in tags:
...: print x.ljust(32),":",tags[x]
Image Orientation : Horizontal (normal)
Image Compression : JPEG
Image FillOrder : 1
Image ImageLength : 643
Image PhotometricInterpretation : 2
Image ImageWidth : 482
Image DocumentName : /home/amca/Images/backyard.tif
Image BitsPerSample : [8, 8, 8]
Image XResolution : 72
Image YResolution : 72
Image SamplesPerPixel : 3
Some of the output is not shown here. For images of other types, it is easiest to invoke a
system call to something like “ExifTool”
1
:
Python
In : ! exiftool cassowary.png
ExifTool Version Number : 9.46
File Name : cassowary.png
File Size : 21 MB
MIME Type : image/png
Image Width : 4000
Image Height : 2248
Bit Depth : 8
Color Type : RGB
Compression : Deflate/Inflate
Background Color : 255 255 255
Pixels Per Unit X : 7086
Pixels Per Unit Y : 7086
Pixel Units : Meters
Exif Byte Order : Little-endian (Intel, II)
Orientation : Horizontal (normal)
X Resolution : 180
Y Resolution : 180
Resolution Unit : inches
Again most of the information is not shown.
2.4 Numeric Types and Conversions
Elements in an array representing an image may have a number of different numeric
data types; the most common are listed in Table 2.1. MATLAB and Octave use “double”
1
Available from http://www.sno.phy.queensu.ca/~phil/exiftool/
..................Content has been hidden....................

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