欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

分析rgb和yuv文件的三个通道的概率分布,并计算各自的熵

程序员文章站 2022-05-28 14:24:08
...

分析rgb和yuv文件的三个通道的概率分布,并计算各自的熵

rgb文件

#include<iostream>
#include<math.h>
using namespace std;

#define RES 256*256

int main()
{
    unsigned char r[RES] = {0},g[RES] = {0},b[RES] = {0};
    double Hr = 0, Hg = 0, Hb = 0;

    FILE *RGB_BUFFER;
    fopen_s(&RGB_BUFFER, "down.rgb", "rb");
    
	unsigned char a[RES * 3] = {0};
    fread(a, 1, RES * 3, RGB_BUFFER);
    
	for (int i = 0,j = 0 ; j < RES*3 ; i++, j += 3)
    {
        b[i] = a[j];
        g[i] = a[j + 1];
        r[i] = a[j + 2];
    }
	
	double r1[256] = {0},g1[256] = {0},b1[256] = {0};
    
	for (int i = 0; i < RES; i++)
    {
        r1[r[i]]++; 
        g1[g[i]]++;
        b1[b[i]]++;
    }
    for (int i = 0; i < 256; i++)
    {
        r1[i] = r1[i] / (RES);
        g1[i] = g1[i] / (RES);
        b1[i] = b1[i] / (RES);
    }
    for (int i = 0; i < 256; i++)
    {
		if (r1[i] != 0) 
		{ 
			Hr += -r1[i] * log(r1[i])/log(2*1.0);
		}
        if (g1[i] != 0) 
		{ 
			Hg += -g1[i] * log(g1[i])/log(2*1.0);
		}
        if (b1[i] != 0) 
		{
			Hb += -b1[i] * log(b1[i])/log(2*1.0);
		}
    }
    printf("H(r)=%f\nH(g)=%f\nH(b)=%f\n",Hr,Hg,Hb);
   
	getchar();
    
    return 0;
}

执行结果
分析rgb和yuv文件的三个通道的概率分布,并计算各自的熵

YUV文件

#include<iostream>
#include<math.h>
using namespace std;

#define RES 256*256

int main()
{
    unsigned char y[RES] = {0},u[RES/4] = {0},v[RES/4] = {0};
    double Hy = 0, Hu = 0, Hv = 0;

    FILE *YUV_BUFFER;
    fopen_s(&YUV_BUFFER, "down.yuv", "rb");

	unsigned char a[98304];     //RES*1.5 = 98304
    fread(a, 1, RES * 1.5, YUV_BUFFER);
    
	for (int i = 0 ; i < RES ; i++)
    {
		y[i] = a[i];
    }
	for (int i = RES ; i < RES*1.25 ; i++)
	{
		u[i - RES] = a[i];
		v[i - RES] = a[i + 16384];    //RES*0.25 = 16384
	}

	double y1[256] = {0},u1[256] = {0},v1[256] = {0};
    
	for (int i = 0; i < RES; i++)
    {
        y1[y[i]]++; 
    }

	for (int i = 0; i < RES/4; i++)
    {
        u1[u[i]]++;
        v1[v[i]]++;
    }
    
	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++)
    {
		if (y1[i] != 0) 
		{ 
			Hy += -y1[i] * log(y1[i])/log(2*1.0); 
		}
        if (u1[i] != 0) 
		{ 
			Hu += -u1[i] * log(u1[i])/log(2*1.0); 
		}
        if (v1[i] != 0) 
		{ 
			Hv += -v1[i] * log(v1[i])/log(2*1.0); 
		}
    }
    printf("H(y)=%f\nH(u)=%f\nH(v)=%f\n",Hy,Hu,Hv);
   
	getchar();
    
    return 0;
}

执行结果
分析rgb和yuv文件的三个通道的概率分布,并计算各自的熵

总结

YUV三轨道熵值都比RGB三轨道熵值低,压缩程度较高。

相关标签: 数据压缩