Mercurial > octave-nkf
annotate scripts/image/ind2gray.m @ 15714:b1cd65881592
Clean up scripts in image directory.
Use Octave coding conventions. Redo docstrings. Add %!tests.
* brighten.m: Put input validation first. Use iscolormap to simplify
input checking.
* cmunique.m: Use faster method of validating input class.
* colormap.m: Tweak docstring. Improve input validation.
* contrast.m: Tweak docstring. Use cmap instead of map as variable
name for clarity.
* gray2ind.m: Wrap long lines. Use faster method of validating input class.
Delete unreachable code for n>65536.
* hsv2rgb.m: Use faster method of validating input class.
* imwrite.m: Tweak FIXME notes.
* ind2gray.m: Use correct caller name for ind2x. Update %!tests
with new 2-input calling convention.
* ind2rgb.m: Tweak docstring. Update %!tests with new 2-input
calling convention.
* iscolormap.m: Tweak docstring. Re-order validation tests.
* ntsc2rgb.m: Use faster method of validating input class. Better input
validation. Add %!tests.
* private/ind2x.m: Use more descriptive variable names.
* rgb2hsv.m: Tweak docstring. Use faster method of validating input class.
* rgb2ind.m: Tweak docstring. Wrap long lines.
* rgb2ntsc.m: Use faster method of validating input class. Improve input
validation. Add %!tests.
* rgbplot.m: Match variable names in docstring to those in function
prototype.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 02 Dec 2012 10:02:57 -0800 |
parents | 74ff287f41cb |
children | bc924baa2c4e |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1 ## Copyright (C) 1994-2012 John W. Eaton |
2313 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
1024 | 18 |
3381 | 19 ## -*- texinfo -*- |
15712
74ff287f41cb
Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents:
15693
diff
changeset
|
20 ## @deftypefn {Function File} {@var{I} =} ind2gray (@var{x}, @var{map}) |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
21 ## Convert a color indexed image to a grayscale intensity image. |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
22 ## |
15712
74ff287f41cb
Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents:
15693
diff
changeset
|
23 ## The image @var{x} must be an indexed image which will be converted using the |
74ff287f41cb
Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents:
15693
diff
changeset
|
24 ## colormap @var{cmap}. If @var{cmap} does not contain enough colors for the |
74ff287f41cb
Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents:
15693
diff
changeset
|
25 ## image, pixels in @var{x} outside the range are mapped to the last color in |
74ff287f41cb
Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents:
15693
diff
changeset
|
26 ## the map before conversion to grayscale. |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
27 ## |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
28 ## The output @var{I} is of the same class as the input @var{x} and may be |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
29 ## one of @code{uint8}, @code{uint16}, @code{single}, or @code{double}. |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
30 ## |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
31 ## Implementation Note: There are several ways of converting colors to |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
32 ## grayscale intensities. This functions uses the luminance value obtained |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
33 ## from @code{rgb2ntsc} which is @code{I = 0.299*R + 0.587*G + 0.114*B}. |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
34 ## Other possibilities include the value component from @code{rgb2hsv} or |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
35 ## using a single color channel from @code{ind2rgb}. |
14260
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
36 ## @seealso{gray2ind, ind2rgb} |
3373 | 37 ## @end deftypefn |
904 | 38 |
3202 | 39 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312 | 40 ## Created: July 1994 |
41 ## Adapted-By: jwe | |
559 | 42 |
15712
74ff287f41cb
Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents:
15693
diff
changeset
|
43 function I = ind2gray (x, map) |
1024 | 44 |
15712
74ff287f41cb
Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents:
15693
diff
changeset
|
45 if (nargin != 2) |
6046 | 46 print_usage (); |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
47 endif |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15712
diff
changeset
|
48 [x, map] = ind2x ("ind2gray", x, map); |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
49 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
50 ## Convert colormap to luminance intensity values |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
51 map *= [0.29894; 0.58704; 0.11402]; |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
52 |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15712
diff
changeset
|
53 ## Convert colormap to same class as that of input so that reshape |
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15712
diff
changeset
|
54 ## will produce output of the same type as the input. |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
55 cls = class (x); |
15712
74ff287f41cb
Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents:
15693
diff
changeset
|
56 if (isinteger (x)) |
74ff287f41cb
Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents:
15693
diff
changeset
|
57 ## if we later add support for int16 images, this will not work. Look into |
74ff287f41cb
Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents:
15693
diff
changeset
|
58 ## im2int16 from image package for such case |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
59 map *= intmax (cls); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
60 elseif (strcmp (cls, "single")) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
61 map = single (map); |
559 | 62 endif |
63 | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
64 ## Replace indices in the input matrix with the indexed luminance value. |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
65 I = reshape (map(x(:)), size (x)); |
559 | 66 |
67 endfunction | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
68 |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15712
diff
changeset
|
69 |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
70 %!shared i2g |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
71 %! i2g = ind2gray (1:100, gray (100)); |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
72 %!assert (i2g, 0:1/99:1, eps); |
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
73 %!assert (gray2ind (i2g, 100), uint8 (0:99)); |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
74 |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15712
diff
changeset
|
75 %% Test input validation |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
76 %!error ind2gray () |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15712
diff
changeset
|
77 %!error ind2gray (1) |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
78 %!error ind2gray (1,2,3) |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15712
diff
changeset
|
79 %!error <X must be an indexed image> ind2gray ({1}, jet (64)) |
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15712
diff
changeset
|
80 %!error <X must be an indexed image> ind2gray (1+i, jet (64)) |
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15712
diff
changeset
|
81 %!error <X must be an indexed image> ind2gray (sparse (1), jet (64)) |
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15712
diff
changeset
|
82 %!error <X must be an indexed image> ind2gray (0, jet (64)) |
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15712
diff
changeset
|
83 %!error <X must be an indexed image> ind2gray (1.1, jet (64)) |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
84 %!error <MAP must be a valid colormap> ind2gray (1, {1}) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
85 %!error <MAP must be a valid colormap> ind2gray (1, 1+i) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
86 %!error <MAP must be a valid colormap> ind2gray (1, ones (2,2,2)) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
87 %!error <MAP must be a valid colormap> ind2gray (1, ones (2,4)) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
88 %!error <MAP must be a valid colormap> ind2gray (1, [-1]) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
89 %!error <MAP must be a valid colormap> ind2gray (1, [2]) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
90 |