RGB文件三通道分量的熵的计算

RGB文件三通道分量的熵的计算

所给的RGB文件是按照b,r,g的顺序排列,文件分辨率 256 × 256 256\times256 256×256
代码实现了以下功能:

  • 计算RGB三通道分量的熵
  • 将结果导出到.cvs文件中

为了方便理解,用Excel进行了数据可视化。

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <fstream>
#define uchar unsigned char
#define F FILE

using namespace std;

const int weight = 256;
const int height = 256;

char cvs_name[weight * height] = "C:\\Users\\sdlwq\\Desktop\\Github\\Data-Compression\\Work\\RGB\\Answer.cvs"; 
char file_name[weight * height] = "C:\\Users\\sdlwq\\Documents\\WeChat Files\\wxid_nzxxvy7wrx5q22\\FileStorage\\File\\2022-02\\test.rgb";
vector<uchar > R, G, B;
uchar RGB[weight * height * 3];
int r[weight * height], b[weight * height], g[weight * height];
double entR[weight * height], entG[weight * height], entB[weight * height];

int main()
{
    //read file
    F *file = NULL;
    file = fopen (file_name, "rb");
    if (file == NULL) {
        printf ("Failed To Open!\n");
        return 0;
    }
    else printf ("Successfully Opened\n");
    //statistics RGB
    fread (RGB, sizeof(uchar), 3 * weight * height, file);
    //查阅资料知,RGB的存储方式在计算机中表现为BGR
    for (int i = 0;i < 3 * weight * height;i ++) {
        if (i % 3 == 0) B.push_back(RGB[i]);
        else if (i % 3 == 1) G.push_back(RGB[i]);
        else R.push_back(RGB[i]);
    }
    //统计RGB每个取值下的数目
    int sum = weight * height;
    for (int i = 0;i < 256;i ++) {
        for (int j = 0;j < sum;j ++) {
            if (i == R[j]) r[i] ++;
            if (i == B[j]) b[i] ++;
            if (i == G[j]) g[i] ++;
        }
    }
    //求概率
    for (int i = 0;i < 256;i ++) {
        entR[i] = (1.0 * r[i]) / sum;
        entG[i] = (1.0 * g[i]) / sum;
        entB[i] = (1.0 * b[i]) / sum;
    }
    //求熵
    double entropyR = 0.0, entropyG = 0.0, entropyB = 0.0;
    for (int i = 0;i < 256;i ++) {
        if (entR[i]) entropyR -= (entR[i] * log(entR[i]) / log(2));
        if (entG[i]) entropyG -= (entG[i] * log(entG[i]) / log(2));
        if (entB[i]) entropyB -= (entB[i] * log(entB[i]) / log(2));
    }
    cout << "R = " << entropyR << ' ' << "G = " << entropyG << ' ' << "B = " << entropyB << endl; 
    //写入结果到.cvs
    ofstream oFile;
    oFile.open(cvs_name, ios :: out | ios :: trunc);
    oFile << "Value" << "," << "R" << "," << "G" << "," << "B" << endl;
    for (int i = 0;i < 256;i ++)
        oFile << i << "," << entR[i] << "," << entG[i] << "," << entB[i] << endl;
    oFile << "Class" << "," << "Entropy" << endl;
    oFile << "R" << "," << entropyR << endl;
    oFile << "G" << "," << entropyG << endl;
    oFile << "B" << "," << entropyB << endl;
    fclose(file);
    return 0;
}

得到的结果:

Class	Entropy		
R	7.22955		
G	7.17846		
B	6.85686	

使用Excel做出图像:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CUCKyrie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值