]> Creatis software - CreaPhase.git/blob - 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
1 ## Copyright (C) 2011 Santiago Reyes González <mailalcuete@gmail.com>
2 ##
3 ## This program is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU General Public License
5 ## as published by the Free Software Foundation; either version 3
6 ## of the License, or (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
12
13 ## -*- texinfo -*-
14 ## @deftypefn {Function File} @var{B} =  rgb2ycbcr(@var{A})
15 ## Perform colour convertion from RGB to YCbCr on a given image.
16 ##
17 ## The image @var{A} must be a NxMx3 image. The conversion
18 ## The convertion changes the image from the RGB color model to YCbCr e.g.
19 ## @example
20 ## imOut = rgb2ycbcr(imIn);
21 ## @end example
22 ## Currently this function only works with @samp{uint8} and will always return
23 ## an @samp{uint8} matrix.
24 ## @seealso{cmunique}
25 ## @end deftypefn
26
27 function im_out =  rgb2ycbcr(im)
28   if (nargin != 1)
29     print_usage;
30   elseif (length(size(im)) != 3 || size(im,3) != 3)
31     error("image must be NxMx3");
32   endif
33
34   im            = im2double(im);
35   im_out(:,:,1) = uint8(floor(77*im(:,:,1)    + 150*im(:,:,2) + 29*im(:,:,3)));
36   im_out(:,:,2) = uint8(floor(((-44*im(:,:,1) - 87*im(:,:,2)  + 131*im(:,:,3))/256 + 0.5)*256));
37   im_out(:,:,3) = uint8(floor(((131*im(:,:,1) - 110*im(:,:,2) - 21*im(:,:,3))/256 + 0.5)*256));
38
39 endfunction