コード例 #1
0
ファイル: HoughCircles.cs プロジェクト: qxp1011/opencvsharp
        public HoughCircles()
        {
            using (IplImage imgSrc = new IplImage(Const.ImageWalkman, LoadMode.Color))
            using (IplImage imgGray = new IplImage(imgSrc.Size, BitDepth.U8, 1))
            using (IplImage imgHough = imgSrc.Clone())
            {
                Cv.CvtColor(imgSrc, imgGray, ColorConversion.BgrToGray);
                Cv.Smooth(imgGray, imgGray, SmoothType.Gaussian, 9);
                //Cv.Canny(imgGray, imgGray, 75, 150, ApertureSize.Size3);

                using (CvMemStorage storage = new CvMemStorage())
                {
                    CvSeq<CvCircleSegment> seq = imgGray.HoughCircles(storage, HoughCirclesMethod.Gradient, 1, 100, 150, 55, 0, 0);
                    foreach (CvCircleSegment item in seq)
                    {
                        imgHough.Circle(item.Center, (int)item.Radius, CvColor.Red, 3);
                    }
                }

                // (5)検出結果表示用のウィンドウを確保し表示する
                using (new CvWindow("gray", WindowMode.AutoSize, imgGray))
                using (new CvWindow("Hough circles", WindowMode.AutoSize, imgHough))
                {
                    CvWindow.WaitKey(0);
                }
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: takahash/rockandrobo
        private void timer1_Tick(object sender, EventArgs e)
        {
            // キャプチャの開始. Capture starts.
            IplImage ipl1 = capture.QueryFrame();
            IplImage ipl2 = new IplImage(ipl1.Size, BitDepth.U8, 1);
            IplImage ipl3 = ipl1.Clone();
            //IplImage ipl2 = capture.QueryFrame();
            //Cv.CvtColor(ipl1, ipl1, ColorConversion.BgrToHsv);
            //Mat mat = new Mat(ipl1, true);

            // 取得したカメラ画像の高さと幅を取得し、labelに表示. Height and width of camera are shown in label.
            labelWidth.Text = capture.FrameWidth.ToString();
            labelHeight.Text = capture.FrameHeight.ToString();

            if (ipl1 != null)
            {
                // pictureBoxに取得した画像を表示. Show the captured image.
                pictureBox1.Image = ipl1.ToBitmap();
                // メモリリークが発生するらしいので
                // プログラムが動的に確保したメモリ領域のうち、
                // 不要になった領域を定期的に自動解放する

                if (GC.GetTotalMemory(false) > 600000)
                {
                    GC.Collect();
                }

                /*
                // Image processing should be written from here.

                // Extract red color
                for (int y = 0; y < ipl1.Height; y++)
                {
                    for (int x = 0; x < ipl1.Width; x++)
                    {
                        CvColor c = ipl1[y, x];

                        // Red color extraction
                        // If the pixel is red-like, the image is white, else black.
                        if (c.R > 80 && c.B < 70 && c.G < 70)
                        {
                            ipl1[y, x] = new CvColor()
                            {
                                B = 255,
                                G = 255,
                                R = 255,
                            };
                        }
                        else
                        {
                            ipl1[y, x] = new CvColor()
                            {
                                // Red color extraction
                                B = 0,
                                G = 0,
                                R = 0,
                            };

                        }
                    }
                }
                */

                // 追加した記述
                Cv.CvtColor(ipl1, ipl2, ColorConversion.BgrToGray);
                Cv.Smooth(ipl2, ipl2, SmoothType.Gaussian, 9);
                using (CvMemStorage storage = new CvMemStorage())
                {
                    CvSeq<CvCircleSegment> seq = ipl2.HoughCircles(storage, HoughCirclesMethod.Gradient, 1, 100, 150, 55, 0, 0);
                    foreach (CvCircleSegment item in seq)
                    {
                        ipl3.Circle(item.Center, (int)item.Radius, CvColor.Red, 3);
                        ipl3.Circle(item.Center, 1, CvColor.Red, 3);
                        labelCenter.Text = item.Center.ToString();
                    }
                }

                //Cv.Laplace(ipl1, ipl2, ApertureSize.Size3);
                // Show the image to picturebox2.
                pictureBox2.Image = ipl2.ToBitmap();
                pictureBox3.Image = ipl3.ToBitmap();

            }
            else
            {
                timer1.Stop();
            }
        }