// 数字認識 internal static int recognizeDigit(CvMat image) { int nonzero = 0; nonzero = image.GetCols( image.Cols-2, image.Cols ).CountNonZero(); if ( image.Rows * 2 == nonzero ) // 1 右端2列がすべて輝点 return 1; nonzero = image.GetRows( image.Rows-2, image.Rows ).CountNonZero(); if ( image.Cols * 2 == nonzero ) // 2 下端2行がすべて輝点 return 2; nonzero = image.GetRows ( 0, 2 ).CountNonZero(); if ( image.Cols * 2 - 2 < nonzero ) // 7 上端2行がすべて輝点.ただし1ピクセルまで欠けても良い return 7; nonzero = image.GetCols ( image.Cols-3, image.Cols-1 ).CountNonZero(); if ( image.Rows * 2 == nonzero ) // 4 右端の左2列がすべて輝点 return 4; CvRect rect = new CvRect( 0, 0, 1, image.Rows*2/3 ); CvMat subarr; nonzero = image.GetSubArr ( out subarr, rect ).CountNonZero(); if ( 0 == nonzero ) // 3 左端の上部3分の2がすべて暗点 return 3; rect = new CvRect ( 0, image.Rows/2, 3, 2 ); nonzero = image.GetSubArr ( out subarr, rect ).CountNonZero(); if ( 0 == nonzero ) // 5 左端の下半分開始すぐのwidth3 height2 がすべて暗点 return 5; rect = new CvRect ( image.Cols/2, image.Rows/2-1, 1, 3 ); nonzero = image.GetSubArr( out subarr, rect ).CountNonZero(); if ( 0 == nonzero ) // 0 中央列中央3ピクセルがすべて暗点 return 0; rect = new CvRect ( image.Cols-1, 0, 1, image.Rows*2/5 ); nonzero = image.GetSubArr( out subarr, rect ).CountNonZero(); if ( 0 == nonzero ) // 6 右端上部5分の2がすべて暗点 return 6; rect = new CvRect ( image.Cols-1, image.Rows-3, 1, 3 ); nonzero = image.GetSubArr( out subarr, rect ).CountNonZero(); if ( 0 == nonzero ) // 右端下部3ピクセルがすべて暗点 return 9; // 8 上記条件を満たさない return 8; }
/** 所属国認識2 * @param image 所属国アイコン部分カラー画像 */ static 所属国 recognizeNationality2( CvMat image ) { // 閾値130.0で2値化した画像を基にする CvMat bin = new CvMat( image.Rows, image.Cols, MatrixType.U8C1); image.CvtColor( bin, ColorConversion.BgrToGray ); bin.Threshold( bin, 130.0, 255.0, ThresholdType.Binary ); // iel: 7 // net: 23 // http://twitpic.com/azbzjh int count = bin.GetRows( 2, bin.Rows ).CountNonZero(); if ( count > 15 ) return 所属国.ネツァワル; if ( count > 3 ) return 所属国.エルソード; return 所属国.不明; }
/// <summary> /// Classical Multidimensional Scaling /// </summary> public MDS() { // creates distance matrix int size = CityDistance.GetLength(0); CvMat t = new CvMat(size, size, MatrixType.F64C1, CityDistance); // adds Torgerson's additive constant to t t += Torgerson(t); // squares all elements of t t.Mul(t, t); // centering matrix G CvMat g = CenteringMatrix(size); // calculates inner product matrix B CvMat b = g * t * g.T() * -0.5; // calculates eigenvalues and eigenvectors of B CvMat vectors = new CvMat(size, size, MatrixType.F64C1); CvMat values = new CvMat(size, 1, MatrixType.F64C1); Cv.EigenVV(b, vectors, values); for (int r = 0; r < values.Rows; r++) { if (values[r] < 0) values[r] = 0; } // multiplies sqrt(eigenvalue) by eigenvector CvMat result = vectors.GetRows(0, 2); for (int r = 0; r < result.Rows; r++) { for (int c = 0; c < result.Cols; c++) { result[r, c] *= Math.Sqrt(values[r]); } } // scaling Cv.Normalize(result, result, 0, 800, NormType.MinMax); //Console.WriteLine(vectors); //Console.WriteLine(values); //Console.WriteLine(result); // opens a window using (IplImage img = new IplImage(800, 600, BitDepth.U8, 3)) using (CvFont font = new CvFont(FontFace.HersheySimplex, 0.5f, 0.5f)) using (CvWindow window = new CvWindow("City Location Estimation")) { img.Zero(); for (int c = 0; c < size; c++) { double x = result[0, c]; double y = result[1, c]; x = x * 0.7 + img.Width * 0.1; y = y * 0.7 + img.Height * 0.1; img.Circle((int)x, (int)y, 5, CvColor.Red, -1); img.PutText(CityNames[c], new CvPoint((int)x+5, (int)y+10), font, CvColor.White); } window.Image = img; Cv.WaitKey(); } }
/// <summary> /// Classical Multidimensional Scaling /// </summary> public MDS() { // creates distance matrix int size = CityDistance.GetLength(0); CvMat t = new CvMat(size, size, MatrixType.F64C1, CityDistance); // adds Torgerson's additive constant to t t += Torgerson(t); // squares all elements of t t.Mul(t, t); // centering matrix G CvMat g = CenteringMatrix(size); // calculates inner product matrix B CvMat b = g * t * g.T() * -0.5; // calculates eigenvalues and eigenvectors of B CvMat vectors = new CvMat(size, size, MatrixType.F64C1); CvMat values = new CvMat(size, 1, MatrixType.F64C1); Cv.EigenVV(b, vectors, values); for (int r = 0; r < values.Rows; r++) { if (values[r] < 0) { values[r] = 0; } } // multiplies sqrt(eigenvalue) by eigenvector CvMat result = vectors.GetRows(0, 2); for (int r = 0; r < result.Rows; r++) { for (int c = 0; c < result.Cols; c++) { result[r, c] *= Math.Sqrt(values[r]); } } // scaling Cv.Normalize(result, result, 0, 800, NormType.MinMax); //Console.WriteLine(vectors); //Console.WriteLine(values); //Console.WriteLine(result); // opens a window using (IplImage img = new IplImage(800, 600, BitDepth.U8, 3)) using (CvFont font = new CvFont(FontFace.HersheySimplex, 0.5f, 0.5f)) using (CvWindow window = new CvWindow("City Location Estimation")) { img.Zero(); for (int c = 0; c < size; c++) { double x = result[0, c]; double y = result[1, c]; x = x * 0.7 + img.Width * 0.1; y = y * 0.7 + img.Height * 0.1; img.Circle((int)x, (int)y, 5, CvColor.Red, -1); img.PutText(CityNames[c], new CvPoint((int)x + 5, (int)y + 10), font, CvColor.White); } window.Image = img; Cv.WaitKey(); } }