Example #1
0
 /// <summary>
 /// 入力特徴量を図にする
 /// </summary>
 /// <param name="data_array"></param>
 private void Debug_DrawInputFeature(CvPoint2D32f[] points, int[] id_array)
 {
     using (IplImage pointsPlot = Cv.CreateImage(new CvSize(300, 300), BitDepth.U8, 3))
     {
         pointsPlot.Zero();
         for (int i = 0; i < id_array.Length; i++)
         {
             int x   = (int)(points[i].X * 300);
             int y   = (int)(300 - points[i].Y * 300);
             int res = id_array[i];
             //                    CvColor color = (res == 1) ? CvColor.Red : CvColor.GreenYellow;
             CvColor color = new CvColor();
             if (res == 1)
             {
                 color = CvColor.Red;
             }
             else if (res == 2)
             {
                 color = CvColor.GreenYellow;
             }
             pointsPlot.Circle(x, y, 2, color, -1);
         }
         CvWindow.ShowImages(pointsPlot);
     }
 }
Example #2
0
        //作成した辞書を図でみる
        public void Debug_DispPredict()
        {
            return;

            //辞書ファイルのロード
            this.libSVM_model = SVM.LoadModel(@"libsvm_model.xml");

            using (IplImage retPlot = new IplImage(300, 300, BitDepth.U8, 3))
            {
                for (int x = 0; x < 300; x++)
                {
                    for (int y = 0; y < 300; y++)
                    {
                        float[] sample = { x / 300f, y / 300f };
                        //問題を作成
                        SVMNode[] node_array = new SVMNode[2];
                        node_array[0] = new SVMNode(1, sample[0]);
                        node_array[1] = new SVMNode(2, sample[1]);
                        int    ret_double = (int)SVM.Predict(libSVM_model, node_array);
                        int    ret_i      = (int)ret_double;
                        CvRect plotRect   = new CvRect(x, 300 - y, 1, 1);
                        if (ret_i == 1)
                        {
                            retPlot.Rectangle(plotRect, CvColor.Red);
                        }
                        else if (ret_i == 2)
                        {
                            retPlot.Rectangle(plotRect, CvColor.GreenYellow);
                        }
                    }
                }
                CvWindow.ShowImages(retPlot);
            }
        }
Example #3
0
        public Image2Stream()
        {
            // Stream -> IplImage
            using (FileStream stream = new FileStream(FilePath.Image.Lenna, FileMode.Open))
                using (IplImage img = IplImage.FromStream(stream, LoadMode.Color))
                {
                    CvWindow.ShowImages(img);

                    // IplImage -> Stream
                    using (MemoryStream ms = new MemoryStream())
                    {
                        img.ToStream(ms, ".tiff");
                        ms.ToString();
                    }
                }

            // Stream -> CvMat
            using (FileStream stream = new FileStream(FilePath.Image.Lenna, FileMode.Open))
                using (CvMat mat = CvMat.FromStream(stream, LoadMode.Color))
                {
                    mat.ToString();

                    // CvMat -> Stream
                    using (MemoryStream ms = new MemoryStream())
                    {
                        mat.ToStream(ms, ".bmp");
                        ms.ToString();
                    }
                }
        }
Example #4
0
        public FaceDetect()
        {
            CheckMemoryLeak();

            // CvHaarClassifierCascade, cvHaarDetectObjects

            CvColor[] colors = new CvColor[] {
                new CvColor(0, 0, 255),
                new CvColor(0, 128, 255),
                new CvColor(0, 255, 255),
                new CvColor(0, 255, 0),
                new CvColor(255, 128, 0),
                new CvColor(255, 255, 0),
                new CvColor(255, 0, 0),
                new CvColor(255, 0, 255),
            };

            const double Scale        = 1.14;
            const double ScaleFactor  = 1.0850;
            const int    MinNeighbors = 2;

            using (IplImage img = new IplImage(FilePath.Image.Yalta, LoadMode.Color))
                using (IplImage smallImg = new IplImage(new CvSize(Cv.Round(img.Width / Scale), Cv.Round(img.Height / Scale)), BitDepth.U8, 1))
                {
                    using (IplImage gray = new IplImage(img.Size, BitDepth.U8, 1))
                    {
                        Cv.CvtColor(img, gray, ColorConversion.BgrToGray);
                        Cv.Resize(gray, smallImg, Interpolation.Linear);
                        Cv.EqualizeHist(smallImg, smallImg);
                    }

                    using (var cascade = CvHaarClassifierCascade.FromFile(FilePath.Text.HaarCascade))
                        using (var storage = new CvMemStorage())
                        {
                            storage.Clear();

                            // 顔の検出
                            Stopwatch         watch = Stopwatch.StartNew();
                            CvSeq <CvAvgComp> faces = Cv.HaarDetectObjects(smallImg, cascade, storage, ScaleFactor, MinNeighbors, 0, new CvSize(30, 30));
                            watch.Stop();
                            Console.WriteLine("detection time = {0}ms\n", watch.ElapsedMilliseconds);

                            // 検出した箇所にまるをつける
                            for (int i = 0; i < faces.Total; i++)
                            {
                                CvRect  r      = faces[i].Value.Rect;
                                CvPoint center = new CvPoint
                                {
                                    X = Cv.Round((r.X + r.Width * 0.5) * Scale),
                                    Y = Cv.Round((r.Y + r.Height * 0.5) * Scale)
                                };
                                int radius = Cv.Round((r.Width + r.Height) * 0.25 * Scale);
                                img.Circle(center, radius, colors[i % 8], 3, LineType.AntiAlias, 0);
                            }
                        }

                    // ウィンドウに表示
                    CvWindow.ShowImages(img);
                }
        }
Example #5
0
        public FitLine()
        {
            CvSize imageSize = new CvSize(500, 500);

            // cvFitLine
            CvPoint2D32f[] points = GetRandomPoints(20, imageSize);
            CvLine2D       line   = Cv.FitLine2D(points, DistanceType.L2, 0, 0.01, 0.01);

            using (IplImage img = new IplImage(imageSize, BitDepth.U8, 3))
            {
                img.Zero();

                // draw line
                {
                    CvPoint pt1, pt2;
                    line.FitSize(img.Width, img.Height, out pt1, out pt2);
                    img.Line(pt1, pt2, CvColor.Green, 1, LineType.Link8);
                }

                // draw points and distances
                using (CvFont font = new CvFont(FontFace.HersheySimplex, 0.33, 0.33))
                {
                    foreach (CvPoint2D32f p in points)
                    {
                        double d = line.Distance(p);

                        img.Circle(p, 2, CvColor.White, -1, LineType.AntiAlias);
                        img.PutText(string.Format("{0:F1}", d), new CvPoint((int)(p.X + 3), (int)(p.Y + 3)), font, CvColor.Green);
                    }
                }

                CvWindow.ShowImages(img);
            }
        }
Example #6
0
        //--------------------------------------------------------------------------------------
        // private
        //---------------------------------------------------------------------------------------

        private void Debug_DispPredict()
        {
            using (IplImage retPlot = new IplImage(300, 300, BitDepth.U8, 3))
            {
                for (int x = 0; x < 300; x++)
                {
                    for (int y = 0; y < 300; y++)
                    {
                        float[] sample    = { x / 300f, y / 300f };
                        CvMat   sampleMat = new CvMat(1, 2, MatrixType.F32C1, sample);
                        int     ret       = (int)svm.Predict(sampleMat);
                        CvRect  plotRect  = new CvRect(x, 300 - y, 1, 1);
                        if (ret == 1)
                        {
                            retPlot.Rectangle(plotRect, CvColor.Red);
                        }
                        else if (ret == 2)
                        {
                            retPlot.Rectangle(plotRect, CvColor.GreenYellow);
                        }
                    }
                }
                CvWindow.ShowImages(retPlot);
            }
        }
Example #7
0
        void ShowMarkedImages(IplImage scr, IplImage tmpl, CvPoint maxPoint)
        {
            var marked = scr.Clone();
            var rect   = new CvRect(maxPoint, tmpl.Size);

            marked.DrawRect(rect, new CvScalar(0, 0, 255), 2);
            CvWindow.ShowImages(marked);
        }
Example #8
0
        public void Run()
        {
            Mat src    = new Mat(FilePath.Image.Lenna511, LoadMode.Color);
            Mat src511 = new Mat(FilePath.Image.Lenna511, LoadMode.Color);

            IplImage ipl    = (IplImage)src;
            IplImage ipl511 = (IplImage)src511;

            CvWindow.ShowImages(ipl, ipl511);
        }
Example #9
0
 static void Main(string[] args)
 {
     using (var img = new IplImage(@"Images/1.jpg"))
         using (var mask = new IplImage(@"Images/1_mask.png", LoadMode.GrayScale))
             using (var dst = new IplImage(img.Size, BitDepth.U8, 3))
             {
                 // Inpaintメソッドで不要領域の除去をする
                 Cv.Inpaint(img, mask, dst, 10.0, InpaintMethod.NS);
                 // 結果をウィンドウで表示
                 CvWindow.ShowImages(dst);
             }
 }
Example #10
0
 /// <summary>
 ///
 /// </summary>
 public void DebugShow()
 {
     using (IplImage img = new IplImage(Cols, Rows, BitDepth.U8, 1))
     {
         img.Zero();
         for (int r = 0; r < Rows; r++)
         {
             for (int c = 0; c < Cols; c++)
             {
                 if (Values[r, c] != 0)
                 {
                     img[r, c] = 255;
                 }
             }
         }
         CvWindow.ShowImages(img);
     }
 }