Mercurial > hg > octave-image
changeset 509:7e71f51a2df7
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
author | carandraug |
---|---|
date | Mon, 05 Dec 2011 04:20:50 +0000 |
parents | b5a03a336f63 |
children | 7d8494a6d8a6 |
files | inst/isrgb.m |
diffstat | 1 files changed, 44 insertions(+), 45 deletions(-) [+] |
line wrap: on
line diff
--- a/inst/isrgb.m +++ b/inst/isrgb.m @@ -1,8 +1,10 @@ -## Copyright (C) 2004 Josep Mones i Teixidor +## Copyright (C) 2000 Kai Habel <kai.habel@gmx.de> +## Copyright (C) 2004 Josep Mones i Teixidor <jmones@puntbarra.com> +## 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 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. -## -## @code{flag=isrgb(A)} returns 1 if @var{A} is a RGB image and 0 if -## not. +## @deftypefn {Function File} {@var{bool} = } isrgb (@var{img}) +## Return true if @var{img} is a RGB image. ## -## 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 - -## Author: Josep Mones i Teixidor <jmones@puntbarra.com> +function bool = isrgb (img) -function flag = isrgb(A) - if (nargin!=1) - usage("flag=isrgb(A)"); + if (nargin != 1) + print_usage; 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; + + 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 + 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);