分析rgb和yuv文件的三个通道的概率分布,并计算各自的熵(编程实现)。
注:
1. down.rgb和down.yuv两个文件的分辨率均为256*256。
2. yuv为4:2:0采样空间。
3. 存储格式:rgb文件按每个像素BGR分量依次存放;YUV格式按照全部像素的Y数据块、U数据块和V数据块依次存放。
1. 分别读取R、G、B(或Y、U、V)到数组中。
2. 分别统计R、G、B(或Y、U、V)三通道的颜色强度级的频数。
3. 别计算R、G、B(或Y、U、V)三通道的颜色强度级的概率,将概率写入文件。
4. 计算熵并输出。
代码如下:
#include
#include
using namespace std;
#define Res 256*256//分辨率
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 R2 = 0, G2 = 0, B2 = 0; //定义R、G、B的熵
FILE* Picture, * Red, * Green, * Blue;
fopen_s(&Picture, "/Users/cxr/Desktop/数据压缩/chengxu/Project1/down.rgb", "rb");
fopen_s(&Red, "/Users/cxr/Desktop/数据压缩/chengxu/Project1/Red.txt", "w");
fopen_s(&Green, "/Users/cxr/Desktop/数据压缩/chengxu/Project1/Green.txt", "w");
fopen_s(&Blue, "/Users/cxr/Desktop/数据压缩/chengxu/Project1/Blue.txt", "w");
if (Picture==0)
printf( "读取图片失败!");
else
{
//分别读取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) {
R2 += -R1[i] * log(R1[i]) / log(2); }
if (G1[i] != 0) {
G2 += -G1[i] * log(G1[i]) / log(2); }
if (B1[i] != 0) {
B2 += -B1[i] * log(B1[i]) / log(2); }
}
printf("R的熵为%f\n", R2);
printf("G的熵为%f\n", G2);
printf("B的熵为%f\n", B2);
}
return 0;
}
代码如下:
#include
#include
using namespace std;
#define Res 256*256//分辨率
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 Y2 = 0, U2 = 0, V2 = 0; //定义Y、U、V的熵
FILE* Picture, * PartY, * PartU, * PartV;
fopen_s(&Picture, "/Users/cxr/Desktop/数据压缩/chengxu/Project2/down.yuv", "rb");
fopen_s(&PartY, "/Users/cxr/Desktop/数据压缩/chengxu/Project2/PartY.txt", "w");
fopen_s(&PartU, "/Users/cxr/Desktop/数据压缩/chengxu/Project2/PartU.txt", "w");
fopen_s(&PartV, "/Users/cxr/Desktop/数据压缩/chengxu/Project2/PartV.txt", "w");
if (Picture == 0)
printf("读取图片失败!");
else
{
//分别读取Y、U、V到数组中
unsigned char Array[98304];
fread(Array, 1, Res * 1.5, Picture);
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(PartY, "%d\t%f\n", i, Y1[i]);
fprintf(PartU, "%d\t%f\n", i, U1[i]);
fprintf(PartV, "%d\t%f\n", i, V1[i]);
}
//计算并输出熵
for (int i = 0; i < 256; i++)
{
if (Y1[i] != 0) {
Y2 += -Y1[i] * log(Y1[i]) / log(2); }
if (U1[i] != 0) {
U2 += -U1[i] * log(U1[i]) / log(2); }
if (V1[i] != 0) {
V2 += -V1[i] * log(V1[i]) / log(2); }
}
printf("Y的熵为%f\n", Y2);
printf("U的熵为%f\n", U2);
printf("V的熵为%f\n", V2);
}
return 0;
}
将编程得到的txt文件导入Excel表格中,绘制图像。
分量 | R | G | B |
---|---|---|---|
熵 | 7.22955 | 7.17846 | 6.85686 |
分量 | Y | U | V |
---|---|---|---|
熵 | 6.331819 | 5.126402 | 4.113143 |
观察RGB与YUV的概率分布图,可以看出YUV分量的值更不均匀,所以它的熵更小。该结论与编程计算的熵的结果相符。