RGB熵的计算
#include"stdio.h"
#include"math.h"
#include"iostream"
#include"windows.h"
using namespace std;
#define MAX 256
void main()
{
float count[MAX]={0};
float h[3]={0};
float iq;
int rgb;
int size=256256,total=256256*3;
unsigned char *RGB_buffer_in;
unsigned char *RGB;
unsigned char *buffer;
RGB_buffer_in=new unsigned char[total];
RGB=new unsigned char[total];
FILE *fp;
fp=fopen(“E:/大三下课程/数据压缩/down.rgb”,“r”);
if(fp==NULL)
{printf(“File not open!/n”);}
fread(RGB_buffer_in,sizeof(unsigned char),total,fp);
for(int i=0;i<3;i++)
{
buffer=RGB_buffer_in;
for(int j=0;j<size;j++)
{
RGB=buffer;
RGB++;
buffer+=3;
}
RGB_buffer_in++;
}
for(rgb=0;rgb<3;rgb++)
{
for(int i=0;i<size;i++)
{
int m=RGB[i];
count[m]++;
}
for(int i=0;i<MAX;i++)
{
count[i]=count[i]/size;
if(count[i]!=0)
{
iq=log(count[i])/log(2.0);
h[rgb]=h[rgb]-count[i]*iq;
}
}
}
fclose(fp);
printf(“B的熵为:%f\n”,h[0]);
printf(“G的熵为:%f\n”,h[1]);
printf(“R的熵为:%f\n”,h[2]);
system(“pause”);
return;
}
结果: R的熵为:7.22955
G的熵为:7.17846
B的熵为:6.85686
YUV熵的计算
#include<stdio.h>
#include
#include<stdio.h>
#include<math.h>
#define MAX 256
void main()
{
float count[MAX]={0};
float h[3]={0};
float iq;
int yuv;
int size=256*256;
unsigned char *YUV_buffer_in;
YUV_buffer_in=new unsigned char[size];
FILE *fp;
fp=fopen(“E:/大三下课程/数据压缩/down.yuv”,“r”);
if(fp==NULL)
{printf(“File not open!/n”);}
for(yuv=0;yuv<3;yuv++)
{
fread(YUV_buffer_in,sizeof(unsigned char),size,fp);
for(int i=0;i<size;i++)
{
int m=YUV_buffer_in[i];
count[m]++;
}
for(int i=0;i<MAX;i++)
{
count[i]=count[i]/size;
if(count[i]!=0)
{
iq=log(count[i])/log(2.0);
h[yuv]=h[yuv]-count[i]*iq;
}
}
}
fclose(fp);
printf(“Y的熵为:%f\n”,h[0]);
printf(“U的熵为:%f\n”,h[1]);
printf(“V的熵为:%f\n”,h[2]);
return ;
}
结果:Y的熵为:6.33182
U的熵为:5.1264
V的熵为:4.11314