图像RGB格式与YUV格式互转

  1 // rgb2yuv.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 
  6 
  7 #define Level 256   //直方图bin数 
  8 #define SIZEX 176   //image列尺寸
  9 #define SIZEY 256   //image列尺寸
 10 #define READ 0
 11 #define WRITE 1
 12 
 13 unsigned char image_in[SIZEY*SIZEX*3];
 14 unsigned char image_out[SIZEY*SIZEX*3];
 15 float Y[SIZEX*SIZEY], U[SIZEX*SIZEY], V[SIZEX*SIZEY];
 16 float Y_out[SIZEX*SIZEY];
 17 
 18 
 19 void GetFileName(char *name , char *message)
 20 {
 21     if((NULL == name) || (NULL == message))
 22     {
 23         return ;
 24     }
 25 
 26     printf("%s" , message);
 27     scanf("%s" , name);
 28 }
 29 
 30 FILE * FileOpen(char *fileName , int mode)
 31 {
 32     FILE *filePoint ;
 33     char *option = "" ;
 34 
 35     if(mode == READ)
 36     {
 37         option = "r+b";
 38     }
 39     else if(mode == WRITE)
 40     {
 41         option = "w+b";
 42     }
 43 
 44     if(NULL == (filePoint = fopen(fileName , option)))
 45     {
 46         printf("File Open Fail!
");
 47     }
 48 
 49     return filePoint ;
 50 }
 51 
 52 void ReadImageData(FILE *filePoint)
 53 {
 54     if(filePoint == NULL)
 55     {
 56         return ;
 57     }
 58 
 59     int y_count ;
 60     unsigned char temp = 0 ;
 61 
 62     for(y_count = 0 ; y_count < SIZEX*SIZEY ; y_count++)
 63     {
 64         fscanf(filePoint , "%c" , &temp);
 65         image_in[y_count] = temp ;
 66     }
 67 }
 68 
 69 void Histogram(long *hist)
 70 {
 71     int i , num ;
 72 
 73     for(num = 0 ; num < Level ; num++)
 74     {
 75         hist[num] = 0 ;
 76     }
 77 
 78     for(i = 0 ; i < SIZEX*SIZEY ; i++)
 79     {
 80         num = Y[i];
 81         hist[num]++;
 82     }
 83     
 84     for(num = 0 ; num < Level ; num++)
 85     {
 86         printf("%d 
" , hist[num]);
 87     }
 88 }
 89 
 90 void Equalization(long *hist ,long *q)
 91 {
 92     int i,n;
 93     long sum=0;
 94 
 95     for(i=0;i<256;i++)
 96     {
 97         sum += hist[i];   /* find distribution */
 98         q[i]=(int) (sum*Level/((float)SIZEY*SIZEX));
 99     }
100 }
101 
102 void Calculate(long *q)
103 {
104      int i, y, data;
105    
106       for(i=0;i<SIZEX*SIZEY;i++){
107             data = Y[SIZEX*SIZEY];
108             Y_out[SIZEX*SIZEY] = q[data];
109          }
110 }
111 
112 void WriteImageData(FILE *file_p)
113 {
114     int Y_Count;
115   
116     for(Y_Count = 0; Y_Count < SIZEX*SIZEY*3; Y_Count++)
117     {
118              fprintf(file_p, "%c", image_out[Y_Count]);
119     }
120 }
121 
122 int _tmain(int argc, _TCHAR* argv[])
123 {
124     FILE *in , *out;
125 
126     char source[20],destin[20];
127     char *message1="Input  Data File Name : " ;
128     char *message2="Output Data File Name : " ;
129 
130     int i ;
131     long hist[Level] = {0} ; 
132     long q[Level]    = {0} ;
133     unsigned char Red[SIZEX*SIZEY] = {0} ;
134     unsigned char Green[SIZEX*SIZEY] = {0} ;
135     unsigned char Blue[SIZEX*SIZEY] = {0} ;
136 
137     /*读取源文件名*/
138     GetFileName(source , message1);
139     GetFileName(destin , message2);
140 
141     /*打开源文件/目的文件*/
142     in  = FileOpen(source , READ);
143     out = FileOpen(destin , WRITE);
144 
145     /*读取源文件数据*/
146     ReadImageData(in);
147 
148     /*RGB2YUV*/
149     for(i = 0 ; i < SIZEX*SIZEY; i++)
150     {
151         /*Get RGB Data*/
152         Red[i]   = image_in[i*3] ; 
153         Green[i] = image_in[i*3+1] ; 
154         Blue[i]  = image_in[i*3+2] ; 
155 
156         /*RGB to YUV*/
157         Y[i] = 0.3*Red[i] + 0.59*Green[i] + 0.11*Blue[i] ;
158         U[i] = (Blue[i]-Y[i]) * 0.493;
159         V[i] = (Red[i]-Y[i]) * 0.877;
160     }
161 
162     /*直方图信息统计*/
163     Histogram(hist);
164 
165     /*直方图均衡*/
166     Equalization(hist , q);
167 
168     /*直方图计算*/
169     Calculate(q); 
170 
171     for(i=0; i < SIZEX*SIZEY; i++)
172     { 
173         Red[i] = Y_out[i] + 0.956*U[i] + 0.621*V[i];
174         Green[i] = Y_out[i] + 0.272*U[i] + 0.647*V[i];
175         Blue[i] = Y_out[i] + 1.1061*U[i] + 1.703*V[i];
176     }
177 
178     for(i=0; i < SIZEY*SIZEX; i++)
179     {   
180 
181         image_out[i*3] = Red[i];
182         image_out[i*3+1] = Green[i];
183         image_out[i*3+2] = Blue[i];
184     }
185         
186     WriteImageData(out);
187     fcloseall();
188 
189     return 0;
190 }

示例图片:

sample.jpg:                     UV分量:  

  

原文地址:https://www.cnblogs.com/qiqibaby/p/5264036.html