rgb对应的每个值 除以 255
注意:sizeof(unsigned char) 和 sizeof(float) 的大小
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
#include <fstream>
using namespace std;
int main()
{
char c;
unsigned char uchar;
int w=400,h=328;
int Y_max = w*h;
int Y_index = 0;
int UV_mark = 0, U_index=0, V_index=0;
float Y[w*h],U[w*h/4],V[w*h/4];
ifstream read;
read.open("s_l-nv12-BT601-TV_range.yuv");
while (read.get(c))
{
uchar = c;
if (Y_index<Y_max)
{
Y[Y_index] = uchar;
Y_index += 1;
}
else{
if(UV_mark % 2 == 0) { U[U_index]=uchar; U_index +=1; }
else{ V[V_index]=uchar; V_index +=1; }
UV_mark += 1;
}
}
cout << "Y范围: [" << *min_element(Y,Y+w*h) << "," << *max_element(Y,Y+w*h) << "]" << endl;
cout << "U范围: [" << *min_element(U,U+w*h/4) << "," << *max_element(U,U+w*h/4) << "]" << endl;
cout << "V范围: [" << *min_element(V,V+w*h/4) << "," << *max_element(V,V+w*h/4) << "]" << endl;
float full_U[w*h], full_V[w*h];
U_index=0; V_index=0;
for (int i=0; i<h; i++)
{
if ((i+1)%2==0)
{
for (int j=0; j<w; j++)
{
if (j%2==0)
{
full_U[i*w+j] = U[U_index];
full_U[i*w+j+1] = U[U_index];
full_U[(i-1)*w+j] = U[U_index];
full_U[(i-1)*w+j+1] = U[U_index];
U_index += 1;
full_V[i*w+j] = V[V_index];
full_V[i*w+j+1] = V[V_index];
full_V[(i-1)*w+j] = V[V_index];
full_V[(i-1)*w+j+1] = V[V_index];
V_index += 1;
}
}
}
}
float temp_fR,temp_fG,temp_fB;
int temp_iR,temp_iG,temp_iB;
unsigned char temp_ucR,temp_ucG,temp_ucB;
cout << "-----------\n";
auto *rgb_data = (unsigned char *) malloc(w*h*3);
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
temp_fR = 1.164*(Y[i*w+j]-16)+1.596*(full_V[i*w+j]-128);
temp_iR = temp_fR;
if(temp_iR>255){temp_iR=255;}
if(temp_iR<0){temp_iR=0;}
temp_ucR = temp_iR;
rgb_data[i*w*3+j*3+0] = temp_ucR;
temp_fG = 1.164*(Y[i*w+j]-16)-0.392*(full_U[i*w+j]-128)-0.812*(full_V[i*w+j]-128);
temp_iG = temp_fG;
if(temp_iG>255){temp_iG=255;}
if(temp_iG<0){temp_iG=0;}
temp_ucG = temp_iG;
rgb_data[i*w*3+j*3+1] = temp_ucG;
temp_fB = 1.164*(Y[i*w+j]-16)+2.016*(full_U[i*w+j]-128);
temp_iB = temp_fB;
if(temp_iB>255){temp_iB=255;}
if(temp_iB<0){temp_iB=0;}
temp_ucB = temp_iB;
rgb_data[i*w*3+j*3+2] = temp_ucB;
}
}
cout << int(rgb_data[0]) << ", " << int(rgb_data[1]) << ", " << int(rgb_data[2]) << endl;
cout << float(rgb_data[0]) << ", " << float(rgb_data[1]) << ", " << float(rgb_data[2]) << endl;
cout << "-----------\n";
cout << sizeof(unsigned char) << ", " << sizeof(float) << endl;
auto *rgb_preprocess = (float *) malloc(w*h*3*sizeof(float));
// 归一化
for (int i=0; i<h; i++)
{
for (int j=0; j<w; j++)
{
rgb_preprocess[i*w*3+j*3+0] = float(rgb_data[i*w*3+j*3+0]) / 255.0;
rgb_preprocess[i*w*3+j*3+1] = float(rgb_data[i*w*3+j*3+1]) / 255.0;
rgb_preprocess[i*w*3+j*3+2] = float(rgb_data[i*w*3+j*3+2]) / 255.0;
}
}
cout << rgb_preprocess[0] << ", " << rgb_preprocess[1] << ", " << rgb_preprocess[2] << endl;
stbi_image_free(rgb_data);
stbi_image_free(rgb_preprocess);
}
Y范围: [16,234]
U范围: [86,134]
V范围: [118,177]
-----------
223, 212, 179
223, 212, 179
-----------
1, 4
0.87451, 0.831373, 0.701961
手动计算
-
223/255 = 0.8745
-
212/255 = 0.8314
-
179/255 = 0.7020