changeset 9175:49f69874df97 octave-forge

imabsdiff: new function
author carandraug
date Sat, 10 Dec 2011 23:36:58 +0000
parents 85de2460068c
children 4b9998d43c9a
files main/image/INDEX main/image/NEWS main/image/inst/imabsdiff.m main/image/inst/imadd.m main/image/inst/imdivide.m main/image/inst/immultiply.m main/image/inst/imsubtract.m
diffstat 7 files changed, 73 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/main/image/INDEX	Sat Dec 10 23:34:33 2011 +0000
+++ b/main/image/INDEX	Sat Dec 10 23:36:58 2011 +0000
@@ -20,6 +20,7 @@
  regionprops
  stdfilt
 Arithmetics
+ imabsdiff
  imadd
  imcomplement
  imdivide
--- a/main/image/NEWS	Sat Dec 10 23:34:33 2011 +0000
+++ b/main/image/NEWS	Sat Dec 10 23:36:58 2011 +0000
@@ -6,6 +6,7 @@
       blockproc
       bwlabeln
       getrangefromclass
+      imabsdiff
       imadd
       imbothat
       imdivide
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/image/inst/imabsdiff.m	Sat Dec 10 23:36:58 2011 +0000
@@ -0,0 +1,64 @@
+## Copyright (C) 2011 Carnë Draug <carandraug+dev@gmail.com>
+##
+## This program 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.
+##
+## This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{out} =} imabsdiff (@var{a}, @var{b})
+## @deftypefnx {Function File} {@var{out} =} imabsdiff (@var{a}, @var{b}, @var{class})
+## Return absolute difference of image or constant to an image.
+##
+## If @var{a} and @var{b} are two images of same size and class, returns the absolute
+## difference between @var{b} and @var{a}.
+##
+## The class of @var{out} will be the same as @var{a} unless @var{a} is logical
+## in which case @var{out} will be double. Alternatively, the class can be
+## specified with @var{class}.
+##
+## @emph{Note 1}: you can force output class to be logical by specifying
+## @var{class}. This is incompatible with @sc{matlab} which will @emph{not} honour
+## request to return a logical matrix.
+##
+## @emph{Note 2}: the values are truncated to the mininum value of the output
+## class.
+## @seealso{imadd, imcomplement, imdivide, immultiply, imsubtract}
+## @end deftypefn
+
+function img = imabsdiff (img, val, out_class = class (img))
+
+  if (nargin < 2 || nargin > 3)
+    print_usage;
+  endif
+
+  ## we want to make subtraction as double so this is it
+  [img, val] = imarithmetics ("imabsdiff", img, val, "double");
+  converter = str2func (tolower (out_class));
+
+  if (nargin < 3 && strcmp (out_class, "logical"))
+    ## it is using logical as default. Use double instead. We only have this
+    ## problem on this function because we are are not actually giving out_class
+    ## to imarithmetics
+    converter = @double;
+  else
+    converter = str2func (tolower (out_class));
+  endif
+  img = converter (abs (img - val));
+
+endfunction
+
+%!assert (imabsdiff (uint8   ([23 250]), uint8   ([26  50])),            uint8   ([ 3 200])); # default to first class and abs works
+%!assert (imabsdiff (uint8   ([23 250]), uint8   ([24  50]), "uint16"),  uint16  ([ 1 200])); # defining output class works (not in matlab)
+%!assert (imabsdiff (uint8   ([23 250]), uint8   ([24 255]), "int8"),    int8    ([ 1   5])); # signed integers kinda work (not in matlab)
+%!assert (imabsdiff (logical ([ 1   0]), logical ([ 1   1])),            double  ([ 0   1])); # return double for two logical images
+%!fail  ("imabsdiff (uint8   ([23 250]), 30");                                                # fails subtracting a scalar
+%!fail  ("imabsdiff (uint8   ([23 250]), uint16  ([23 250]))");                               # input need to have same class
--- a/main/image/inst/imadd.m	Sat Dec 10 23:34:33 2011 +0000
+++ b/main/image/inst/imadd.m	Sat Dec 10 23:36:58 2011 +0000
@@ -32,7 +32,7 @@
 ##
 ## @emph{Note 2}: the values are truncated to the maximum value of the output
 ## class.
-## @seealso{imcomplement, imdivide, immultiply, imsubtract}
+## @seealso{imabsdiff, imcomplement, imdivide, immultiply, imsubtract}
 ## @end deftypefn
 
 function img = imadd (img, val, out_class = class (img))
@@ -70,4 +70,4 @@
 %!assert (imadd (uint8   ([23 250]), uint8   ([23 250]), "uint16"),  uint16  ([46 500])); # defining output class works
 %!assert (imadd (logical ([ 1   0]), logical ([ 1   1])),            double  ([ 2   1])); # return double for two logical images
 %!assert (imadd (logical ([ 1   0]), logical ([ 1   1]), "logical"), logical ([ 1   1])); # this is matlab incompatible on purpose
-%!fail  ("imadd (uint8   ([23 250]), uint16  ([23 250]))")                                # input need to have same class
+%!fail  ("imadd (uint8   ([23 250]), uint16  ([23 250]))");                               # input need to have same class
--- a/main/image/inst/imdivide.m	Sat Dec 10 23:34:33 2011 +0000
+++ b/main/image/inst/imdivide.m	Sat Dec 10 23:36:58 2011 +0000
@@ -28,7 +28,7 @@
 ##
 ## @emph{Note}: the values are truncated to the mininum value of the output
 ## class.
-## @seealso{imadd, imcomplement, immultiply, imsubtract}
+## @seealso{imabsdiff, imadd, imcomplement, immultiply, imsubtract}
 ## @end deftypefn
 
 function img = imdivide (img, val, out_class = class (img))
@@ -58,4 +58,4 @@
 %!assert (imdivide (uint8   ([23 250]), 2),                             uint8   ([ 12 125])); # works subtracting a scalar
 %!assert (imdivide (uint8   ([23 250]), uint8   ([ 2  50]), "uint16"),  uint16  ([ 12   5])); # defining output class works (not in matlab)
 %!assert (imdivide (logical ([1 1 0 0]), logical ([1 0 1 0])),          double  ([1 Inf 0 NaN])); # dividing logical matrix  (tested in matlab)
-%!fail  ("imdivide (uint8   ([23 250]), uint16  ([23 250]))")                                 # input needs to have same class
+%!fail  ("imdivide (uint8   ([23 250]), uint16  ([23 250]))");                                # input needs to have same class
--- a/main/image/inst/immultiply.m	Sat Dec 10 23:34:33 2011 +0000
+++ b/main/image/inst/immultiply.m	Sat Dec 10 23:36:58 2011 +0000
@@ -28,7 +28,7 @@
 ##
 ## @emph{Note}: the values are truncated to the mininum value of the output
 ## class.
-## @seealso{imadd, imcomplement, imdivide, imsubtract}
+## @seealso{imabsdiff, imadd, imcomplement, imdivide, imsubtract}
 ## @end deftypefn
 
 function img = immultiply (img, val, out_class = class (img))
--- a/main/image/inst/imsubtract.m	Sat Dec 10 23:34:33 2011 +0000
+++ b/main/image/inst/imsubtract.m	Sat Dec 10 23:36:58 2011 +0000
@@ -46,7 +46,7 @@
 ##
 ## Because both 190 and 200 were truncated to 127 before subtraction, their difference
 ## is zero.
-## @seealso{imadd, imcomplement, imdivide, immultiply}
+## @seealso{imabsdiff, imadd, imcomplement, imdivide, immultiply}
 ## @end deftypefn
 
 function img = imsubtract (img, val, out_class = class (img))
@@ -80,4 +80,4 @@
 %!assert (imsubtract (uint8   ([23 250]), uint8   ([24 255]), "int8"),    int8    ([-1   0])); # signed integers kinda work (not in matlab)
 %!assert (imsubtract (logical ([ 1   0]), logical ([ 1   1])),            double  ([ 0  -1])); # return double for two logical images
 %!assert (imsubtract (logical ([ 1   0]), logical ([ 1   1]), "logical"), logical ([ 0   0])); # this is matlab incompatible on purpose
-%!fail  ("imsubtract (uint8   ([23 250]), uint16  ([23 250]))")                                # input need to have same class
+%!fail  ("imsubtract (uint8   ([23 250]), uint16  ([23 250]))");                               # input need to have same class