6778
|
1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton |
7018
|
2 @c |
|
3 @c This file is part of Octave. |
|
4 @c |
|
5 @c Octave is free software; you can redistribute it and/or modify it |
|
6 @c under the terms of the GNU General Public License as published by the |
|
7 @c Free Software Foundation; either version 3 of the License, or (at |
|
8 @c your option) any later version. |
|
9 @c |
|
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 @c for more details. |
|
14 @c |
|
15 @c You should have received a copy of the GNU General Public License |
|
16 @c along with Octave; see the file COPYING. If not, see |
|
17 @c <http://www.gnu.org/licenses/>. |
3294
|
18 |
4167
|
19 @node Image Processing |
3294
|
20 @chapter Image Processing |
|
21 |
6529
|
22 Since an image basically is a matrix Octave is a very powerful |
|
23 environment for processing and analysing images. To illustrate |
|
24 how easy it is to do image processing in Octave, the following |
|
25 example will load an image, smooth it by a 5-by-5 averaging filter, |
|
26 and compute the gradient of the smoothed image. |
|
27 |
|
28 @example |
6535
|
29 I = loadimage ("default.img"); |
|
30 S = conv2 (I, ones (5, 5) / 25, "same"); |
|
31 [Dx, Dy] = gradient (S); |
6529
|
32 @end example |
|
33 |
|
34 @noindent |
|
35 In this example @code{S} contains the smoothed image, and @code{Dx} |
|
36 and @code{Dy} contains the partial spatial derivatives of the image. |
|
37 |
6535
|
38 @menu |
|
39 * Loading and Saving Images:: |
|
40 * Displaying Images:: |
|
41 * Representing Images:: |
|
42 * Plotting on top of Images:: |
|
43 * Color Conversion:: |
|
44 @end menu |
|
45 |
6529
|
46 @node Loading and Saving Images |
|
47 @section Loading and Saving Images |
|
48 |
|
49 The first step in most image processing tasks is to load an image |
|
50 into Octave. Currently Octave only support saving images in the |
|
51 Portable Pixmap Format (PPM), PostScript, and Octave's own format, and |
|
52 loading images in Octave's format. Most image processing code will |
|
53 follow the structure of this code |
3294
|
54 |
6529
|
55 @example |
6535
|
56 I = loadimage ("my_input_image.img"); |
|
57 J = process_my_image (I); |
|
58 saveimage ("my_output_image.img", J); |
6529
|
59 @end example |
|
60 |
|
61 @DOCSTRING(loadimage) |
|
62 |
|
63 @DOCSTRING(saveimage) |
|
64 |
|
65 @DOCSTRING(IMAGE_PATH) |
3294
|
66 |
6529
|
67 @node Displaying Images |
|
68 @section Displaying Images |
3294
|
69 |
6529
|
70 A natural part of image processing is visualization of an image. |
|
71 The most basic function for this is the @code{imshow} function that |
|
72 shows the image given in the first input argument. This function uses |
|
73 an external program to show the image. If gnuplot 4.2 or later is |
|
74 available it will be used to display the image, otherwise the |
|
75 @code{display}, @code{xv}, or @code{xloadimage} program is used. The |
|
76 actual program can be selected with the @code{image_viewer} function. |
3294
|
77 |
6529
|
78 @DOCSTRING(imshow) |
3294
|
79 |
3373
|
80 @DOCSTRING(image) |
3294
|
81 |
3373
|
82 @DOCSTRING(imagesc) |
3294
|
83 |
6529
|
84 @DOCSTRING(image_viewer) |
|
85 |
|
86 @node Representing Images |
|
87 @section Representing Images |
|
88 |
|
89 In general Octave supports four different kinds of images, gray-scale |
|
90 images, RGB images, binary images, and indexed images. A gray-scale |
6535
|
91 image is represented with an M-by-N matrix in which each |
6529
|
92 element corresponds to the intensity of a pixel. An RGB image is |
6535
|
93 represented with an M-by-N-by3 array where each |
6529
|
94 3-vector corresponds to the red, green, and blue intensities of each |
|
95 pixel. |
|
96 |
|
97 The actual meaning of the value of a pixel in a gray-scale or RGB |
|
98 image depends on the class of the matrix. If the matrix is of class |
|
99 @code{double} pixel intensities are between 0 and 1, if it is of class |
|
100 @code{uint8} intensities are between 0 and 255, and if it is of class |
|
101 @code{uint16} intensities are between 0 and 65535. |
|
102 |
6535
|
103 A binary image is a M-by-N matrix of class @code{logical}. |
6529
|
104 A pixel in a binary image is black if it is @code{false} and white |
|
105 if it is @code{true}. |
|
106 |
6535
|
107 An indexed image consists of an M-by-N matrix of integers |
|
108 and a C-by-3 color map. Each integer corresponds to an |
6529
|
109 index in the color map, and each row in the color map corresponds to |
|
110 a RGB color. The color map must be of class @code{double} with values |
|
111 between 0 and 1. |
|
112 |
|
113 @DOCSTRING(gray2ind) |
3294
|
114 |
3373
|
115 @DOCSTRING(ind2gray) |
3294
|
116 |
6529
|
117 @DOCSTRING(rgb2ind) |
|
118 |
3373
|
119 @DOCSTRING(ind2rgb) |
3294
|
120 |
6529
|
121 @DOCSTRING(colormap) |
|
122 |
6788
|
123 @DOCSTRING(autumn) |
|
124 |
|
125 @DOCSTRING(bone) |
|
126 |
|
127 @DOCSTRING(cool) |
|
128 |
|
129 @DOCSTRING(copper) |
|
130 |
6529
|
131 @DOCSTRING(gray) |
|
132 |
6788
|
133 @DOCSTRING(hot) |
|
134 |
|
135 @DOCSTRING(hsv) |
|
136 |
|
137 @DOCSTRING(jet) |
|
138 |
6529
|
139 @DOCSTRING(ocean) |
|
140 |
6788
|
141 @DOCSTRING(pink) |
|
142 |
|
143 @DOCSTRING(prism) |
|
144 |
|
145 @DOCSTRING(rainbow) |
|
146 |
|
147 @DOCSTRING(spring) |
|
148 |
|
149 @DOCSTRING(summer) |
|
150 |
|
151 @DOCSTRING(white) |
|
152 |
|
153 @DOCSTRING(winter) |
|
154 |
6529
|
155 @node Plotting on top of Images |
|
156 @section Plotting on top of Images |
|
157 |
|
158 If gnuplot is being used to display images it is possible to plot on |
|
159 top of images. Since an image is a matrix it is indexed by row and |
|
160 column values. The plotting system is, however, based on the |
|
161 traditional @math{(x, y)} system. To minimize the difference between |
|
162 the two systems Octave places the origin of the coordinate system in |
|
163 the point corresponding to the pixel at @math{(1, 1)}. So, to plot |
|
164 points given by row and column values on top of an image, one should |
|
165 simply call @code{plot} with the column values as the first argument |
|
166 and the row values as the second. As an example the following code |
|
167 generates an image with random intensities between 0 and 1, and shows |
|
168 the image with red circles over pixels with an intensity above |
|
169 @math{0.99}. |
|
170 |
|
171 @example |
6535
|
172 I = rand (100, 100); |
|
173 [row, col] = find (I > 0.99); |
|
174 hold ("on"); |
|
175 imshow (I); |
|
176 plot (col, row, "ro"); |
|
177 hold ("off"); |
6529
|
178 @end example |
|
179 |
|
180 @node Color Conversion |
|
181 @section Color Conversion |
|
182 |
|
183 Octave supports conversion from the RGB color system to NTSC and HSV |
|
184 and vice versa. |
|
185 |
|
186 @DOCSTRING(rgb2hsv) |
|
187 |
|
188 @DOCSTRING(hsv2rgb) |
3294
|
189 |
3373
|
190 @DOCSTRING(rgb2ntsc) |
3294
|
191 |
3373
|
192 @DOCSTRING(ntsc2rgb) |
3294
|
193 |
3803
|
194 |