annotate doc/interpreter/image.txi @ 6778:083721ae3dfa

[project @ 2007-07-18 17:03:10 by jwe]
author jwe
date Wed, 18 Jul 2007 17:03:11 +0000
parents 3ef1aa12f04c
children c81a0f3f5a82
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6778
083721ae3dfa [project @ 2007-07-18 17:03:10 by jwe]
jwe
parents: 6535
diff changeset
1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
2 @c This is part of the Octave manual.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
3 @c For copying conditions, see the file gpl.texi.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
4
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3803
diff changeset
5 @node Image Processing
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
6 @chapter Image Processing
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
7
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
8 Since an image basically is a matrix Octave is a very powerful
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
9 environment for processing and analysing images. To illustrate
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
10 how easy it is to do image processing in Octave, the following
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
11 example will load an image, smooth it by a 5-by-5 averaging filter,
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
12 and compute the gradient of the smoothed image.
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
13
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
14 @example
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
15 I = loadimage ("default.img");
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
16 S = conv2 (I, ones (5, 5) / 25, "same");
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
17 [Dx, Dy] = gradient (S);
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
18 @end example
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
19
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
20 @noindent
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
21 In this example @code{S} contains the smoothed image, and @code{Dx}
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
22 and @code{Dy} contains the partial spatial derivatives of the image.
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
23
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
24 @menu
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
25 * Loading and Saving Images::
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
26 * Displaying Images::
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
27 * Representing Images::
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
28 * Plotting on top of Images::
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
29 * Color Conversion::
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
30 @end menu
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
31
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
32 @node Loading and Saving Images
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
33 @section Loading and Saving Images
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
34
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
35 The first step in most image processing tasks is to load an image
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
36 into Octave. Currently Octave only support saving images in the
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
37 Portable Pixmap Format (PPM), PostScript, and Octave's own format, and
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
38 loading images in Octave's format. Most image processing code will
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
39 follow the structure of this code
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
40
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
41 @example
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
42 I = loadimage ("my_input_image.img");
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
43 J = process_my_image (I);
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
44 saveimage ("my_output_image.img", J);
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
45 @end example
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
46
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
47 @DOCSTRING(loadimage)
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
48
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
49 @DOCSTRING(saveimage)
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
50
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
51 @DOCSTRING(IMAGE_PATH)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
52
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
53 @node Displaying Images
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
54 @section Displaying Images
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
55
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
56 A natural part of image processing is visualization of an image.
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
57 The most basic function for this is the @code{imshow} function that
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
58 shows the image given in the first input argument. This function uses
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
59 an external program to show the image. If gnuplot 4.2 or later is
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
60 available it will be used to display the image, otherwise the
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
61 @code{display}, @code{xv}, or @code{xloadimage} program is used. The
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
62 actual program can be selected with the @code{image_viewer} function.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
63
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
64 @DOCSTRING(imshow)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
65
3373
36405da8e173 [project @ 1999-11-23 20:54:17 by jwe]
jwe
parents: 3294
diff changeset
66 @DOCSTRING(image)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
67
3373
36405da8e173 [project @ 1999-11-23 20:54:17 by jwe]
jwe
parents: 3294
diff changeset
68 @DOCSTRING(imagesc)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
69
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
70 @DOCSTRING(image_viewer)
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
71
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
72 @node Representing Images
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
73 @section Representing Images
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
74
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
75 In general Octave supports four different kinds of images, gray-scale
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
76 images, RGB images, binary images, and indexed images. A gray-scale
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
77 image is represented with an M-by-N matrix in which each
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
78 element corresponds to the intensity of a pixel. An RGB image is
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
79 represented with an M-by-N-by3 array where each
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
80 3-vector corresponds to the red, green, and blue intensities of each
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
81 pixel.
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
82
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
83 The actual meaning of the value of a pixel in a gray-scale or RGB
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
84 image depends on the class of the matrix. If the matrix is of class
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
85 @code{double} pixel intensities are between 0 and 1, if it is of class
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
86 @code{uint8} intensities are between 0 and 255, and if it is of class
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
87 @code{uint16} intensities are between 0 and 65535.
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
88
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
89 A binary image is a M-by-N matrix of class @code{logical}.
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
90 A pixel in a binary image is black if it is @code{false} and white
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
91 if it is @code{true}.
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
92
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
93 An indexed image consists of an M-by-N matrix of integers
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
94 and a C-by-3 color map. Each integer corresponds to an
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
95 index in the color map, and each row in the color map corresponds to
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
96 a RGB color. The color map must be of class @code{double} with values
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
97 between 0 and 1.
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
98
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
99 @DOCSTRING(gray2ind)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
100
3373
36405da8e173 [project @ 1999-11-23 20:54:17 by jwe]
jwe
parents: 3294
diff changeset
101 @DOCSTRING(ind2gray)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
102
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
103 @DOCSTRING(rgb2ind)
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
104
3373
36405da8e173 [project @ 1999-11-23 20:54:17 by jwe]
jwe
parents: 3294
diff changeset
105 @DOCSTRING(ind2rgb)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
106
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
107 @DOCSTRING(colormap)
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
108
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
109 @DOCSTRING(gray)
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
110
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
111 @DOCSTRING(ocean)
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
112
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
113 @node Plotting on top of Images
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
114 @section Plotting on top of Images
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
115
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
116 If gnuplot is being used to display images it is possible to plot on
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
117 top of images. Since an image is a matrix it is indexed by row and
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
118 column values. The plotting system is, however, based on the
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
119 traditional @math{(x, y)} system. To minimize the difference between
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
120 the two systems Octave places the origin of the coordinate system in
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
121 the point corresponding to the pixel at @math{(1, 1)}. So, to plot
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
122 points given by row and column values on top of an image, one should
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
123 simply call @code{plot} with the column values as the first argument
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
124 and the row values as the second. As an example the following code
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
125 generates an image with random intensities between 0 and 1, and shows
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
126 the image with red circles over pixels with an intensity above
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
127 @math{0.99}.
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
128
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
129 @example
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
130 I = rand (100, 100);
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
131 [row, col] = find (I > 0.99);
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
132 hold ("on");
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
133 imshow (I);
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
134 plot (col, row, "ro");
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6529
diff changeset
135 hold ("off");
6529
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
136 @end example
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
137
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
138 @node Color Conversion
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
139 @section Color Conversion
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
140
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
141 Octave supports conversion from the RGB color system to NTSC and HSV
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
142 and vice versa.
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
143
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
144 @DOCSTRING(rgb2hsv)
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
145
853f99e292ec [project @ 2007-04-16 21:29:03 by jwe]
jwe
parents: 6502
diff changeset
146 @DOCSTRING(hsv2rgb)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
147
3373
36405da8e173 [project @ 1999-11-23 20:54:17 by jwe]
jwe
parents: 3294
diff changeset
148 @DOCSTRING(rgb2ntsc)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
149
3373
36405da8e173 [project @ 1999-11-23 20:54:17 by jwe]
jwe
parents: 3294
diff changeset
150 @DOCSTRING(ntsc2rgb)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
151
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents: 3373
diff changeset
152