/// <summary> /// 出現数パターン配列を取得 /// </summary> /// <param name="ba">パターン判定元画像のアクセサ</param> /// <param name="rect">判定に使用する範囲</param> /// <returns>出現数パターン配列</returns> private int[,,] GetPattern(RapidBitmapAccessor ba, Rectangle rect) { int[,,] patterns = new int[COLOR_DIVISION_NUM, COLOR_DIVISION_NUM, COLOR_DIVISION_NUM]; int colorRange = 256 / COLOR_DIVISION_NUM; for (int x = rect.X; x < rect.X + rect.Width; x = x + 2) { for (int y = rect.Y; y < rect.Y + rect.Height; y = y + 2) { if (MASK[x - rect.X, y - rect.Y] != 1) { // マスクされている個所は処理しない continue; } // RGBから色番号を決定 Color c = ba.GetPixel(x, y); int ri = c.R / colorRange; int gi = c.G / colorRange; int bi = c.B / colorRange; patterns[ri, gi, bi]++; } } return(patterns); }
/// <summary> /// ぷよタイプ識別用のサンプルデータを更新する。 /// </summary> /// <param name="puyoType">ぷよ種別</param> /// <param name="ba">サンプルデータ画像のアクセサ</param> /// <remarks>サンプルデータ画像のサイズは1セルのサイズとする。</remarks> public void UpdateSample(PuyoType puyoType, RapidBitmapAccessor ba) { colorSamples[puyoType] = GetPattern(ba, new Rectangle() { X = 0, Y = 0, Width = CaptureField.UNIT, Height = CaptureField.UNIT }); }
/// <summary> /// フィールドがクリックされた /// </summary> /// <param name="sender">イベント発生源</param> /// <param name="e">イベント情報</param> /// <param name="fieldNo">フィールド番号</param> private void ClickField(object sender, MouseEventArgs e, int fieldNo) { if (!sampler.IsSampling) { return; } PictureBox fieldImg = (PictureBox)sender; if (e.Button == MouseButtons.Left) { // サンプル選択 int x = pointOnFieldImg.X - (pointOnFieldImg.X % CaptureField.UNIT); int y = pointOnFieldImg.Y - (pointOnFieldImg.Y % CaptureField.UNIT); Rectangle pixelingCellRect = new Rectangle(x, y, CaptureField.UNIT, CaptureField.UNIT); using (Bitmap forAnalyzeBmp = new Bitmap(fieldImg.Width, fieldImg.Height)) using (Graphics forAnalyzeG = Graphics.FromImage(forAnalyzeBmp)) { // フィールドのキャプチャ範囲を取り込む Rectangle fieldRect = captureRects.GetFieldRect(fieldNo); //取り込んだ画像を解析用のBMPに出力 Rectangle dest = new Rectangle(0, 0, 192, 384); Rectangle src = new Rectangle() { X = fieldRect.X - captureRects.CaptureRect.X, Y = fieldRect.Y - captureRects.CaptureRect.Y, Width = fieldRect.Width, Height = fieldRect.Height }; forAnalyzeG.DrawImage(screenBmp, dest, src, GraphicsUnit.Pixel); Bitmap cellBmp = forAnalyzeBmp.Clone(pixelingCellRect, forAnalyzeBmp.PixelFormat); PuyoType puyoType = sampler.GetSamplingType(); // 選択したサンプルを設定 RapidBitmapAccessor ba = new RapidBitmapAccessor(cellBmp); ba.BeginAccess(); detector.UpdateSample(puyoType, ba); ba.EndAccess(); // サンプルした画像を保存 if (sampleImgs[puyoType].Image != null) { sampleImgs[puyoType].Image.Dispose(); } sampleImgs[puyoType].Image = cellBmp; sampler.SaveSample(cellBmp); } } sampler.Proceed(); statusLabel.Text = sampler.GetText(); }
/// <summary> /// ネクスト画像を解析する /// </summary> /// <param name="bmp">解析する画像</param> /// <returns>ネクスト状態</returns> private CaptureField AnalyzeNext(Bitmap bmp) { CaptureField field = new CaptureField(); RapidBitmapAccessor ba = new RapidBitmapAccessor(bmp); ba.BeginAccess(); ColorPairPuyo pp = new ColorPairPuyo(); for (int y = 0; y < 2; y++) { pp[y] = detector.Detect(ba, field.GetNextRect(0, y)); } field.Next = pp; ba.EndAccess(); return(field); }
/// <summary> /// サンプル画像の初期設定を行う /// </summary> /// <param name="puyoType">ぷよ種別</param> private void updateSample(PuyoType puyoType) { string filePath = Path.Combine("img", puyoType.ToString() + ".bmp"); if (!File.Exists(filePath)) { return; } Bitmap sampleBmp = (Bitmap)Bitmap.FromFile(filePath); RapidBitmapAccessor ba = new RapidBitmapAccessor(sampleBmp); ba.BeginAccess(); detector.UpdateSample(puyoType, ba); ba.EndAccess(); sampleImgs[puyoType].Image = sampleBmp; }
/// <summary> /// フィールド状態を解析する /// </summary> /// <param name="bmp">解析対象画像</param> /// <returns>フィールド状態</returns> private CaptureField AnalyzeField(Bitmap bmp) { CaptureField field = new CaptureField(); RapidBitmapAccessor ba = new RapidBitmapAccessor(bmp); ba.BeginAccess(); for (int y = 0; y < CaptureField.Y_MAX; y++) { for (int x = 0; x < CaptureField.X_MAX; x++) { field.SetPuyoType(x, y, detector.Detect(ba, field.GetRect(x, y))); } } ba.EndAccess(); field.Correct(); return(field); }
/// <summary> /// ぷよ種別を判定する /// </summary> /// <param name="ba">判定元画像のアクセサ</param> /// <param name="rect">判定に使用する範囲</param> /// <returns>判定されたぷよ種別</returns> public PuyoType Detect(RapidBitmapAccessor ba, Rectangle rect) { int[,,] pattern = GetPattern(ba, rect); return(GetPuyoType(pattern)); }