public CharactersInfo(CvMat _image) { positions = new List<CvRect>(); // 画像コピー image = _image.Clone(); // 文字列認識 // 輝点列検索フラグ.falseなら暗点列を探す bool searchingBright = true; // 始点 int left = 0; // 終点 int right; for (int col = 0; col < image.Cols; col++) { // 列の輝点数 int nonzero = image.GetCol(col).CountNonZero(); // 探索モードによる分岐 if ( true == searchingBright) { // 輝点列探索中 if (0 < nonzero) { // 輝点が1個以上あったなら left = col; // フラグ切り替え searchingBright = false; } } else { // 暗点列探索中 if (0 == nonzero) { // 暗点列だったなら right = col; // LowestWidth を満足するか if (CharactersInfo.LowestWidth > right - left) { // 条件を満たさない場合は探索やり直し searchingBright = true; continue; } // 文字と認める CvMat character = image.GetCols( left, right ); // 上端輝点行を探す int top = 0; for (int row = 0; row < character.Rows; row++) { if (0 < character.GetRow(row).CountNonZero()) { // 輝点発見 top = row; break; } } // 下端輝点行を探す int bottom = character.Rows - 1; for (int row = bottom; row > top; row--) { if (0 < character.GetRow(row).CountNonZero()) { // 輝点発見 bottom = row + 1; break; } } // 文字領域確定 positions.Add(new CvRect(left, top, right - left, bottom - top)); // 探索フラグ切り替え searchingBright = true; } } } }