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