1 ## Copyright (C) 2011 Santiago Reyes González <mailalcuete@gmail.com>
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.
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.
14 ## @deftypefn {Function File} @var{B} = rgb2ycbcr(@var{A})
15 ## Perform colour convertion from RGB to YCbCr on a given image.
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.
20 ## imOut = rgb2ycbcr(imIn);
22 ## Currently this function only works with @samp{uint8} and will always return
23 ## an @samp{uint8} matrix.
27 function im_out = rgb2ycbcr(im)
30 elseif (length(size(im)) != 3 || size(im,3) != 3)
31 error("image must be NxMx3");
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));