Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
67 views8 pages

Fahad Ali Khan Ivc

This document contains code for converting images from RGB to YCbCr color space and saving them in different chroma subsampling formats like 4:2:2 and 4:2:0. It includes a block diagram and flowchart to represent the process. The code opens an input RGB image, reads the pixel values, converts to YCbCr and writes the output in 4:2:2 or 4:2:0 format to a file. It also contains code to read multiple images and convert them to YCbCr 4:2:0 format and save in a single output file.

Uploaded by

Fahad Ali Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views8 pages

Fahad Ali Khan Ivc

This document contains code for converting images from RGB to YCbCr color space and saving them in different chroma subsampling formats like 4:2:2 and 4:2:0. It includes a block diagram and flowchart to represent the process. The code opens an input RGB image, reads the pixel values, converts to YCbCr and writes the output in 4:2:2 or 4:2:0 format to a file. It also contains code to read multiple images and convert them to YCbCr 4:2:0 format and save in a single output file.

Uploaded by

Fahad Ali Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

IVC

Submitted To:
DR
NASRUMINALLAH

Prepared By:
Fahad Ali khan.





UNI VERSI TY OF ENGI NEERI NG
AND TECHNOLOGY
PESHAWAR



PART A.



RGB
IMAGE
YCbCr
4:2:2
YCbCr
4:2:0
YCbCr


BLOCK DIAGRAM REPRESENTATION

FILE OPEN FILE OPEN
FILE WRITE
CONVERT
RGB TO
YCbCr
YCbCr
4:2:2
FAILED TO
OPEN
NO
YES
YCbCr
4:2:0
FAILED TO
OPEN
NO
FOR WRITING A FILE
FOR READING A FILE
FLOW CHART REPRESENTATION










CODE.


#include <stdio.h>
#include <conio.h>
#include "stdafx.h"
#include <iomanip>
#include <iostream>


#define wid 352
#define Hgt 240

using namespace std;

int main()
{

FILE *fr;
FILE *fw;

fr=fopen("tennis.rgb","rb");
if(fr==NULL)
{
printf("FILE NOT OPENING\n");
return 0;
}
else{
printf("tennis.rgb OPEN SUCCESSFULLY\n");
}


fw=fopen("tennis2.yuv","wb");
if(fw==NULL)
{
printf("FILE NOT OPENING\n");
return 0;
}


unsigned char red[hgt][wid];
unsigned char green[hgt][wid];
unsigned char blue[hgt][wid];

unsigned char Y[hgt][wid];
unsigned char Cb[hgt][wid];
unsigned char Cr[hgt][wid];


// For 4:2:2
unsigned char Cb2[hgt/2][wid/2];
unsigned char Cr2[hgt/2][wid/2];

for(int frame=0; frame<10; frame++){

fread(red, hgt*wid,sizeof(unsigned char),fr);
fread(green, hgt*wid,sizeof(unsigned char),fr);
fread(blue, hgt*wid,sizeof(unsigned char),fr);

// RGB to YUV
for(int i=0; i<Hhgt; i++){
for(int j=0; j<wid; j++){

Y[i][j] = 0.299*red[i][j] + 0.587*green[i][j] +
0.114*blue[i][j];
Cb[i][j] = 0.564*(blue[i][j] - Y[i][j]);
Cr[i][j] = 0.713*(red[i][j] - Y[i][j]);
}
}

// For 4:2:0
for(int i=0; i<hgt/2; i++){
for(int j=0; j<wid/2; j++){
Cb2[i][j] = Cb[i*2][j*2];
Cr2[i][j] = Cr[i*2][j*2];
}
}

fwrite(Y, hgt*wid, sizeof(unsigned char),fw);
fwrite(Cb2, hgt*wid>>2, sizeof(unsigned char),fw);
fwrite(Cr2, hgt*wid>>2, sizeof(unsigned char),fw);
}

fclose(fr);
fclose(fp);

return 0;


OUTPUT


















PART B.

CODE



#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <string>
#define ROWS 360
#define COLUMNS 240

using namespace std;

int main()
{

FILE *fr;
FILE *fw;

fw=fopen("mom.yuv","wb");

char *s[] = {
"mom/mom.010.ppm",
"mom/mom.011.ppm",
"mom/mom.012.ppm",
"mom/mom.013.ppm",
"mom/mom.014.ppm",
"mom/mom.015.ppm",
"mom/mom.016.ppm",
"mom/mom.017.ppm",
"mom/mom.018.ppm",
"mom/mom.019.ppm",
"mom/mom.020.ppm",
"mom/mom.021.ppm",
"mom/mom.022.ppm",
"mom/mom.023.ppm",
"mom/mom.024.ppm",
"mom/mom.025.ppm"};

int num_pics = sizeof(s)/sizeof(s[0]);

for(int pic=0; pic<num_pics; pic++){

unsigned char red[1];
unsigned char green[1];
unsigned char blue[1];

unsigned char Y[COLUMNS*ROWS];
unsigned char Cb[COLUMNS*ROWS];
unsigned char Cr[COLUMNS*ROWS];

unsigned char Cb2[COLUMNS/2][ROWS/2];
unsigned char Cr2[COLUMNS/2][ROWS/2];

fr=fopen(s[pic],"rb");

int j=0;

for(int i=15; i<COLUMNS*ROWS; i++){
fread(red, 1, sizeof(unsigned char), fr);
fread(green, 1, sizeof(unsigned char), fr);
fread(blue, 1, sizeof(unsigned char), fr);

Y[j] = 0.299*red[0] + 0.587*green[0] +
0.114*blue[0];
Cb[j] = 0.564*(blue[0] - Y[j]);
Cr[j] = 0.713*(red[0] - Y[j]);

j++;
}

//setting format for 4:2:0
for(int i=0; i<COLUMNS/2; i++){
for(int k=0; k<ROWS/2; k++){
Cb2[i][k] = Cb[(i*2*ROWS)+(k*2)];
Cr2[i][k] = Cr[(i*2*ROWS)+(k*2)];
}
}
fwrite(Y, COLUMNS*ROWS, sizeof(unsigned
char),fw);
fwrite(Cb2, COLUMNS*ROWS>>2, sizeof(unsigned
char),fw);
fwrite(Cr2, COLUMNS*ROWS>>2, sizeof(unsigned
char),fw);
}
cout<<"Completed";
getch();

return 0;
}

You might also like