/** 背景のデータ */ /** * コンストラクタ **/ public KiruColleClient(string serialPort, int baudRate, string id) { sensor = new SensorManager(serialPort, baudRate); du = new DataUploader(); this.id = id; du.setLocale(id); }
/** 画像に含まれる平均的な色を取得するメソッド * 閾値を設定することができる * @param src 調査したい画像 * @param threshold 閾値 * @param du データアップロード用クラス * @return 平均的な色に染めた画像 */ public Color PickColor(Bitmap src, double threshold, DataUploader du) { int r = 0; int g = 0; int b = 0; int count = 0; // 全ての画素に対して for (int y = 0; y < bg.Height; y++) { for (int x = 0; x < bg.Width; x++) { // 画像をグレースケールに double src_gray = (src.GetPixel(x, y).R + src.GetPixel(x, y).G + src.GetPixel(x, y).B) / 3.0f; // 採用する色 if (threshold < src_gray) { r += src.GetPixel(x, y).R; g += src.GetPixel(x, y).G; b += src.GetPixel(x, y).B; count++; // src.SetPixel(x, y, Color.FromArgb((int)src_gray, (int)src_gray, (int)src_gray)); } } } if (count != 0) { r /= count; g /= count; b /= count; } else { // Console.WriteLine((int)r + "," + (int)g + "," + (int)b); useData = false; Console.WriteLine("Error"); return Color.FromArgb(0, 0, 0); } // 平均的な色 // Console.WriteLine(r + "," + g + "," + b); useData = true; du.setColorData(r,g,b); return Color.FromArgb(r,g,b); }