# Magnification of a colored image through two color space: RGB & YCbCr

I have tried to magnify a colored image using bicubic method. I did this in two ways; First, I resize R, G, and B parts of it. Second, I transformed RGB to YCbCr color space and then resized Y, Cb, and Cr parts of it. When I use YCbCr color space, final image changes in color but not in whole shape of the image. I could not realize the reason. May you help me?

code:

clear all
clc

R=A(:,:,1);
G=A(:,:,2);
B=A(:,:,3);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Y  = 0.257.*R + 0.504.*G + 0.098.*B + 16;
Cb = -0.148.*R - 0.291.*G + 0.439.*B + 128;
Cr = 0.439.*R - 0.368.*G - 0.071.*B + 128;

Y1  = imresize(Y, 2, 'bicubic');
Cb1 = imresize(Cb, 2, 'bicubic');
Cr1 = imresize(Cr, 2, 'bicubic');

R1 = 1.164.*(Y1-16) + 1.596.*(Cr1-128);
G1 = 1.164.*(Y1-16) - 0.813.*(Cr1-128) - 0.391.*(Cb1-128);
B1 = 1.164.*(Y1-16) + 2.018.*(Cb1-128);

A1 = cat(3,R1,G1,B1);

imwrite(A1,'Sun1.jpg');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
R2=imresize(R, 2, 'bicubic');
G2=imresize(G, 2, 'bicubic');
B2=imresize(B, 2, 'bicubic');

A2 = cat(3,R2,G2,B2);

imwrite(A2,'Sun2.jpg');


Original image:

Resized image(YCbCr color space):

• Eventhough a slight hue shift might be expected during a resizing of RGB or YCbCr images, which due to various reasons such as formula approximation or computational precision, the images you posted shows more drift then is expected, indicating a formula or implementation error. Oct 30, 2016 at 9:24
• I checked the formulas in several references. The code is very simple. I have used the resize function of MatLab with a specified method(bicubic). Also, I have tested other methods(such as bilinear and nearest); but the result are not different. I don't really know where this problem comes from? Oct 30, 2016 at 9:33

I have checked your code and it worked fine in my matab 6.0 implementation with the following lines of of modification:

A=double(imread('Sun.jpg'));  % convert uint8 data to double
...                           % further operations here
A1(A1(:)<0.0) = 0.0;          % check and correct out of range values
A1(A1(:)>255.0) = 255.0;      % check and correct out of range values
imwrite(A1/255.0,'Sun1.jpg'); % while writing the data of class double use [0,1] data range


According to this, as I said you must deal with numerical issues such as an out of range value.

• You are right. The drawback originates of numerical issues. I used rgb2ycbcr() and ycbcr2rgb() functions of MatLab instead of manual transformation and it passed away. Thanks! Oct 30, 2016 at 12:46
• That's good to now! Then you may select the answer as accepted and upvote it, so that the question can be concluded. Oct 30, 2016 at 13:39