目的
- 从rgb文件中提取rgb三个分量
- 计算他们的出现频率和分量的熵
- 将上述内容输出成csv文件
- 画图
代码如下
#include"stdafx.h"
#include<fstream>
#include<iostream>
#include<cmath>
using namespace std;
const char*Path_in = "down.rgb";
const char*Path_out = "rgb.csv";
unsigned int length = 0;
int times[3][256];
int main()
{
ifstream in(Path_in, ios::binary);
if (!in.is_open())
{
cout << "failed" << endl;
return 0;
}
while (!in.eof())
{
unsigned char r, g, b;
unsigned int pos = 0;
int n = 0;
in.seekg(0, ios::end);
int size = int(in.tellg());
in.seekg(0, ios::beg);
unsigned char*buffer = new unsigned char[size];
in.read((char*)buffer, size);
circulation:for (auto i : { &b,&g,&r })
{
*i = buffer[pos++];
times[2 - n][*i]++;
n++;
if (int(pos) > size) { length++; goto cl; }
if (i == &r)
{
length++;
n= 0;
goto circulation;
}
}
}
cl:in.close();
ofstream out(Path_out, ios::binary);
if (!out.is_open())
{
cout << "failed" << endl;
return 0;
}
out << "value,r,g,b" << endl;
for (int j = 0; j < 256; ++j)
{
out << j << ",";
for (int i = 0; i < 3; i++)
{
double p = 1.0*times[i][j] / length;
out << p << (i == 2 ? "\n" : ",");
}
}
out.close();
system("pause");
return 0;
}
生成的表格和统计图如下