You are on page 1of 9

c  c

 

 

EE 178 Information Theory

Computer Vision Project

Submitted to:

Mr. Noel S. Gunay

Submitted by:

Romeo D. Alterado Jr.

April 6, 2011

Mindanao State University

General Santos City


ë  

Color space conversion, specifically RGB to YUV and vice versa is basically rotating a load image
by implementing bitmap rotation through Visual C++ programming language. The image is rotated at an
angle of any value; such value is considered as an input to the application. An image is loaded from a file
and is rotated through rotation algorithm. The said algorithm is defined by an angle-sum formula and is
directly applied to the bitmap. Pixel information from the output or resulted image is taken and its
inverse rotation is computed to find its location in the source image. This prevents holes in the resulting
image. The pixel color of the source image is then obtained and set to the resulting image or output
bitmap open cv pixel parameters.




The project, ͞Color Space Conversion(RGB2YUV and YUV2RGB)͟, aims to use Visual C++
programming language, OpenCV source library and pixel accession concept in converting an image.
Specifically, it seeks to convert a load image at a given formula and access the Y channel, U channel and
V channel.

 

The project is implemented using a CCITT registered formula for conversion. Coordinates of a
pixel in the source bitmap (pImg1) is converted into its new image in the output bitmap (pImg2) by using
a formula. However, taking each pixel in this method obtain lots of discripances in the output bitmap
due to discrete integer of a pixel. The algorithm should start calculating from the output bitmap and
know its location in the input bitmap; hence, computing its conversion uses the formula:

RGB to YUV Formula

Y = (0.299*R) + (0.587*G) + (0.114*B);

U = -(0.168736*R)- (0.331264*G) + (0.5*B) + 128;

V = (0.5*R) - (0.418688*G) - (0.081312*B) + 128;

YUV to RGB Formula

Y = R * .299000 + G * .587000 + B * .114000

U = R * -.168736 + G * -.331264 + B * .500000 + 128

V = R * .500000 + G * -.418688 + B * -.081312 + 128


c 

Shown below are the codes and parameters used in the project.

IplImage *pImg2 = cvCreateImage(cvGetSize(pImg1),IPL_DEPTH_8U,3);


uchar *p, *t;
int i,j;

for (i=0; i<pImg1->height;i++){


for(j=0; j<pImg1->width; j++){

t=(uchar*)pImg1->imageData+(i*pImg1->widthStep+j*3);
p=(uchar*)pImg2->imageData+(i*pImg2->widthStep+j*3);
double Y, V, U;

int R = t[0], G = t[1], B = t[2];


Y = (0.299*R) + (0.587*G) + (0.114*B);
U = -(0.168736*R)- (0.331264*G) + (0.5*B) + 128;
V = (0.5*R) - (0.418688*G) - (0.081312*B) + 128;

int Yy,Uu,Vv;

Yy = (int) Y;
Uu = (int) U;
Vv = (int) V;

}
}

cvNamedWindow("Processed Image");
cvShowImage("Processed Image",pImg2);
UpdateData(FALSE);

int CMyReportYUVDlg::DoModal()
{

return CDialog::DoModal();
}

void CMyReportYUVDlg::OnBUTTONOpenFile() {
CFileDialog dlg(TRUE);
if(dlg.DoModal() == IDOK){
CString fName;
fName = dlg.GetFileName();

pImg1 = cvLoadImage(fName);
cvNamedWindow("Original Image");
cvShowImage("Original Image",pImg1);
UpdateData(FALSE);
}
}

void CMyReportYUVDlg::OnBUTTONConvert2() {
IplImage *pImg4 = cvCreateImage(cvGetSize(pImg1),IPL_DEPTH_8U,3);
uchar *p, *t;
int i,j;
for (i=0; i<pImg1->height;i++){
for(j=0; j<pImg1->width; j++){

t=(uchar*)pImg1->imageData+(i*pImg1->widthStep+j*3);
p=(uchar*)pImg4->imageData+(i*pImg4->widthStep+j*3);

double B, G, R;
int Y = t[0], U = t[1], V = t[2];

B = (Y) +(1.4020)*(U-128);
G = (Y) -(0.34414)*(V-128)-(0.71414)*(U-128);
R = (Y) + (1.77200)*(V-128);
int Bb,Gg,Rr;
Bb = (int) B;
Gg = (int) G;
Rr = (int) R;

p[0]= Bb;
p[1]= Gg;
p[2]= Rr;
}
}

cvNamedWindow("Processed Image");
cvShowImage("Processed Image",pImg4);
UpdateData(FALSE);

void CMyReportYUVDlg::OnOpenCVConvert2() {
IplImage *pImg5 = cvCreateImage(cvGetSize(pImg1),IPL_DEPTH_8U,3);

cvCvtColor(pImg1,pImg5,CV_YCrCb2RGB);

cvNamedWindow("Processed Image2");
cvShowImage("Processed Image2",pImg5);
UpdateData(FALSE);
}

void CMyReportYUVDlg::OnBUTTONYCHANNEL()
{
IplImage *pImg6 = cvCreateImage(cvGetSize(pImg1),IPL_DEPTH_8U,3);
uchar *p, *t;
int i,j;

for (i=0; i<pImg1->height;i++){


for(j=0; j<pImg1->width; j++){

t=(uchar*)pImg1->imageData+(i*pImg1->widthStep+j*3);
p=(uchar*)pImg6->imageData+(i*pImg6->widthStep+j*3);
float Y;

int R = t[0], G = t[1], B = t[2];


Y = (0.299*R) + (0.587*G) + (0.114*B);

int Yy;

Yy = (int) Y;

p[0]= Yy;
p[1]= Yy;
p[2]= Yy;
}
}

cvNamedWindow("Processed Image Y");


cvShowImage("Processed Image Y",pImg6);
UpdateData(FALSE);
}

void CMyReportYUVDlg::OnBUTTONVCHANNEL()
{
IplImage *pImg8 = cvCreateImage(cvGetSize(pImg1),IPL_DEPTH_8U,3);
uchar *p, *t;
int i,j;

for (i=0; i<pImg1->height;i++){


for(j=0; j<pImg1->width; j++){

t=(uchar*)pImg1->imageData+(i*pImg1->widthStep+j*3);
p=(uchar*)pImg8->imageData+(i*pImg8->widthStep+j*3);
double V;

int R = t[0], G = t[1], B = t[2];

V = (0.5*R) - (0.418688*G) - (0.081312*B) + 128;

int Vv;

Vv = (int) V;
p[0]= Vv;
p[1]= Vv;
p[2]= Vv;

}
}

cvNamedWindow("Processed Image V");


cvShowImage("Processed Image V",pImg8);
UpdateData(FALSE);

void CMyReportYUVDlg::OnBUTTONUCHANNEL()
{
IplImage *pImg7 = cvCreateImage(cvGetSize(pImg1),IPL_DEPTH_8U,3);
uchar *p, *t;
int i,j;

for (i=0; i<pImg1->height;i++){


for(j=0; j<pImg1->width; j++){

t=(uchar*)pImg1->imageData+(i*pImg1->widthStep+j*3);
p=(uchar*)pImg7->imageData+(i*pImg7->widthStep+j*3);
double U;

int R = t[0], G = t[1], B = t[2];

U = -(0.168736*R)- (0.331264*G) + (0.5*B) + 128;

int Uu;

Uu = (int) U;

p[0]= Uu;
p[1]= Uu;
p[2]= Uu;

}
}

cvNamedWindow("Processed Image U");


cvShowImage("Processed Image U",pImg7);
UpdateData(FALSE);

}


 ë   

Figure 1. Programming in Microsoft Visual C++

Figure 1. Building and Program execution

Figure 3. Original Image




Figure 3. Original Image and converted image using the Formula for RGB
to YUV colorspace.

Figure 3. Original Image and converted image using the Formula for YUV
to RGB colorspace

Figure 3. Original Image and converted image using the Formula for RGB
to YUV colorspace showing each channel, the Y, U l and V channel.
       

The basic concepts in accessing a particular pixel is given in the OpenCV library but you can use
the c++ programming language to acces the pixel of an image which can be incorporated to other
programming language for innovation particularly computer vision.

   

 This project recommends to display three channel of the YUV colorspace, then vary the value of
the Y channel, for this is where the intensity of the lightness or darkness of an image is stored then and
display the RGB image with the converted one.

    

Computer Vision is a complex endeavor but if you know how to follow basic instruction, it is
simple. Computer vision plays a significant role in imaging and video processing and has a vital
applications in technology where human used it. From simple method of accessing a particular pixel of
an image has provided important information in the works of applicable devices such as cameras and
robots. With the basic knowledge in computer languages, collaboration of computer vision and a little
interest, advancement is just an inch away.

Open CV and Visual C++ programming language is a tiresome yet enjoyable subject. For a typical
student like me who needs to have lecture and a teacher to teach on what exactly has to be done on the
project, finds difficulty on this matter. Reading the lessons and understanding it alone cracks my head,
virtually. But learning to ask, consult and ask advice from your teacher ease everything under the sun.

Computer vision has opened a door to the different kinds of colorspace, especially the YUV. In
addition, ͞basic͟ concepts about edge detection, bitmap and vector representation of colors are
emphasized.

On the road towards learning, difficulty do exists. Undertaking the information theory subject
without strong foundation in programming with C++ will give difficulties and hardship in understanding
it because it is not just a matter of understanding but knowing how to implement it makes it more
meritable. As the time being is concern, we cannot optimize the utility of the OpenCV because of time
constraints. Well, to be honest this project is not as perfect as we expected it to be. This means to say,
our knowledge is not yet enough for us to be called a programmer.

Maybe not today, but if our heart is willing we can be one of the best. A globally competitive
programmer.

You might also like