From: <car...@us...> - 2011-12-05 04:20:56
|
Revision: 9269 http://octave.svn.sourceforge.net/octave/?rev=9269&view=rev Author: carandraug Date: 2011-12-05 04:20:50 +0000 (Mon, 05 Dec 2011) Log Message: ----------- isrgb: * pretty much complete rewrite based on isgray * added Kai Habel to copyright since it's based on isgray from him * improved help text * update license to GPLv3+ * checked matlab, logical MxNx3 matrix should return false Modified Paths: -------------- trunk/octave-forge/main/image/inst/isrgb.m Modified: trunk/octave-forge/main/image/inst/isrgb.m =================================================================== --- trunk/octave-forge/main/image/inst/isrgb.m 2011-12-05 04:06:15 UTC (rev 9268) +++ trunk/octave-forge/main/image/inst/isrgb.m 2011-12-05 04:20:50 UTC (rev 9269) @@ -1,8 +1,10 @@ -## Copyright (C) 2004 Josep Mones i Teixidor +## Copyright (C) 2000 Kai Habel <kai...@gm...> +## Copyright (C) 2004 Josep Mones i Teixidor <jm...@pu...> +## Copyright (C) 2011 Carnë Draug <car...@gm...> ## ## 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 2 of the License, or +## 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, @@ -14,67 +16,64 @@ ## along with this program; If not, see <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {@var{flag} = } isrgb (@var{A}) -## Returns true if parameter is a RGB image. +## @deftypefn {Function File} {@var{bool} = } isrgb (@var{img}) +## Return true if @var{img} is a RGB image. ## -## @code{flag=isrgb(A)} returns 1 if @var{A} is a RGB image and 0 if -## not. -## -## To the decide @code{isrgb} uses the follow algorithm: +## A variable is considereed to be a RGB image if it is 3-dimensional, +## non-sparse matrix, of size MxNx3 and: ## @itemize @bullet -## @item -## If @var{A} is of class double then it checks if all values are -## between 0 and 1, and if size is m-by-n-by-3. -## @item -## If @var{A} is of class uint16, uint8 or logical then it checks is m-by-n-by-3. +## @item is of class double and all values are in the range [0, 1]; +## @item is of class uint8 or uint16. ## @end itemize ## -## @strong{Compatibility notes:} +## Note that RGB time-series image have 4 dimensions (NxMx3xtime) but +## isrgb will still return false. ## -## Information needed on whether MATLAB accepts logical arrays as RGB -## images (now this functions accepts them if they are m-by-n-by-3 arrays. -## +## @seealso{isbw, isgray, isind} ## @end deftypefn -## TODO: Check if logical arrays should be considered RGB +function bool = isrgb (img) -## Author: Josep Mones i Teixidor <jm...@pu...> + if (nargin != 1) + print_usage; + endif -function flag = isrgb(A) - if (nargin!=1) - usage("flag=isrgb(A)"); + bool = false; + if (ismatrix (img) && ndims (img) == 3 && size (img, 3) == 3 && !issparse (img)) + switch (class (img)) + case "double" + ## to speed this up, we can look at a sample of the image first + bool = is_rgb_double (img(1:ceil (rows (img) /100), 1:ceil (columns (img) /100))); + if (bool) + ## sample was true, we better make sure it's real + bool = is_rgb_double (img); + endif + case {"uint8", "uint16"} + bool = true; + endswitch endif - - if(ismatrix(A)) - flag=1; - s=size(A); - if(length(s)!=3 || s(3)!=3) - flag=0; ## false if not m-by-n-by-3 - elseif(strcmp(typeinfo(A),"matrix") && (any(A(:)<0) || any(A(:)>1))) - flag=0; ## false if class double but items are <0 or >1 - endif - else - flag=0; - endif + endfunction +function bool = is_rgb_double (img) + bool = all ((img(:) >= 0 & img(:) <= 1) | isnan (img(:))); +endfunction %!demo %! isrgb(rand(1,2,3)) %! # A 1-by-2-by-3 double matrix with elements between 0 and 1 is a RGB image. - %!# Non-matrix -%!assert(isrgb("this is not a RGB image"),0); +%!assert(isrgb("this is not a RGB image"),false); %!# Double matrix tests -%!assert(isrgb(rand(5,5)),0); -%!assert(isrgb(rand(5,5,1,5)),0); -%!assert(isrgb(rand(5,5,3,5)),0); -%!assert(isrgb(rand(5,5,3)),1); -%!assert(isrgb(ones(5,5,3)),1); -%!assert(isrgb(ones(5,5,3)+.0001),0); -%!assert(isrgb(zeros(5,5,3)-.0001),0); +%!assert(isrgb(rand(5,5)),false); +%!assert(isrgb(rand(5,5,1,5)),false); +%!assert(isrgb(rand(5,5,3,5)),false); +%!assert(isrgb(rand(5,5,3)),true); +%!assert(isrgb(ones(5,5,3)),true); +%!assert(isrgb(ones(5,5,3)+.0001),false); +%!assert(isrgb(zeros(5,5,3)-.0001),false); -%!# Logical -%!assert(isrgb(logical(round(rand(5,5,3)))),1); +%!# Logical -- checked in matlab, should return false +%!assert(isrgb(logical(round(rand(5,5,3)))),false); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <car...@us...> - 2012-09-02 03:34:02
|
Revision: 10948 http://octave.svn.sourceforge.net/octave/?rev=10948&view=rev Author: carandraug Date: 2012-09-02 03:33:55 +0000 (Sun, 02 Sep 2012) Log Message: ----------- isrgb: use shared private functions for checks Modified Paths: -------------- trunk/octave-forge/main/image/inst/isrgb.m Modified: trunk/octave-forge/main/image/inst/isrgb.m =================================================================== --- trunk/octave-forge/main/image/inst/isrgb.m 2012-09-02 03:32:21 UTC (rev 10947) +++ trunk/octave-forge/main/image/inst/isrgb.m 2012-09-02 03:33:55 UTC (rev 10948) @@ -39,15 +39,12 @@ endif bool = false; - if (ismatrix (img) && ndims (img) == 3 && size (img, 3) == 3 && !issparse (img) && !isempty (img)) + if (!isimage (img)) + bool = false; + elseif (ndims (img) == 3 && size (img, 3) == 3) switch (class (img)) case "double" - ## to speed this up, we can look at a sample of the image first - bool = is_rgb_double (img(1:ceil (rows (img) /100), 1:ceil (columns (img) /100))); - if (bool) - ## sample was true, we better make sure it's real - bool = is_rgb_double (img); - endif + bool = ispart (@is_rgb_double, img); case {"uint8", "uint16"} bool = true; endswitch @@ -59,10 +56,6 @@ bool = all ((img(:) >= 0 & img(:) <= 1) | isnan (img(:))); endfunction -%!demo -%! isrgb(rand(1,2,3)) -%! # A 1-by-2-by-3 double matrix with elements between 0 and 1 is a RGB image. - %!# Non-matrix %!assert(isrgb("this is not a RGB image"),false); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |