まず、実験コード
#include <iostream>
#include <fstream>
using namespace std;
#define Height 256
#define Width 256
#define Size 256
double Count_Entropy(double* freq) {
double entropy=0;
for (int i = 0; i < Size; i++)
{
if ((*(freq + i)) != 0) {
entropy += (*(freq + i)) * log2(1 / (*(freq + i)));
}
}
return entropy;
}
void Count_freq(double *freq,int *cnt) {
for (int i = 0; i < Size; i++) {
*(freq + i) = *(cnt + i) / (double)(Height * Width);
}
}
void PrintToTxt(double* freq, ofstream& File) {
for (int i = 0; i < Size; i++) {
File <<i<<"\t"<<*(freq+i)<< endl;
}
}
int main(){
unsigned char Red[Height][Width] = { 0 }, Green[Height][Width] = { 0 }, Blue[Height][Width] = {};
int Red_cnt[Size] = { 0 }, Blue_cnt[Size] = { 0 }, Green_cnt[Size] = { 0 };
double Red_freq[Size] = { 0 }, Blue_freq[Size] = { 0 }, Green_freq[Size] = { 0 };
ifstream File_in;
File_in.open("C:\\File_xieyh\\FILE\\down.rgb", ios::in | ios::binary);
if (!File_in) {
cout << "Error opening down.rgb" << endl;
return 0;
}
ofstream R_sat, G_sat, B_sat;
R_sat.open("R_sat.txt", ios::out, ios::trunc);
G_sat.open("G_sat.txt", ios::out, ios::trunc);
B_sat.open("B_sat.txt", ios::out, ios::trunc);
if (!R_sat||!G_sat||!B_sat) {
cout << "Error opening R_sat.txt||G_sat.txt||B_sat.txt" << endl;
return 0;
}
R_sat << "symbol\tfreq" << endl;
G_sat << "symbol\tfreq" << endl;
B_sat << "symbol\tfreq" << endl;
for (int i = 0; i <Height; i++) {
for (int j = 0; j < Width; j++) {
File_in.read((char*)(*(Blue + i)+j), sizeof(unsigned char));
Blue_cnt[*(*(Blue + i) + j)]++;
File_in.read((char*)(*(Green + i) + j), sizeof(unsigned char));
Green_cnt[*(*(Green + i) + j)]++;
File_in.read((char*)(*(Red + i) + j), sizeof(unsigned char));
Red_cnt[*(*(Red + i) + j)]++;
}
}
Count_freq(Red_freq, Red_cnt);
Count_freq(Green_freq, Green_cnt);
Count_freq(Blue_freq, Blue_cnt);
PrintToTxt(Red_freq, R_sat);
PrintToTxt(Green_freq, G_sat);
PrintToTxt(Blue_freq, B_sat);
cout << "已输出至txt" << endl;
double Red_Entropy, Green_Entropy, Blue_Entropy;
Red_Entropy = Count_Entropy(Red_freq);
Green_Entropy = Count_Entropy(Green_freq);
Blue_Entropy = Count_Entropy(Blue_freq);
cout << "Red_Entropy=" << Red_Entropy << endl;
cout << "Green_Entropy=" << Green_Entropy << endl;
cout << "Blue_Entropy=" << Blue_Entropy << endl;
File_in.close();
R_sat.close();
G_sat.close();
B_sat.close();
return 0;
}
第二に、統計ファイル
(1)R成分
- R_sat.txt
- R成分チャート
(2)G成分
- G_sat.txt
- G成分チャート
(3)B成分
- B_sat.txt
- B成分チャート
第三に、各成分のエントロピー