Advertisement
Guest User

rgb2ycbcr

a guest
Mar 30th, 2019
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.85 KB | None | 0 0
  1. ## Copyright (C) 2013 Carnë Draug <carandraug+dev@gmail.com>
  2. ##
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 3 of the License, or
  6. ## (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. ## You should have received a copy of the GNU General Public License
  14. ## along with this program; if not, see <http://www.gnu.org/licenses/>.
  15.  
  16. function out = rgb2ycbcr (in, standard)
  17.  
  18.   img = false; # was input an image?
  19.   if (iscolormap (in))
  20.     ## do nothing, it's a colormap
  21.   elseif (isrgb (in))
  22.     img = true;
  23.     ## we shape it as a colormap (2D matrix) so we can use matrix multiplcation
  24.     nRows = rows (in);
  25.     nCols = columns (in);
  26.     in    = reshape (in, [nRows*nCols 3]);
  27.   else
  28.     error ("input must be a colormap (Nx3) or RGB image (NxMx3)");
  29.   endif
  30.  
  31.   if (ischar (standard))
  32.     if (strcmpi (standard, "601")) # for ITU-R BT.601
  33.       Kb = 0.114;
  34.       Kr = 0.299;
  35.     elseif (strcmpi (standard, "709")) # for ITU-R BT.709
  36.       Kb = 0.0722;
  37.       Kr = 0.2126;
  38.     elseif (strcmpi (standard, "2020")) # for ITU-R BT.2020
  39.       Kb = 0.0593;
  40.       Kr = 0.2627;
  41.     else
  42.       error ("unknown standard `%s'", standard);
  43.     endif
  44.   elseif (isnumeric (standard) && numel (standard) == 2)
  45.     Kb = standard(1);
  46.     Kr = standard(2);
  47.   else
  48.     error ("must specify a standard (string), or Kb and Kr values");
  49.   endif
  50.  
  51.   ## the color matrix for the conversion. Derived from:
  52.   ##    Y  = Kr*R + (1-Kr-Kb)*G + kb*B
  53.   ##    Cb = (1/2) * ((B-Y)/(1-Kb))
  54.   ##    Cr = (1/2) * ((R-Y)/(1-Kr))
  55.   ## It expects RGB values in the range [0 1], and returns Y in the
  56.   ## range [0 1], and Cb and Cr in the range [-0.5 0.5]
  57.   cmat = [  Kr            (1-Kr-Kb)            Kb
  58.           -(Kr/(2-2*Kb)) -(1-Kr-Kb)/(2-2*Kb)   0.5
  59.             0.5          -(1-Kr-Kb)/(2-2*Kr) -(Kb/(2-2*Kr)) ];
  60.  
  61.   cls = class (in);
  62.   if (! isfloat (in))
  63.     in  = im2double (in);
  64.   endif
  65.  
  66.   ## note that these blocks are the inverse of one another. Changes
  67.   ## in one will most likely require a change on the other
  68.   ## convert to YCbCr colorspace
  69.   out = in * cmat';
  70.   ## rescale Cb and Cr to range [0 1]
  71.   out(:, [2 3]) += 0.5;
  72.  
  73.   switch (cls)
  74.     case {"single", "double"}
  75.       ## do nothing. All is good
  76.     case "uint8"
  77.       out = im2uint8 (out);
  78.     case "uint16"
  79.       out = im2uint16 (out);
  80.     otherwise
  81.       error ("unsupported image class %s", cls);
  82.   endswitch
  83.  
  84.   if (img)
  85.     ## put the image back together
  86.     out = reshape (out, [nRows nCols 3]);
  87.   endif
  88.  
  89. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement