matlab中有自帶的rgb轉ycbcr函數,但是根據觀測,其Y的值為[16 235],不符合我們的要求,所以,提供另一種規范下的轉換腳本函數,其Y的值滿足[0 255]
RGB轉YUV
% function yuv = myrgb2yuv(image) % input params. % image: input color image with 3 channels, which value must be [0 255] % output % yuv: 3 channels(YUV444, Y plane, U plane, V plane), value [0 255], double % % % Author: KevenLee % Contact: hudalikm@163.com % Version: V1.0 function yuv = myrgb2yuv(image) image = double(image); R = image(:,:,1); G = image(:,:,2); B = image(:,:,3); yuv(:,:,1) = 0.299.*R + 0.587.*G + 0.114.*B; yuv(:,:,2) = - 0.1687.*R - 0.3313.*G + 0.5.*B + 128; yuv(:,:,3) = 0.5.*R - 0.4187.*G - 0.0813.*B + 128; end
YUV轉RGB
% function rgb = myyuv2rgb(image) % input params. % image: input YUV image with YUV444 format, which value must be [0 255] % output % rgb: 3 channels color image, value [0 255], double % % % Author: KevenLee % Contact: hudalikm@163.com % Version: V1.0 function rgb = myyuv2rgb(image) image = double(image); Y = image(:,:,1); U = image(:,:,2); V = image(:,:,3); R = Y + 1.402.*(V-128); G = Y - 0.34414.*(U-128) - 0.71414.*(V-128); B = Y + 1.772.*(U-128); rgb(:,:,1) = R; rgb(:,:,2) = G; rgb(:,:,3) = B; end
