]> Creatis software - CreaPhase.git/blobdiff - octave_packages/image-1.0.15/rgb2ycbcr.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / rgb2ycbcr.m
diff --git a/octave_packages/image-1.0.15/rgb2ycbcr.m b/octave_packages/image-1.0.15/rgb2ycbcr.m
new file mode 100644 (file)
index 0000000..f637ada
--- /dev/null
@@ -0,0 +1,39 @@
+## Copyright (C) 2011 Santiago Reyes González <mailalcuete@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.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} @var{B} =  rgb2ycbcr(@var{A})
+## Perform colour convertion from RGB to YCbCr on a given image.
+##
+## The image @var{A} must be a NxMx3 image. The conversion
+## The convertion changes the image from the RGB color model to YCbCr e.g.
+## @example
+## imOut = rgb2ycbcr(imIn);
+## @end example
+## Currently this function only works with @samp{uint8} and will always return
+## an @samp{uint8} matrix.
+## @seealso{cmunique}
+## @end deftypefn
+
+function im_out =  rgb2ycbcr(im)
+  if (nargin != 1)
+    print_usage;
+  elseif (length(size(im)) != 3 || size(im,3) != 3)
+    error("image must be NxMx3");
+  endif
+
+  im            = im2double(im);
+  im_out(:,:,1) = uint8(floor(77*im(:,:,1)    + 150*im(:,:,2) + 29*im(:,:,3)));
+  im_out(:,:,2) = uint8(floor(((-44*im(:,:,1) - 87*im(:,:,2)  + 131*im(:,:,3))/256 + 0.5)*256));
+  im_out(:,:,3) = uint8(floor(((131*im(:,:,1) - 110*im(:,:,2) - 21*im(:,:,3))/256 + 0.5)*256));
+
+endfunction