private void button7_Click(object sender, EventArgs e) { Manual mc = new Manual(); mc.ShowDialog(this); }
private void uun(object sender, EventArgs e) { t.Stop(); writeLog("タイマーt停止"); t1.Stop(); writeLog("タイマーt1停止"); //using (var cap = Cv.CreateCameraCapture(0)) // カメラのキャプチャ //{ //IplImage im = new IplImage(); // カメラ画像格納用の変数 //while (Cv.WaitKey(1) == -1) // 任意のキーが入力されるまでカメラ映像を表示 //{ // im = Cv.QueryFrame(capture); // カメラからフレーム(画像)を取得 // Cv.ShowImage("Test", im); // カメラ映像の表示 // //Bitmap iplim = BitmapConverter.ToBitmap(im); // //pictureBox2.Image = iplim; //} //} //this.Focus(); image = Cv.QueryFrame(capture); //撮影 Cv.ReleaseImage(image); image = Cv.QueryFrame(capture); writeLog("撮影し、imageに格納しました"); timer.Start(); writeLog("タイマーtimer開始"); Bitmap myBitmap = BitmapConverter.ToBitmap(image); pictureBox2.Image = myBitmap; writeLog("imageをBitmap化、右上のボックスに表示します"); IplImage srcImg = BitmapConverter.ToIplImage(myBitmap); IplImage srcImgGray = new IplImage(srcImg.Size, BitDepth.U8, 1); IplImage tmpImg = new IplImage(srcImg.Size, BitDepth.U8, 1); if(Properties.Settings.Default.autoc == true) { CvMemStorage storage = new CvMemStorage(0); Cv.CvtColor(srcImg, srcImgGray, ColorConversion.BgrToGray); //画像の二値化と輪郭の検出 Cv.Threshold(srcImgGray, tmpImg, 100, 255, ThresholdType.Binary); // Cv.Threshold(srcImgGray,tmpImg, 0, 255, ThresholdType.Otsu); Bitmap srcimageG = BitmapConverter.ToBitmap(tmpImg); pictureBox4.Image = srcimageG; CvSeq<CvPoint> contours; //輪郭シーケンスを定義 Cv.FindContours(tmpImg, storage, out contours,CvContour.SizeOf,ContourRetrieval.Tree,ContourChain.ApproxSimple); contours = Cv.ApproxPoly(contours, CvContour.SizeOf, storage, CvConst.CV_POLY_APPROX_DP, 10, true); //ツリーノードイテレータの初期化 writeLog("ツリーノードイテレータを初期化しています..."); CvTreeNodeIterator<CvSeq<CvPoint>> it = new CvTreeNodeIterator<CvSeq<CvPoint>>(contours, 1); foreach (CvSeq<CvPoint> contour in it) { // 輪郭を構成する頂点座標を取得 CvPoint tmp = contour[-1].Value; int vertex_count = contours.Total; writeLog("頂点が" + vertex_count + "個検出されました"); for (int i = 0; i < contour.Total; i++) { int count = i + 1 ; writeLog(count + "回目のトライです"); writeLog("キャリブレーションを試みます..."); if (contour.Total == 4 && contours.Total ==1) { writeLog("4つの頂点と1の矩形が検出されました"); writeLog("射影変換処理を開始します..."); CvPoint point = contour[i].Value; srcImg.Line(tmp, point, CvColor.Blue, 2); Bitmap tmpLine = BitmapConverter.ToBitmap(srcImg); pictureBox4.Image = tmpLine; tmp = point; CvPoint a = contour[0].Value; CvPoint b = contour[1].Value; CvPoint c = contour[2].Value; CvPoint d = contour[3].Value; //src画像用の座標 double[] A = new double[]{ a.X, a.Y, b.X, b.Y, c.X, c.Y, d.X, d.Y }; // 変換先の頂点の座標 double[] B = new double[]{ 0, 0, 0, 720, 960, 720, 960, 0 }; Properties.Settings.Default.X1 = A[0]; Properties.Settings.Default.Y1 = A[1]; Properties.Settings.Default.X2 = A[2]; Properties.Settings.Default.Y2 = A[3]; Properties.Settings.Default.X3 = A[4]; Properties.Settings.Default.Y3 = A[5]; Properties.Settings.Default.X4 = A[6]; Properties.Settings.Default.Y4 = A[7]; Properties.Settings.Default.Save(); homography hom = new homography(); IplImage dst = hom.hom(srcImg, A, B); Bitmap homed = BitmapConverter.ToBitmap(dst); pictureBox5.Image = homed; } else { writeLog("条件を満たさなかったため、射影変換をスキップします"); } timer.Stop(); writeLog("タイマーtimerを停止"); //Bitmap hhh= BitmapConverter.ToBitmap(imgTo); //writeLog("指定色を抽出した画像を右下に表示します"); //pictureBox3.Image = hhh; } } Cv.ReleaseImage(image); writeLog("imageに使用していたメモリを開放します"); } else { writeLog("自動キャリブレーションはスキップされました"); writeLog("マニュアルキャリブレーションを実行します"); Manual ma = new Manual(); writeLog("設定画面を開きます"); DialogResult result = ma.ShowDialog(this); if (!result.Equals(DialogResult.OK)) { // 設定内容を読み込みなおす Properties.Settings.Default.Reload(); writeLog("propertiesをリロードしました"); return; } // 設定内容を保存する Properties.Settings.Default.Save(); writeLog("propertiesを保存しました"); double[] M = 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 }; homography hom1 = new homography(); IplImage dst = Cv.CreateImage(new CvSize(image.Width, image.Height), BitDepth.U8, 3); dst = hom1.hom(srcImg, M, B); Bitmap homed = BitmapConverter.ToBitmap(dst); pictureBox5.Image = homed; } if (Properties.Settings.Default.MousePaint == true) { ext1 = new Timer(); ext1.Interval = 100; ext1.Tick += new EventHandler(MPloop); ext1.Start(); } else { ext1 = new Timer(); ext1.Interval = 100; ext1.Tick += new EventHandler(homloop); ext1.Start(); } }