Transcript Chapter 2

Chapter 2: Images and MATLAB
2.1 Grayscale Images
• MATLAB is a data analysis software package with
powerful support for matrices and matrix
operations
 Command window
>> help imdemos
 Reads pixel values from an image
>> w=imread(‘caeraman.tif’);
>> figure, imshow(w); impixelinfo
1
2.1 Grayscale Images
• figure
Creates a figure on the screen
A figure is a window in which a graphics object
can be placed
• imshow(w)
 displays the matrix w as an image
• impixelinfo
2
 turns on the pixel values in our figure
 They appear at the bottom of the figure in the form
Pixel Info: (X, Y) intensity
2.1 Grayscale Images
cameraman.tif
3
2.2 RGB Images
>> a=imread(‘autumn.tif’);
>> figure, imshow(a), impixelinfo
4
FIGURE 2.2
5
2.2 RGB Images
• Multidimensional array
>> size(a)
 206 (rows) 345 (columns) 3 (pages)
>> a(100,200,2)
 25
>> a(100,200,1:3) or >> a(100,200,:)
or function impixel
>> impexel(a,200,100)
 75 25 30
6
2.3 Indexed Color Images
>> figure, imshow(‘forest.tif’),
impixelinfo
>> c=imread(‘forest.tif’);
>> figure, imshow(c),
impixelinfo
7
2.3 Indexed Color Images
>> [c, cmap]=imread(‘forest.tif’);
>> figure, imshow(c, cmap), impixelinfo
• Information about Your Image
• A great deal of information can be obtained with
the imfinfo function
>> imfinfo(‘forest.tif’)
8
2.4 Data Types and Conversions
9
2.4 Data Types and Conversions
10
2.5 Images Files and Formats
• You can use MATLAB for image processing very
happily without ever really knowing the
difference between GIF, TIFF, PNG, etc.
• However, some knowledge of the different
graphics formats can be extremely useful in
order to make a reasoned decision
• Header information
11
 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
2.5 Images Files and Formats
• The imread and imwrite functions of MATLAB
currently support the following formats
 JPEG
These images are created using the Joint Photographics
Experts Group compression method (ch14)
TIFF
A very general format that supports different compression
methods, multiple images per file, and binary, grayscale,
truecolor, and indexed images
12
2.5 Images Files and Formats
GIF
A venerable format designed for data transfer. It is still
popular and well supported, but is somewhat restricted in
the image types it can handle
 BMP
Microsoft Bitmap format has become very popular and is
used by Microsoft operating systems
PNG, HDF, PCX, XWD, ICO, CUR
13
FIGURE 2.3
• A HEXADECIMAL DUMP FUNCTION
14
2.5.1 Vector versus Raster Images
• We may store image information in two different
ways
 Vector images: a collection of lines or vectors
 Raster images: a collection of dots
• The great bulk of image file formats store images
as raster information
• As well as containing all pixel information, an
image file must contain some header information
15
 this must include the size of the image, but may also include
some documentation, a color map, and the compression used
 e.g. PGM format was designed to be a generic format used for
conversion between other formats
2.5.3 Microsoft BMP
16
2.5.3 Microsoft BMP
17
2.5.3 Microsoft BMP
 The image width is given by bytes 18–21; they are in the second row
42 00 00 00
 To find the actual width, we reorder these bytes back-to-front:
00 00 00 42
 Now we can convert to decimal
(4×161)+(2×160) = 66 which is the image width in pixels
 The image height
1F 00 00 00 (1×161)+(F×160) = 31
18
2.5.4 GIF and PNG
• GIF
 Colors are stored using a color map. The GIF specification
allows a maximum of 256 colors per image
 GIF doesn’t allow binary or grayscale images, except as can be
produced with RGB values
 The pixel data is compressed using LZW (Lempel-Ziv-Welch)
compression
 The GIF format allows multiple images per file. This aspect can
be used to create animated GIFs
19
2.5.4 GIF and PNG
• PNG
 The PNG format has been more recently designed to replace
GIF and to overcome some of GIF’s disadvantages
 Does not rely on any patented algorithms, and it supports
more image types than GIF
 Supports grayscale, true color, and indexed images
 Moreover, its compression utility, zlib, always results in
genuine compression
20
2.5.5 JPEG
• The JPEG algorithm uses lossy compression, in
which not all the original data can be recovered
>> dumphex('football.jpg',4)
ans =
FF
00
03
07
21
D8
01
03
06
FF
00
03
08
E0 00 10 4A 46 49 46 00 01 01 00 00 01 ......JFIF......
00 FF DB 00 43 00 03 02 02 03 02 02 03 .......C........
04 03 03 04 05 08 05 05 04 04 05 0A 07 ................
0C 0A 0C 0C 0B 0A 0B 0B 0D 0E 12 10 0D ................
2.5.5 JPEG
22
2.5.6 TIFF
• One of the most comprehensive image formats
• Can store multiple images per file
• Allows different compression routines and different byte
orderings
• Allows binary, grayscale, truecolor or indexed images,
and opacity or transparency
• An excellent format for data exchange
23
2.5.6 TIFF
24
2.5.6 TIFF
• This particular image uses the little-endian
byte ordering
• The first image in this file (which is in fact
the only image), begins at byte
E0 01 01 00
• Because this is a little-endian file, we reverse
the order of the bytes: 00 01 01 E0. This
works out to 66016.
25
2.5.7 DICOM
• DICOM (Digital Imaging and Communications in
Medicine)
• Like GIF, may hold multiple image files
• May be considered as slices or frames of a three
dimensional object
• The DICOM specification is huge and complex. Drafts
have been published on the World Wide Web
26
2.5.8 Files in MATLAB
• Which writes the image stored in matrix X with color
map map (if appropriate) to file filename with
format fmt
• Without the map argument, the image data is supposed
to be grayscale or RGB
e.g. >>imwrite(c,cmap,’forest.png’,’png’);
>>imwrite(c,’forest1.png’,’png’);
27
Exercise
• The following shows the hexadecimal dump of
a BMP file:
42 4D 7E 05 02 00 00 00 00 00 36 04 00 00 28 00
00 00 23 01 00 00 C2 01 00 00 01 00 08 00 00 00
00 00 48 01 02 00 00 00 00 00 00 00 00 00 00 01
00 00 00 00 00 00 00 00 00 00 01 01 01 00 02 02
Determine the height and width of this image (in
pixels) and state whether it is a grayscale or
color image.
28