分析rgb和yuv文件的三个通道的概率分布,并计算各自的熵。(编程实现)
两个文件的分辨率均为256*256,yuv为4:2:0采样空间,
存储格式为:rgb文件按每个像素BGR分量依次存放;
YUV格式按照全部像素的Y数据块、U数据块和V数据块依次存放。
1、分别读取R、G、B(或Y、U、V)到数组中。
2、计算三通道的颜色强度级的频数。
3、计算三通道的颜色强度级的概率。
4、将三个通道分量频率写为red、green、blue三个txt文件存放。
5、频率统计结果导入Excel画图得出。
(c++很难进行绘图操作)
6、熵值由程序直接输出。
rgb部分
#include
#include
using namespace std;
#define Res 65536
int main()
{
unsigned char R[Res] = {
0 }, G[Res] = {
0 }, B[Res] = {
0 }; //定义 R、G、B分量
double R1[256] = {
0 }, G1[256] = {
0 }, B1[256] = {
0 }; //定义 R、G、B概率分量
double RS = 0, GS = 0, BS = 0; //定义 R、G、B的熵
FILE* Picture, * Red, * Green, * Blue;
fopen_s(&Picture,"D:/新建文件夹 (3)/数据压缩/rgb/down.rgb", "rb");
fopen_s(&Red,"D:/新建文件夹 (3)/数据压缩/rgb/Red.txt", "w");
fopen_s(&Green,"D:/新建文件夹 (3)/数据压缩/rgb/Green.txt", "w");
fopen_s(&Blue, "D:/新建文件夹 (3)/数据压缩/rgb/Blue.txt", "w");
//分别读取R、G、B到数组中
unsigned char Array[Res * 3] = {
0 };
fread(Array, 1, Res * 3, Picture);
for (int i = 0, j = 0; i < Res * 3; i = i + 3, j++)
{
B[j] = *(Array + i);
G[j] = *(Array + i + 1);
R[j] = *(Array + i + 2);
}
//分别统计R、G、B三通道的256个颜色强度级的频数
for (int i = 0; i < Res; i++)
{
R1[R[i]]++;
G1[G[i]]++;
B1[B[i]]++;
}
//分别计算R、G、B三通道的256个颜色强度级的概率
for (int i = 0; i < 256; i++)
{
R1[i] = R1[i] / (Res);
B1[i] = B1[i] / (Res);
G1[i] = G1[i] / (Res);
}
//将概率写入文件
for (int i = 0; i < 256; i++)
{
fprintf(Red, "%d\t%f\n", i, R1[i]);
fprintf(Green, "%d\t%f\n", i, G1[i]);
fprintf(Blue, "%d\t%f\n", i, B1[i]);
}
//计算并输出熵
for (int i = 0; i < 256; i++)
{
if (R1[i] != 0) {
RS += -R1[i] * log(R1[i]) / log(2.0); }
if (G1[i] != 0) {
GS += -G1[i] * log(G1[i]) / log(2.0); }
if (B1[i] != 0) {
BS += -B1[i] * log(B1[i]) / log(2.0); }
}
cout << "R熵:" << RS << endl;
cout << "G熵:" << GS << endl;
cout << "B熵:" << BS << endl;
system("pause");
return 0;
}
熵:R:7.22955 G:7.17846 B:6.85686
yuv部分
#include
#include
using namespace std;
#define Res 65536
int main()
{
unsigned char Y[Res] = {
0 }, U[Res/4] = {
0 }, V[Res/4] = {
0 }; //定义Y、U、V分量
double Y1[256] = {
0 }, U1[256] = {
0 }, V1[256] = {
0 }; //定义Y、U、V概率分量
double Ys = 0, Us = 0, Vs = 0; //定义Y、U、V的熵
FILE* Pic, * PY, * PU, * PV;
fopen_s(&Pic, "D:/新建文件夹 (3)/数据压缩/yuv/down.yuv", "rb");
fopen_s(&PY, "D:/新建文件夹 (3)/数据压缩/yuv/PY.txt", "w");
fopen_s(&PU, "D:/新建文件夹 (3)/数据压缩/yuv/PU.txt", "w");
fopen_s(&PV, "D:/新建文件夹 (3)/数据压缩/yuv/PV.txt", "w");
//读取Y、U、V到数组
unsigned char Array[98304];
fread(Array, 1, Res * 1.5, Pic);
for (int i = 0; i < Res ; i++)
{
Y[i] = *(Array + i);
}
for (int i = Res; i < Res * 1.25; i++)
{
U[i-65536] = *(Array + i);
}
for (int i = Res * 1.25; i < Res * 1.5; i++)
{
V[i - 81920] = *(Array + i);
}
//统计Y、U、V三通道的颜色强度级的频数
for (int i = 0; i < Res; i++)
{
Y1[Y[i]]++;
}
for (int i = 0; i < (Res/4); i++)
{
U1[U[i]]++;
V1[V[i]]++;
}
//计算Y、U、V三通道的256个颜色强度级的概率
for (int i = 0; i < 256; i++)
{
Y1[i] = Y1[i] / (Res);
U1[i] = U1[i] / (Res/4);
V1[i] = V1[i] / (Res/4);
}
for (int i = 0; i < 256; i++)
{
fprintf(PY, "%d\t%f\n", i, Y1[i]);
fprintf(PU, "%d\t%f\n", i, U1[i]);
fprintf(PV, "%d\t%f\n", i, V1[i]);
}
//计算输出熵
for (int i = 0; i < 256; i++)
{
if (Y1[i] != 0) {
Ys += -Y1[i] * log(Y1[i]) / log(2.0); }
if (U1[i] != 0) {
Us += -U1[i] * log(U1[i]) / log(2.0); }
if (V1[i] != 0) {
Vs += -V1[i] * log(V1[i]) / log(2.0); }
}
printf("Y熵:%f\n", Ys);
printf("U熵:%f\n", Us);
printf("V熵:%f\n", Vs);
return 0;
}
熵:Y:6.331819 U:5.126402 V:4.113143
RGB分量的熵大于YUV分量,去相关性更好。