对课程中的down.rgb和down.yuv分析三个通道的概率分布,并计算各自的熵。(编程实现)
个文件的分辨率均为256*256,yuv为4:2:0采样空间,存储格式为:rgb文件按每个像素BGR分量依次存放;YUV格式按照全部像素的Y数据块、U数据块和V数据块依次存放。
对于down.rgb及分配率,数据量为0-256*256
int main(void)
{
//定义指针、分量数组、读入数据输出数据
FILE *picture_rgb=NULL;
FILE *R=NULL;
FILE *G=NULL;
FILE *B=NULL;
//定义各分量数组
unsigned char *RGB_buffer = NULL;
RGB_buffer=(unsigned char *)malloc(sizeof(unsigned char)*3*width*height);
unsigned char Rr[width*height]={0};
unsigned char Gg[width*height]={0};
unsigned char Bb[width*height]={0};
int data1;
data1=fopen_s(&picture_rgb,"C:\\Users\\16203\\Desktop\\down.rgb","rb"); //open down.rgb
fopen_s(&R,"C:\\Users\\16203\\Desktop\\R_out.txt","w");
fopen_s(&G,"C:\\Users\\16203\\Desktop\\G_out.txt","w");
fopen_s(&B,"C:\\Users\\16203\\Desktop\\B_out.txt","w");
fread(RGB_buffer,sizeof(unsigned char),3*width*height,picture_rgb);
//读取文件值:
int a=0;
int r=0;
int g=0;
int b=0;
for(a;a<3*width*height;a++)
{
if(a%3==0)
{
Bb[b]=*(RGB_buffer+a);
b++;
}
else if(a%3==1)
{
Gg[g]=*(RGB_buffer+a);
g++;
}
else if(a%3==2)
{
Rr[r]=*(RGB_buffer+a);
r++;
}
}
//计算分量熵值
double H_R=0;
double H_G=0;
double H_B=0;
for(int i=0;i<256;i++)
{
if(Rre[i]!=0)
{
H_R=H_R-Rre[i]*(log(Rre[i])/log(double(2)));
}
//以2为底对数熵公式
if(Gre[i]!=0)
{
H_G=H_G-Gre[i]*(log(Gre[i])/log(double(2)));
}
if(Bre[i]!=0)
{
H_B=H_B-Bre[i]*(log(Bre[i])/log(double(2)));
}
}
printf("熵R=%f\n",H_R);
printf("熵G=%f\n",H_G);
printf("熵B=%f\n",H_B);
YUV文件
//定义指针、分量数组、读入数据输出数据
int main(void)
{
FILE *picture_yuv=NULL;
FILE *Y;
FILE *U;
FILE *V;
//定义各分量数组
unsigned char *YUV_buffer = (unsigned char *)malloc(sizeof(unsigned char) *1.5*width*height);
unsigned char Yy[width*height]={0};
unsigned char Uu[width*height/4]={0};
unsigned char Vv[width*height/4]={0};
fopen_s(&picture_yuv,"down.yuv","rb");
fopen_s(&Y,"Y_out.txt","w");
fopen_s(&U,"U_out.txt","w");
fopen_s(&V,"V_out.txt","w");
fread(YUV_buffer,sizeof(unsigned char),1.5*width*height,picture_yuv);
//读取文件值
int y=0;
int u=0;
int v=0;
for(int a=0,b=0;a<width*height;a++,b++)
{
Yy[b]=YUV_buffer[a];
}
for(int a=width*height, b=0;a<width*height*1.25;a++,b++)
{
Uu[a]=YUV_buffer[a];
}
for(int a=width*height*1.25, b=0;a<width*height*1.5;a++,b++)
{
Vv[b]=YUV_buffer[a];
}
//计算分量熵值
double H_Y=0;
double H_U=0;
double H_V=0;
for(int i=0;i<256;i++)
{
if(Yre[i]!=0)
{
H_Y=H_Y-Yre[i]*(log(Yre[i])/log(double(2)));
}
if(Ure[i]!=0)
{
H_U=H_U-Ure[i]*(log(Ure[i])/log(double(2)));
}
if(Vre[i]!=0)
{
H_V=H_V-Vre[i]*(log(Vre[i])/log(double(2)));
}
}
printf("熵Y=%f\n",H_Y);
printf("熵U=%f\n",H_U);
printf("熵V=%f\n",H_V);
结果得:
熵R=7.2 bit/symbol
熵G=7.2 bit/symbol
熵B=6.9 bit/symbol
熵Y=6.3 bit/symbol
熵U=5.1 bit/symbol
熵V=4.1 bit/symbol