changeset 7633:ba15376ddfe1

Add the contrast function
author David Bateman <dbateman@free.fr>
date Tue, 25 Mar 2008 12:03:26 -0400
parents d6e63a15cc75
children ae90e05ad299
files scripts/ChangeLog scripts/image/Makefile.in scripts/image/contrast.m
diffstat 3 files changed, 58 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Mon Mar 24 18:14:18 2008 -0400
+++ b/scripts/ChangeLog	Tue Mar 25 12:03:26 2008 -0400
@@ -1,3 +1,7 @@
+2008-03-25  David Bateman  <dbateman@free.fr>
+
+	* image/contrast.m: New function.
+
 2008-03-24  Thomas Weber  <thomas.weber.mail@gmail.com>
 
 	* pkg/pkg.m: Allow installation of already extracted packages.
--- a/scripts/image/Makefile.in	Mon Mar 24 18:14:18 2008 -0400
+++ b/scripts/image/Makefile.in	Tue Mar 25 12:03:26 2008 -0400
@@ -34,10 +34,10 @@
 INSTALL_DATA = @INSTALL_DATA@
 
 SOURCES = __img__.m __img_via_file__.m autumn.m bone.m brighten.m colormap.m \
-  cool.m copper.m flag.m gmap40.m gray.m gray2ind.m hot.m hsv.m hsv2rgb.m \
-  image.m image_viewer.m imagesc.m imshow.m ind2gray.m ind2rgb.m jet.m \
-  loadimage.m ntsc2rgb.m ocean.m pink.m prism.m rainbow.m rgb2hsv.m rgb2ind.m \
-  rgb2ntsc.m saveimage.m spring.m summer.m white.m winter.m
+  contrast.m cool.m copper.m flag.m gmap40.m gray.m gray2ind.m hot.m hsv.m \
+  hsv2rgb.m image.m image_viewer.m imagesc.m imshow.m ind2gray.m ind2rgb.m \
+  jet.m loadimage.m ntsc2rgb.m ocean.m pink.m prism.m rainbow.m rgb2hsv.m \
+  rgb2ind.m rgb2ntsc.m saveimage.m spring.m summer.m white.m winter.m
 
 IMAGES = default.img
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/image/contrast.m	Tue Mar 25 12:03:26 2008 -0400
@@ -0,0 +1,50 @@
+## Copyright (C) 2008  David Bateman
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} contrast (@var{x}, @var{n})
+## Return a gray colormap that maximizes the contrast in an image. The
+## returned colormap will have @var{n} rows. If @var{n} is not defined
+## then the size of the current colormap is used instead.
+## @seealso{colormap}
+## @end deftypefn
+
+function map = contrast (x, n)
+
+  if (nargin == 1)
+    n = rows (colormap);
+  elseif (nargin == 2)
+    if (! isscalar (n))
+      error ("contrast: n must be a scalar");
+    endif
+  else
+    print_usage ();
+  endif
+
+  x = x(:);
+  minx = min (x);
+  map = find (diff (sort ([round(n * ((x - minx) ./ (max(x) - minx))); [0:n]'])));
+  minm = min (map);
+  map = (map - minm) ./ (max (map) - minm);
+  map = [map, map, map];
+endfunction
+
+%!assert (contrast(1:100,10),[([0:9]/9)',([0:9]/9)',([0:9]/9)'],1e-10)
+%!demo
+%! image (reshape (1:100, 10, 10))
+%! colormap (contrast (1:100,10))