Chapter 9 Loading Images
How do we read images inot matlab and manipulate them?
Let's do a help on images:
this gives:
>> help images
Image Processing Toolbox.
Version 2.2 (R11) 05-Aug-1998
Release information.
Readme - Display information about versions 2.0, 2.1, and 2.2.
Image display.
colorbar - Display colorbar (MATLAB Toolbox).
getimage - Get image data from axes.
image - Create and display image object (MATLAB Toolbox).
imagesc - Scale data and display as image (MATLAB Toolbox).
immovie - Make movie from multiframe indexed image.
imshow - Display image.
montage - Display multiple image frames as rectangular montage.
subimage - Display multiple images in single figure.
truesize - Adjust display size of image.
warp - Display image as texture-mapped surface.
zoom - Zoom in and out of image or 2-D plot (MATLAB Toolbox).
Image file I/O.
imfinfo - Return information about image file (MATLAB Toolbox).
imread - Read image file (MATLAB Toolbox).
imwrite - Write image file (MATLAB Toolbox).
Geometric operations.
imcrop - Crop image.
imresize - Resize image.
imrotate - Rotate image.
interp2 - 2-D data interpolation (MATLAB Toolbox).
Pixel values and statistics.
corr2 - Compute 2-D correlation coefficient.
imcontour - Create contour plot of image data.
imfeature - Compute feature measurements for image regions.
imhist - Display histogram of image data.
impixel - Determine pixel color values.
improfile - Compute pixel-value cross-sections along line segments.
mean2 - Compute mean of matrix elements.
pixval - Display information about image pixels.
std2 - Compute standard deviation of matrix elements.
Image analysis.
edge - Find edges in intensity image.
qtdecomp - Perform quadtree decomposition.
qtgetblk - Get block values in quadtree decomposition.
qtsetblk - Set block values in quadtree decomposition.
Image enhancement.
histeq - Enhance contrast using histogram equalization.
imadjust - Adjust image intensity values or colormap.
imnoise - Add noise to an image.
medfilt2 - Perform 2-D median filtering.
ordfilt2 - Perform 2-D order-statistic filtering.
wiener2 - Perform 2-D adaptive noise-removal filtering.
Linear filtering.
conv2 - Perform 2-D convolution (MATLAB Toolbox).
convmtx2 - Compute 2-D convolution matrix.
convn - Perform N-D convolution (MATLAB Toolbox).
filter2 - Perform 2-D linear filtering (MATLAB Toolbox).
fspecial - Create predefined filters.
Linear 2-D filter design.
freqspace - Determine 2-D frequency response spacing (MATLAB Toolbox).
freqz2 - Compute 2-D frequency response.
fsamp2 - Design 2-D FIR filter using frequency sampling.
ftrans2 - Design 2-D FIR filter using frequency transformation.
fwind1 - Design 2-D FIR filter using 1-D window method.
fwind2 - Design 2-D FIR filter using 2-D window method.
Image transforms.
dct2 - Compute 2-D discrete cosine transform.
dctmtx - Compute discrete cosine transform matrix.
fft2 - Compute 2-D fast Fourier transform (MATLAB Toolbox).
fftn - Compute N-D fast Fourier transform (MATLAB Toolbox).
fftshift - Reverse quadrants of output of FFT (MATLAB Toolbox).
idct2 - Compute 2-D inverse discrete cosine transform.
ifft2 - Compute 2-D inverse fast Fourier transform (MATLAB Toolbox).
ifftn - Compute N-D inverse fast Fourier transform (MATLAB Toolbox).
iradon - Compute inverse Radon transform.
phantom - Generate a head phantom image.
radon - Compute Radon transform.
Neighborhood and block processing.
bestblk - Choose block size for block processing.
blkproc - Implement distinct block processing for image.
col2im - Rearrange matrix columns into blocks.
colfilt - Perform neighborhood operations using columnwise functions.
im2col - Rearrange image blocks into columns.
nlfilter - Perform general sliding-neighborhood operations.
Binary image operations.
applylut - Perform neighborhood operations using lookup tables.
bwarea - Compute area of objects in binary image.
bweuler - Compute Euler number of binary image.
bwfill - Fill background regions in binary image.
bwlabel - Label connected components in binary image.
bwmorph - Perform morphological operations on binary image.
bwperim - Determine perimeter of objects in binary image.
bwselect - Select objects in binary image.
dilate - Perform dilation on binary image.
erode - Perform erosion on binary image.
makelut - Construct lookup table for use with applylut.
Region-based processing.
roicolor - Select region of interest, based on color.
roifill - Smoothly interpolate within arbitrary region.
roifilt2 - Filter a region of interest.
roipoly - Select polygonal region of interest.
Colormap manipulation.
brighten - Brighten or darken colormap (MATLAB Toolbox).
cmpermute - Rearrange colors in colormap.
cmunique - Find unique colormap colors and corresponding image.
colormap - Set or get color lookup table (MATLAB Toolbox).
imapprox - Approximate indexed image by one with fewer colors.
rgbplot - Plot RGB colormap components (MATLAB Toolbox).
Color space conversions.
hsv2rgb - Convert HSV values to RGB color space (MATLAB Toolbox).
ntsc2rgb - Convert NTSC values to RGB color space.
rgb2hsv - Convert RGB values to HSV color space (MATLAB Toolbox).
rgb2ntsc - Convert RGB values to NTSC color space.
rgb2ycbcr - Convert RGB values to YCBCR color space.
ycbcr2rgb - Convert YCBCR values to RGB color space.
Image types and type conversions.
dither - Convert image using dithering.
gray2ind - Convert intensity image to indexed image.
grayslice - Create indexed image from intensity image by thresholding.
im2bw - Convert image to binary image by thresholding.
im2double - Convert image array to double precision.
im2uint8 - Convert image array to 8-bit unsigned integers.
im2uint16 - Convert image array to 16-bit unsigned integers.
ind2gray - Convert indexed image to intensity image.
ind2rgb - Convert indexed image to RGB image (MATLAB Toolbox).
isbw - Return true for binary image.
isgray - Return true for intensity image.
isind - Return true for indexed image.
isrgb - Return true for RGB image.
mat2gray - Convert matrix to intensity image.
rgb2gray - Convert RGB image or colormap to grayscale.
rgb2ind - Convert RGB image to indexed image.
Toolbox preferences.
iptgetpref - Get value of Image Processing Toolbox preference.
iptsetpref - Set value of Image Processing Toolbox preference.
Demos.
dctdemo - 2-D DCT image compression demo.
edgedemo - Edge detection demo.
firdemo - 2-D FIR filtering and filter design demo.
imadjdemo - Intensity adjustment and histogram equalization demo.
nrfiltdemo - Noise reduction filtering demo.
qtdemo - Quadtree decomposition demo.
roidemo - Region-of-interest processing demo.
Slide shows.
ipss001 - Region labeling of steel grains.
ipss002 - Feature-based logic.
ipss003 - Correction of nonuniform illumination.
9.0.1 Reading In the Image:
The help file on imread gives:
>> help imread
IMREAD Read image from graphics file.
A = IMREAD(FILENAME,FMT) reads the image in FILENAME into
A. If the file contains a grayscale intensity image, A is
a two-dimensional array. If the file contains a truecolor
(RGB) image, A is a three-dimensional (M-by-N-by-3) array.
FILENAME is a string that specifies the name of the
graphics file, and FMT is a string that specifies the
format of the file. The file must be in the current
directory or in a directory on the MATLAB path. If IMREAD
cannot find a file named FILENAME, it looks for a file
named FILENAME.FMT.
The possible values for FMT include:
'jpg' or 'jpeg' Joint Photographic Experts Group (JPEG)
'tif' or 'tiff' Tagged Image File Format (TIFF)
'bmp' Windows Bitmap (BMP)
'png' Portable Network Graphics
'hdf' Hierarchical Data Format (HDF)
'pcx' Windows Paintbrush (PCX)
'xwd' X Window Dump (XWD)
[X,MAP] = IMREAD(FILENAME,FMT) reads the indexed image in
FILENAME into X and its associated colormap into MAP.
Colormap values in the image file are automatically
rescaled into the range [0,1].
[...] = IMREAD(FILENAME) attempts to infer the format of the
file from its content.
Data types
----------
In most of the image file formats supported by IMREAD,
pixels are stored using 8 or fewer bits per color plane.
When reading such a file, the class of the output (A or X)
is uint8. IMREAD also supports reading 16-bit-per-pixel
data from TIFF and PNG files; for such image files, the
class of the output (A or X) is uint16.
TIFF-specific syntaxes
----------------------
[...] = IMREAD(...,IDX) reads in one image from a
multi-image TIFF file. IDX is an integer value that
specifies the order that the image appears in the file.
For example, if IDX is 3, IMREAD reads the third image in
the file. If you omit this argument, IMREAD reads the
first image in the file.
PNG-specific syntaxes
---------------------
[...] = IMREAD(...,'BackgroundColor',BG) composites any
transparent pixels in the input image against the color
specified in BG. If BG is 'none', then no compositing is
performed. Otherwise, if the input image is indexed, BG
should be an integer in the range [1,P] where P is the
colormap length. If the input image is grayscale, BG
should be an integer in the range [0,1]. If the input
image is RGB, BG should be a three-element vector whose
values are in the range [0,1]. The string
'BackgroundColor' may be abbreviated.
If the ALPHA output argument is used (see below), then BG
defaults to 'none' if not specified by the
user. Otherwise, if the PNG file contains a background
color chunk, that color is used as the default value for
BG. If ALPHA is not used and the file does not contain a
background color chunk, then the default value for BG is 1
for indexed images; 0 for grayscale images; and [0 0 0]
for RGB images.
[A,MAP,ALPHA] = IMREAD(...) returns the alpha channel if
one is present; otherwise ALPHA is []. Note that MAP may
be empty if the file contains a grayscale or truecolor
image.
HDF-specific syntaxes
---------------------
[...] = IMREAD(...,REF) reads in one image from a
multi-image HDF file. REF is an integer value that
specifies the reference number used to identify the image.
For example, if REF is 12, IMREAD reads the image whose
reference number is 12. (Note that in an HDF file the
reference numbers do not necessarily correspond with the
order of the images in the file. You can use IMFINFO to
match up image order with reference number.) If you omit
this argument, IMREAD reads the first image in the file.
Supported file types
--------------------
JPEG Any baseline JPEG image; JPEG images with some
commonly used extensions
TIFF Any baseline TIFF image, including 1-bit, 8-bit, and
24-bit uncompressed images; 1-bit, 8-bit, and 24-bit
images with packbits compression; 1-bit images with
CCITT compression; also, 16-bit grayscale, 16-bit
indexed, and 48-bit RGB images
BMP 1-bit, 4-bit, 8-bit, and 24-bit uncompressed images;
4-bit and 8-bit run-length encoded (RLE) images
PNG Any PNG image, including 1-bit, 2-bit, 4-bit, 8-bit,
and 16-bit grayscale images; 8-bit and 16-bit
indexed images; 24-bit and 48-bit RGB images
HDF 8-bit raster image datasets, with or without an
associated colormap; 24-bit raster image datasets
PCX 1-bit, 8-bit, and 24-bit images
XWD 1-bit and 8-bit ZPixmaps; XYBitmaps; 1-bit XYPixmaps
See also IMFINFO, IMWRITE, FREAD, IMAGE, DOUBLE, UINT8.
Ok, let's try this out on the gnome image I use on my
web page. First we see what type of file my gnome.jpg
is: as we expect we find
>> imfinfo('gnome.jpg')
ans =
Filename: 'gnome.jpg'
FileModDate: '05-Oct-2000 14:36:55'
FileSize: 19672
Format: 'jpg'
FormatVersion: ''
Width: 273
Height: 398
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: ''
>>
Now let's read in the image with the imread command:
>> C = imread('gnome.jpg');
Displaying the Image:
We can view the image by typing
>> image(C)
Note the image looks squashed. We can clean it up by
using the utility function truesize:
>> help truesize
TRUESIZE Adjust display size of image.
TRUESIZE(FIG,[MROWS NCOLS]) adjusts the display size of an
image. FIG is a figure containing a single image or a single
image with a colorbar. [MROWS MCOLS] is a 1-by-2 vector that
specifies the requested screen area (in pixels) that the
image should occupy.
TRUESIZE(FIG) uses the image height and width for
[MROWS MCOLS]. This results in the display having one screen
pixel for each image pixel.
If you omit the figure argument, TRUESIZE works on the
current figure.
Remarks
-------
If the 'TruesizeWarning' toolbox preference is 'on', TRUESIZE
displays a warning if the image is too large to fit on the
screen. (The entire image is still displayed, but at less
than true size.) If 'TruesizeWarning' is 'off', TRUESIZE does
not display the warning. Note that this preference applies
even when you call TRUESIZE indirectly, such as through
IMSHOW.
See also IMSHOW, IPTSETPREF, IPTGETPREF.
So now that we have the image displayed, we just type the
following to reset the aspect ratio to make it look more pleasing.
>> truesize
9.0.2 Manipulating the Image:
Our image here is stored in unsigned int 8 or uint8 format and
matLab won't let us add, multiply and so forth these things. So we
look up conversion routines.
Data types (classes)
double - Convert to double precision.
sparse - Create sparse matrix.
char - Create character array (string).
cell - Create cell array.
struct - Create or convert to structure array.
single - Convert to single precision.
uint8 - Convert to unsigned 8-bit integer.
uint16 - Convert to unsigned 16-bit integer.
uint32 - Convert to unsigned 32-bit integer.
int8 - Convert to signed 8-bit integer.
int16 - Convert to signed 16-bit integer.
int32 - Convert to signed 32-bit integer.
It looks like we should convert our image to double, do our
manipulations and then convert back to uint8.
It would go like this:
>> D = double(C); We convert C to doubles
>> E = DD/2; We half all the entries in D
>> F = uint8(E); We convert back to uint8
>> image(F) Now we can display the new image
>> imwrite(F,'gnome-divby2.jpg','jpg');
we can write our new image to a jpg
file if we want
Now today I put all our class pictures onto my hard drive so we
can start our manipulations. Walmart gv`ave us these images in
jpg format, so I can read these image files into Matlab with the
imread command. We can then compute the average images,
difference images and so forth.