/// <summary> /// プレーシェア画像から各種情報を検出して返します。 /// </summary> /// <param name="filename">ファイル名</param> /// <returns>スコア</returns> public static DetectResult Detect(string filename) { Mat playShare = null; DetectProperty prop = null; try { playShare = new Mat(filename, ImreadModes.Color); } catch (FileNotFoundException ex) { MessageBox.Show("プレーシェア画像の読込中にエラーが発生しました。\n\n------------------------------\n" + ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Stop); throw ex; } try { string json = ""; using (var sr = new StreamReader(JSONPATH, System.Text.Encoding.UTF8)) { json = sr.ReadToEnd(); } prop = JsonConvert.DeserializeObject <DetectProperty>(json); } catch (FileNotFoundException ex) { MessageBox.Show("設定の読込中にエラーが発生しました。\n\n------------------------------\n" + ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Stop); throw ex; } if (prop == null) { throw new NullReferenceException(); } var score = MatchScoreTemplate(playShare, prop); var grade = CalcurateGrade(score); return(new DetectResult(score, grade)); }
/// <summary> /// 画像からスコアを検出します。 /// </summary> /// <param name="playShare">元画像(プレーシェア画像)</param> /// <param name="prop">設定情報</param> /// <returns>スコア</returns> private static int MatchScoreTemplate(Mat playShare, DetectProperty prop) { string _score = ""; for (var i = 0; i < 8; i++) { var max = 0.0; var num = 0; for (var t = 0; t < 10; t++) { var file = "./template/" + t + ".jpg"; Mat template = null; try { template = new Mat(file, ImreadModes.Color); } catch (FileNotFoundException ex) { MessageBox.Show("テンプレート画像の読込中にエラーが発生しました。\nテンプレートフォルダの存在を確認してください。\n\n------------------------------\n" + ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Stop); throw ex; } var part = new Rect(); if (i < 5) { // 大文字 part.H = prop.ScoreNum_Large.H; part.W = prop.ScoreNum_Large.W; part.X = prop.ScoreNum_Large.X + (i * part.W); part.Y = prop.ScoreNum_Large.Y; } else { // 小文字 part.W = prop.ScoreNum_Small.W; part.H = prop.ScoreNum_Small.H; part.X = prop.ScoreNum_Small.X + ((i - 5) * (part.W + 3)); part.Y = prop.ScoreNum_Small.Y; // テンプレート画像の縮小 var resized_img = new Mat( new Size((int)(template.Width * ((double)prop.ScoreNum_Small.W / prop.ScoreNum_Large.W)), (int)(template.Height * ((double)prop.ScoreNum_Small.W / prop.ScoreNum_Large.W))), template.Type() ); Cv2.Resize(template, resized_img, resized_img.Size()); template = resized_img; } var val = MatchTemplate(playShare, template, part); if (val > max) { max = val; num = t; } } _score += num; } return(int.Parse(_score)); }