/// <summary> /// cvGetHuMomentsにより初期化 /// </summary> /// <param name="moments">画像モーメント構造体への参照</param> #else /// <summary> /// Initialize by cvGetHuMoments /// </summary> /// <param name="moments">Pointer to the moment state structure. </param> #endif public CvHuMoments(CvMoments moments) { if (moments == null) { throw new ArgumentNullException("moments"); } CvInvoke.cvGetHuMoments(moments, this); }
/// <summary> /// cvGetHuMomentsにより初期化 /// </summary> /// <param name="moments">画像モーメント構造体への参照</param> #else /// <summary> /// Initialize by cvGetHuMoments /// </summary> /// <param name="moments">Pointer to the moment state structure. </param> #endif public CvHuMoments(CvMoments moments) { if (moments == null) { throw new ArgumentNullException("moments"); } NativeMethods.cvGetHuMoments(moments, this); }
public Moments() { // (1)画像を読み込む.3チャンネル画像の場合はCOIがセットされていなければならない using (IplImage srcImg = new IplImage(Const.ImageLenna, LoadMode.AnyColor | LoadMode.AnyDepth)) { if (srcImg.NChannels == 3 && srcImg.COI == 0) { srcImg.COI = 1; } // (2)入力画像の3次までの画像モーメントを計算する CvMoments moments = new CvMoments(srcImg, false); srcImg.COI = 0; // (3)モーメントやHuモーメント不変量を,得られたCvMoments構造体の値を使って計算する. double spatialMoment = moments.GetSpatialMoment(0, 0); double centralMoment = moments.GetCentralMoment(0, 0); double normCMoment = moments.GetNormalizedCentralMoment(0, 0); CvHuMoments huMoments = new CvHuMoments(moments); // (4)得られたモーメントやHuモーメント不変量を文字として画像に描画 using (CvFont font = new CvFont(FontFace.HersheySimplex, 1.0, 1.0, 0, 2, LineType.Link8)) { string[] text = new string[10]; text[0] = string.Format("spatial={0:F3}", spatialMoment); text[1] = string.Format("central={0:F3}", centralMoment); text[2] = string.Format("norm={0:F3}", spatialMoment); text[3] = string.Format("hu1={0:F10}", huMoments.Hu1); text[4] = string.Format("hu2={0:F10}", huMoments.Hu2); text[5] = string.Format("hu3={0:F10}", huMoments.Hu3); text[6] = string.Format("hu4={0:F10}", huMoments.Hu4); text[7] = string.Format("hu5={0:F10}", huMoments.Hu5); text[8] = string.Format("hu6={0:F10}", huMoments.Hu6); text[9] = string.Format("hu7={0:F10}", huMoments.Hu7); CvSize textSize = font.GetTextSize(text[0]); for (int i = 0; i < 10; i++) { srcImg.PutText(text[i], new CvPoint(10, (textSize.Height + 3) * (i + 1)), font, CvColor.Black); } } // (5)入力画像とモーメント計算結果を表示,キーが押されたときに終了 using (CvWindow window = new CvWindow("Image", WindowMode.AutoSize)) { window.ShowImage(srcImg); Cv.WaitKey(0); } } }
public Moments() { using (IplImage srcImg = new IplImage(FilePath.Image.Lenna, LoadMode.AnyColor | LoadMode.AnyDepth)) { if (srcImg.NChannels == 3 && srcImg.COI == 0) { srcImg.COI = 1; } CvMoments moments = new CvMoments(srcImg, false); srcImg.COI = 0; double spatialMoment = moments.GetSpatialMoment(0, 0); double centralMoment = moments.GetCentralMoment(0, 0); double normCMoment = moments.GetNormalizedCentralMoment(0, 0); CvHuMoments huMoments = new CvHuMoments(moments); // drawing using (CvFont font = new CvFont(FontFace.HersheySimplex, 1.0, 1.0, 0, 2, LineType.Link8)) { string[] text = new string[10]; text[0] = string.Format("spatial={0:F3}", spatialMoment); text[1] = string.Format("central={0:F3}", centralMoment); text[2] = string.Format("norm={0:F3}", spatialMoment); text[3] = string.Format("hu1={0:F10}", huMoments.Hu1); text[4] = string.Format("hu2={0:F10}", huMoments.Hu2); text[5] = string.Format("hu3={0:F10}", huMoments.Hu3); text[6] = string.Format("hu4={0:F10}", huMoments.Hu4); text[7] = string.Format("hu5={0:F10}", huMoments.Hu5); text[8] = string.Format("hu6={0:F10}", huMoments.Hu6); text[9] = string.Format("hu7={0:F10}", huMoments.Hu7); CvSize textSize = font.GetTextSize(text[0]); for (int i = 0; i < 10; i++) { srcImg.PutText(text[i], new CvPoint(10, (textSize.Height + 3) * (i + 1)), font, CvColor.Black); } } using (var window = new CvWindow("Image", WindowMode.AutoSize)) { window.ShowImage(srcImg); Cv.WaitKey(0); } } }
public void homloop(object sender, EventArgs e) { image = Cv.QueryFrame(capture); homography homl = new homography(); double[] A = new double[]{ Properties.Settings.Default.X1, Properties.Settings.Default.Y1, Properties.Settings.Default.X2, Properties.Settings.Default.Y2, Properties.Settings.Default.X3, Properties.Settings.Default.Y3, Properties.Settings.Default.X4, Properties.Settings.Default.Y4 }; // 変換先の頂点の座標 double[] B = new double[]{ 0, 0, 0, 720, 960, 720, 960, 0 }; IplImage homed1 = Cv.CreateImage(new CvSize(image.Width, image.Height), BitDepth.U8, 3); homed1 = homl.hom(image, A, B); Bitmap homedb = BitmapConverter.ToBitmap(homed1); pictureBox5.Image = homedb; double H1 = Decimal.ToDouble(Properties.Settings.Default.ext_H1); double S1 = Decimal.ToDouble(Properties.Settings.Default.ext_S1); double V1 = Decimal.ToDouble(Properties.Settings.Default.ext_V1); double H2 = Decimal.ToDouble(Properties.Settings.Default.ext_H2); double S2 = Decimal.ToDouble(Properties.Settings.Default.ext_S2); double V2 = Decimal.ToDouble(Properties.Settings.Default.ext_V2); label1.Text = timer.Elapsed.ToString(); IplImage imgTracking = Cv.CreateImage(new CvSize(image.Width, image.Height), BitDepth.U8, 3); Cv.Zero(imgTracking); IplImage imgHSV = Cv.CreateImage(homed1.Size, BitDepth.U8, 3); Cv.CvtColor(homed1, imgHSV, ColorConversion.BgrToHsv); IplImage imgTo = Cv.CreateImage(new CvSize(imgHSV.Width, imgHSV.Height), BitDepth.U8, 1); //指定色を抽出 Cv.InRangeS(imgHSV, new CvScalar(H1, S1, V1), new CvScalar(H2, S2, V2), imgTo); if(Properties.Settings.Default.Filter1 == true) { Cv.Smooth(imgTo, imgTo, SmoothType.Median, 19, 19); } //ディスプレイの高さ int h = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; //ディスプレイの幅 int w = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; IplImage imgDispray = Cv.CreateImage(new CvSize(w, h), BitDepth.U8, 1); Cv.Resize(imgTo, imgDispray); CvMoments moments = new CvMoments(imgDispray, false); Bitmap imgToB = BitmapConverter.ToBitmap(imgDispray); pictureBox3.Image = imgToB; //指定色の面積取得 double area = Cv.GetCentralMoment(moments, 0, 0); double moments01 = Cv.GetSpatialMoment(moments, 0, 1); double moments10 = Cv.GetSpatialMoment(moments, 1, 0); //重心座標取得 int posX = (int)(moments10 / area); int posY = (int)(moments01 / area); if ((PastPoint.X != posX) || (PastPoint.Y != posY)) { if (MyStopWatch.IsRunning == true) { MyStopWatch.Stop(); } float nowX = Math.Abs((float)(posX - PastPoint.X)); //X成分の距離 //移動距離 float nowY = Math.Abs((float)(posY - PastPoint.Y)); //Y成分の距離 float nowXY = (float)Math.Sqrt((double)((nowX * nowX) + (nowY * nowY))); float MouseSpeed = (nowXY / (float)(MyStopWatch.ElapsedMilliseconds)); PastPoint = new Point(posX, posY); label2.Text = "移動距離X" + nowX.ToString(); label3.Text = "移動距離Y" + nowY.ToString(); label4.Text = "面積" + area.ToString(); label5.Text = "重心X" + posX.ToString(); label6.Text = "重心Y" + posY.ToString(); label7.Text = "カーソル速度" + MouseSpeed.ToString(); MyStopWatch.Restart(); //TopMost = true; if(posX < 0) { posX = 480; } if(posY < 0) { posY = 360; } //位置が0でなく、移動速度が0.4~1.6pixel/msec if (PastPoint.X >= 0 && PastPoint.Y >= 0 && posX >= 0 && posY >= 0 && MouseSpeed > 0.4 && MouseSpeed < 1.6) //0.4-1.6 { Graphics g = pictureBox1.CreateGraphics(); //描画定義 writeLog("線を描画しています"); Pen pen = new Pen(Color.Aqua); //ペン色 pen.Width = 5; //ペン太さ g.DrawLine(pen, sx1, sy1, posX, posY); sx1 = posX; sy1 = posY; PastPoint = new Point(posX, posY); //ug.Dispose(); aiu = 0; Invalidate(); } if (posY > 720 && MouseSpeed < 2.0 && aiu == 0) //2.0- { writeLog("ページを送ります"); //this.Hide(); SendKeys.Send("{RIGHT}"); aiu = 1; Invalidate(); } if (posY < 240 && MouseSpeed < 2.0 && aiu == 0) { writeLog("ページを戻します"); //this.Hide(); SendKeys.Send("{LEFT}"); aiu = 1; Invalidate(); } sx1 = posX; sy1 = posY; } }