Is there a direct function for RGB to YUV color space conversion? please help

29 views (last 30 days)
A=imshow('fig.bmp'); yuv= rgb2yuv(A): figure;imshow(yuv);

Answers (3)

Image Analyst
Image Analyst on 29 Sep 2018
You can use rgb2xyz(), then use the formula here: http://www.easyrgb.com/en/math.php

Fowzi barznji
Fowzi barznji on 14 Mar 2020
Y′UV is often used as a term for YCbCr.
  1 Comment
Fowzi barznji
Fowzi barznji on 14 Mar 2020
RGB = imread('peppers.png');
YCBCRimg = rgb2ycbcr(RGB);
figure
imshow(RGB);
title('Image in RGB Color Space');
figure
imshow(YCBCRimg);
title('Image in YCBCR Color Space');

Sign in to comment.


DGM
DGM on 3 Nov 2021
Edited: DGM on 12 May 2023
Old post, I know... Gotta put a reference answer somewhere.
Assuming that OP actually wants what they say they want, then no, YUV is not LUV or YCbCr.
The differences between the luma-chroma models may be of little consequence if all that's needed is decent brightness/color separation. If that's the case, just pick one. If there's a technical need to compare against existing data in a specific model, then maybe the differences are significant.
Existing Tools
MATLAB/IPT already has tools for YIQ (rgb2ntsc()) and YCbCr() (rgb2ycbcr()). For other luma-chroma models, you might try something like this, from the File Exchange, or you can convert it yourself using the following methods.
General RGB-Yxx Conversion
This is simply a linear transformation of the image data. All you need is the image and the transformation matrix. Post-R2016b, this can be done with imapplymatrix().
% concise method using IPT tools:
% imapplymatrix() has some undocumented behaviors
% see https://www.mathworks.com/matlabcentral/answers/778967-is-this-the-expected-behavior-of-imapplymatrix-with-explicit-output-class-parameter
% use with caution or use MIMT imappmat() instead.
rgbpict = im2double(rgbpict);
lumachromapict = imapplymatrix(A,rgbpict);
Otherwise, it can be done with basic tools.
% basic method using bsxfun() instead:
rgbpict = im2double(rgbpict);
A = permute(A,[1 3 2]);
outpict = zeros(size(rgbpict));
for c = 1:size(A,1)
lumachromapict(:,:,c) = sum(bsxfun(@times,rgbpict,A(c,:,:)),3);
end
The conversion back to RGB can be done by simply using the inverse of the transformation matrix A.
The Transformation Matrix
This is where it's easy to get lost and decide that the differences don't matter. There are different models, different applications, and different standards that have been introduced over time. I'm not going to pretend that any of these representations are exactly compliant with any particular standard, but they're generally close enough to demonstrate that (e.g.) YUV is not YCbCr or that YCbCr is not YIQ
RGB2YUV
% Rec 470/601 luma
A = [0.299 0.587 0.114; -0.14713 -0.28886 0.436; 0.615 -0.51499 -0.10001];
% Rec 709 luma
A = [0.2126 0.7152 0.0722; -0.09991 -0.33609 0.436; 0.615 -0.55861 -0.05639];
RGB2YPbPr (or YCbCr sans integer-scaling and margins)
% Rec 470/601 luma
A = [0.299 0.587 0.114; -0.1687 -0.3313 0.5; 0.5 -0.4187 -0.08131];
% Rec 709 luma
A = [0.2126 0.7152 0.0722; -0.1146 -0.3854 0.5; 0.5 -0.4542 -0.04585];
RGB2YDbDr
% Rec 470/601 luma
A = [0.299 0.587 0.114; -0.450 -0.883 1.333; -1.333 1.116 0.217];
RGB2YIQ
% Rec 470/601 luma
A = [0.299 0.587 0.114; 0.5959 -0.2746 -0.3213; 0.2115 -0.5227 0.3112];
Deriving the Matrix
You don't need to do this to do the conversion, but I think it's demonstrative. It might also be useful if more precision is desired (not that it's likely). Disregarding details like headroom/footroom and integer representations, most models reflect a common representation. The differences in the following models can be reduced to the exchange of two simple scaling factors.
% luma weights (Rec 470/601)
wr = 0.299;
wg = 0.587;
wb = 0.114;
% axis extents
% YUV, YIQ
crmx = 0.436;
cbmx = 0.615;
% YDbDr
%crmx = 1.333;
%cbmx = -1.333;
% YPbPr, YCbCr
%crmx = 0.5;
%cbmx = 0.5;
Ay = [wr wg wb]; % coefficients for Y
Ab = crmx/(1-wb)*([0 0 1]-Ay); % coefficients for U,Db,Pb
Ar = cbmx/(1-wr)*([1 0 0]-Ay); % coefficients for V,Dr,Pr
A = [Ay; Ab; Ar]
A = 3×3
0.2990 0.5870 0.1140 -0.1471 -0.2889 0.4360 0.6150 -0.5150 -0.1000
The matrix for YIQ can be calculated from the matrix for YUV like so:
Ai = -sind(33)*Ab + cosd(33)*Ar;
Aq = cosd(33)*Ab + sind(33)*Ar;
A = [Ay; Ai; Aq]
A = 3×3
0.2990 0.5870 0.1140 0.5959 -0.2746 -0.3213 0.2115 -0.5227 0.3112
Of course, I never explained where the luma weights or other factors came from. You can certainly go deeper if you want.

Products


Release

R2013a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!