Mentions légales du service

Skip to content
Snippets Groups Projects
Commit b9db2695 authored by Naethis's avatar Naethis
Browse files

Added more option for rgb2ycbcr and ycbcr2rgb

parent 545565ee
No related branches found
No related tags found
No related merge requests found
% Author : Hadi Amirpour % email : hadi.amirpour@gmail.com % Copyright(c) EmergIMG, % Universidade da Beira Interior % script for RGB 444 to YCbCr 444 color space conversion in n bits % Input: % 1-rgb ---> RGB image in double format % 2-n ---> number of the bits, this number should be either 8 or 10 % Output: % 1-ycbcr ---> YCbCr image in n bits % Modified by Elian Dib (elian.dib@inria.fr) on October, 18th 2018 function [ycbcr] = rgb2ycbcr(rgb,n) % Recommendation ITU-R BT.709-6 M = [ 0.212600 0.715200 0.072200 ; -0.114572 -0.385428 0.500000 ; 0.500000 -0.454153 -0.045847]; if (nargin < 2) n = 8; end ycbcr = reshape(double(rgb), [], 3) * M'; ycbcr(:,1) = (219*ycbcr(:,1)+16)*2^(n-8); %Luminance ycbcr(:,2:3) = (224*ycbcr(:,2:3) + 128)*2^(n-8); ycbcr = reshape(ycbcr, size(rgb)); if(n==8) ycbcr = uint8(ycbcr); elseif (n==10 || n==16) ycbcr = uint16(ycbcr); else print('invalid bit depth') end end % Author : Hadi Amirpour % email : hadi.amirpour@gmail.com % Copyright(c) EmergIMG, % Universidade da Beira Interior % script for RGB 444 to YCbCr 444 color space conversion in n bits % Input: % 1-rgb ---> RGB image in double format % 2-n ---> number of the bits, this number should be either 8 or 10 % Output: % 1-ycbcr ---> YCbCr image in n bits % Modified by Elian Dib (elian.dib@inria.fr) on October, 18th 2018 function [ycbcr] = rgb2ycbcr(rgb,n,fullRange,conv2int) % Recommendation ITU-R BT.709-6 M = [ 0.212600 0.715200 0.072200 ; -0.114572 -0.385428 0.500000 ; 0.500000 -0.454153 -0.045847]; if (nargin < 2) if isinteger(rgb) [~,n] = utils.precision(class(rgb)); else n = 0; end end if (nargin < 3) fullRange = true; end if (nargin < 4) if isinteger(rgb) conv2int = true; else conv2int = false; end end if isinteger(rgb) rgb = double(rgb)./double(intmax(class(rgb))); end if fullRange offsetY = 0; scaleY = 255; offsetC = 128; scaleC = 255; else offsetY = 16; scaleY = 219; offsetC = 128; scaleC = 224; end ycbcr = reshape(double(rgb), [], 3) * M'; ycbcr(:,1) = (scaleY*ycbcr(:,1) + offsetY)*2^(n-8); ycbcr(:,2:3) = (scaleC*ycbcr(:,2:3) + offsetC)*2^(n-8); ycbcr = reshape(ycbcr, size(rgb)); if conv2int if(n<=8) ycbcr = uint8(ycbcr); elseif (n<=16) ycbcr = uint16(ycbcr); elseif (n<=32) ycbcr = uint32(ycbcr); elseif (n<=64) ycbcr = uint64(ycbcr); else print('invalid bit depth') end end end
\ No newline at end of file \ No newline at end of file
......
% Created by Elian Dib (elian.dib@inria.fr) on October, 18th 2018 function [rgb] = ycbcr2rgb(ycbcr,n) % Recommendation ITU-R BT.709-6 M = [ 1.000000 0.000000 1.574800 ; 1.000000 -0.187350 -0.468130 ; 1.000000 1.855630 0.000000]; if (nargin < 2) n = 8; end sz = size(ycbcr); ycbcr = double(ycbcr); ycbcr = reshape(ycbcr, [], 3); ycbcr(:,1) = (ycbcr(:,1)-16*2^(n-8))/(219*2^(n-8)); %Luminance ycbcr(:,2:3) = (ycbcr(:,2:3)-128*2^(n-8))/(224*2^(n-8)); rgb = ycbcr * M'; rgb = reshape(rgb, sz); end % Created by Elian Dib (elian.dib@inria.fr) on October, 18th 2018 function [rgb] = ycbcr2rgb(ycbcr,n,fullRange,conv2int) % Recommendation ITU-R BT.709-6 M = [ 1.000000 0.000000 1.574800 ; 1.000000 -0.187350 -0.468130 ; 1.000000 1.855630 0.000000]; if (nargin < 2) if isinteger(ycbcr) [~,n] = utils.precision(class(ycbcr)); else n = 0; end end if (nargin < 3) fullRange = true; end if (nargin < 4) if isinteger(ycbcr) conv2int = true; else conv2int = false; end end if fullRange offsetY = 0; scaleY = 255; offsetC = 128; scaleC = 255; else offsetY = 16; scaleY = 219; offsetC = 128; scaleC = 224; end sz = size(ycbcr); ycbcr = double(ycbcr); ycbcr = reshape(ycbcr, [], 3); ycbcr(:,1) = (ycbcr(:,1) -offsetY*2^(n-8))/(scaleY*2^(n-8)); ycbcr(:,2:3) = (ycbcr(:,2:3)-offsetC*2^(n-8))/(scaleC*2^(n-8)); rgb = ycbcr * M'; rgb = reshape(rgb, sz); if conv2int rgb = (2^n-1)*rgb; if(n<=8) rgb = uint8(rgb); elseif (n<=16) rgb = uint16(rgb); elseif (n<=32) rgb = uint32(rgb); elseif (n<=64) rgb = uint64(rgb); else print('invalid bit depth') end end end
\ No newline at end of file \ No newline at end of file
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment