RGBファイルからさまざまなコンポーネントを抽出して確率を計算します

目的

  • RGB 3つのコンポーネントを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;
}

生成されるテーブルと統計は次のとおりです

ここに画像の説明を挿入
ここに画像の説明を挿入

元の記事を5件公開 Likes0 訪問数203

おすすめ

転載: blog.csdn.net/m0_46340275/article/details/104883961