annotate scripts/image/contrast.m @ 15683:806ea52af230

Overhaul m-files in image directory to provide better support for images stored as integers. * NEWS: Add note about overhaul of image scripts to support integer classes. * brighten.m: Add demo. * colormap.m: Better input validation. * contrast.m: Re-position window in demo. * gray2ind.m: Redo docstring. Match variables in docstring to function prototype. Better input validation. Return integer class outputs as Matlab does. Add %!tests. * hsv2rgb.m: Add to docstring. Better input validation. Redo algorithm to get rido of obsolete matrix-fill methods like kron(). Add %!tests. * image.m: Redo docstring. Match variables in docstring to function prototype. Better input validation without using for loops. * imagesc.m: Redo docstring. Match variables in docstring to function prototype. Remove DEPRECATEDZOOM functionality. Add demos. * imfinfo.m: Redo docstring. * ind2gray.m: Redo docstring. Match variables in docstring to function prototype. Redo algorithm to directly calculate luminance value rather than getting it from rgb2ntsc. Add %!tests. * ind2rgb.m: Redo docstring. Better input validation. Add some %!tests. * ntsc2rgb.m: Redo docstring. Better input validation. Add %!tests. * rgb2hsv.m: Better input validation. Add %!tests. * rgb2ind.m: Better input validation. Code algorithm in cleaner method for ease of understanding. * rgb2ntsc.m: Redo docstring: Better input validation. Add some %!tests.
author Rik <rik@octave.org>
date Tue, 27 Nov 2012 16:38:13 -0800
parents 1f911333ed3d
children b1cd65881592
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1 ## Copyright (C) 2008-2012 David Bateman
7633
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
2 ##
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
3 ## This file is part of Octave.
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
4 ##
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 ## your option) any later version.
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
9 ##
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
13 ## General Public License for more details.
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
14 ##
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
18
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
19 ## -*- texinfo -*-
14260
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14253
diff changeset
20 ## @deftypefn {Function File} {@var{map} =} contrast (@var{x})
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14253
diff changeset
21 ## @deftypefnx {Function File} {@var{map} =} contrast (@var{x}, @var{n})
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 7633
diff changeset
22 ## Return a gray colormap that maximizes the contrast in an image. The
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 7633
diff changeset
23 ## returned colormap will have @var{n} rows. If @var{n} is not defined
14245
4506eade9f04 Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents: 14237
diff changeset
24 ## then the size of the current colormap is used.
14260
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14253
diff changeset
25 ## @seealso{colormap, brighten}
7633
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
26 ## @end deftypefn
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
27
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
28 function map = contrast (x, n)
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
29
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
30 if (nargin == 1)
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
31 n = rows (colormap);
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
32 elseif (nargin == 2)
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
33 if (! isscalar (n))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
34 error ("contrast: N must be a scalar");
7633
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
35 endif
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
36 else
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
37 print_usage ();
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
38 endif
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
39
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
40 x = x(:);
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
41 minx = min (x);
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
42 map = find (diff (sort ([round(n * ((x - minx) ./ (max(x) - minx))); [0:n]'])));
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
43 minm = min (map);
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
44 map = (map - minm) ./ (max (map) - minm);
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
45 map = [map, map, map];
14245
4506eade9f04 Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents: 14237
diff changeset
46
7633
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
47 endfunction
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
48
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
49
7633
ba15376ddfe1 Add the contrast function
David Bateman <dbateman@free.fr>
parents:
diff changeset
50 %!demo
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
51 %! figure;
14245
4506eade9f04 Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents: 14237
diff changeset
52 %! img = reshape (1:100, 10, 10);
14253
f9bd63f5ddd0 contrast.m: Improve demo.
Rik <octave@nomad.inbox5.com>
parents: 14245
diff changeset
53 %! imagesc (img);
f9bd63f5ddd0 contrast.m: Improve demo.
Rik <octave@nomad.inbox5.com>
parents: 14245
diff changeset
54 %! colormap (gray (64));
f9bd63f5ddd0 contrast.m: Improve demo.
Rik <octave@nomad.inbox5.com>
parents: 14245
diff changeset
55 %! title ("Image with default 64 gray levels");
f9bd63f5ddd0 contrast.m: Improve demo.
Rik <octave@nomad.inbox5.com>
parents: 14245
diff changeset
56 %! pos = get (gcf, "position");
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
57 %! pos(1) += pos(3) + 25;
14253
f9bd63f5ddd0 contrast.m: Improve demo.
Rik <octave@nomad.inbox5.com>
parents: 14245
diff changeset
58 %! figure ("position", pos);
f9bd63f5ddd0 contrast.m: Improve demo.
Rik <octave@nomad.inbox5.com>
parents: 14245
diff changeset
59 %! colormap (contrast (img, 10));
f9bd63f5ddd0 contrast.m: Improve demo.
Rik <octave@nomad.inbox5.com>
parents: 14245
diff changeset
60 %! imagesc (img);
14245
4506eade9f04 Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents: 14237
diff changeset
61 %! title ("Image with contrast enhanced");
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
62
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
63 %!assert (contrast (1:100,10), [([0:9]/9)',([0:9]/9)',([0:9]/9)'], 1e-10)
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
64