Exemplo n.º 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="kpt"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        static private int GetPointOctave(CvSURFPoint kpt, CvSURFParams param)
        {
            /* Wavelet size at first layer of first octave. */ 
            const int HAAR_SIZE0 = 9;
            /* Wavelet size increment between layers. This should be an even number, 
             such that the wavelet sizes in an octave are either all even or all odd.
             This ensures that when looking for the neighbours of a sample, the layers
             above and below are aligned correctly. */
            const int HAAR_SIZE_INC = 6;

            int bestOctave = 0;
            float minDiff = float.MaxValue;
            for (int octave = 1; octave < param.NOctaves; octave++)
            {
                for (int layer = 0; layer < param.NOctaveLayers; layer++)
                {
                    float diff = Math.Abs(kpt.Size - (float)((HAAR_SIZE0 + HAAR_SIZE_INC * layer) << octave));
                    if (minDiff > diff)
                    {
                        minDiff = diff;
                        bestOctave = octave;
                        if (Math.Abs(minDiff - 0) < 1e-9)
                            return bestOctave;
                    }
                }
            }
            return bestOctave;
        }
Exemplo n.º 2
0
        public SURFSample()
        {
            // cvExtractSURF

            // call cv::initModule_nonfree() before using SURF/SIFT.
            Cv2.InitModule_NonFree();

            using (IplImage obj = Cv.LoadImage(FilePath.Image.SurfBox, LoadMode.GrayScale))
                using (IplImage image = Cv.LoadImage(FilePath.Image.SurfBoxinscene, LoadMode.GrayScale))
                    using (IplImage objColor = Cv.CreateImage(obj.Size, BitDepth.U8, 3))
                        using (IplImage correspond = Cv.CreateImage(new CvSize(image.Width, obj.Height + image.Height), BitDepth.U8, 1))
                        {
                            Cv.CvtColor(obj, objColor, ColorConversion.GrayToBgr);

                            Cv.SetImageROI(correspond, new CvRect(0, 0, obj.Width, obj.Height));
                            Cv.Copy(obj, correspond);
                            Cv.SetImageROI(correspond, new CvRect(0, obj.Height, correspond.Width, correspond.Height));
                            Cv.Copy(image, correspond);
                            Cv.ResetImageROI(correspond);

                            CvSURFPoint[] objectKeypoints, imageKeypoints;
                            float[][]     objectDescriptors, imageDescriptors;
                            Stopwatch     watch = Stopwatch.StartNew();
                            {
                                CvSURFParams param = new CvSURFParams(500, true);
                                Cv.ExtractSURF(obj, null, out objectKeypoints, out objectDescriptors, param);
                                Console.WriteLine("Object Descriptors: {0}", objectDescriptors.Length);
                                Cv.ExtractSURF(image, null, out imageKeypoints, out imageDescriptors, param);
                                Console.WriteLine("Image Descriptors: {0}", imageDescriptors.Length);
                            }
                            watch.Stop();
                            Console.WriteLine("Extraction time = {0}ms", watch.ElapsedMilliseconds);
                            watch.Reset();
                            watch.Start();

                            CvPoint[] srcCorners = new CvPoint[4]
                            {
                                new CvPoint(0, 0), new CvPoint(obj.Width, 0), new CvPoint(obj.Width, obj.Height), new CvPoint(0, obj.Height)
                            };
                            CvPoint[] dstCorners = LocatePlanarObject(objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, srcCorners);
                            if (dstCorners != null)
                            {
                                for (int i = 0; i < 4; i++)
                                {
                                    CvPoint r1 = dstCorners[i % 4];
                                    CvPoint r2 = dstCorners[(i + 1) % 4];
                                    Cv.Line(correspond, new CvPoint(r1.X, r1.Y + obj.Height), new CvPoint(r2.X, r2.Y + obj.Height), CvColor.White);
                                }
                            }

                            // 対応点同士を線で引く
                            int[] ptPairs = FindPairs(objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors);
                            for (int i = 0; i < ptPairs.Length; i += 2)
                            {
                                CvSURFPoint r1 = objectKeypoints[ptPairs[i]];
                                CvSURFPoint r2 = imageKeypoints[ptPairs[i + 1]];
                                Cv.Line(correspond, r1.Pt, new CvPoint(Cv.Round(r2.Pt.X), Cv.Round(r2.Pt.Y + obj.Height)), CvColor.White);
                            }

                            // 特徴点の場所に円を描く
                            for (int i = 0; i < objectKeypoints.Length; i++)
                            {
                                CvSURFPoint r      = objectKeypoints[i];
                                CvPoint     center = new CvPoint(Cv.Round(r.Pt.X), Cv.Round(r.Pt.Y));
                                int         radius = Cv.Round(r.Size * (1.2 / 9.0) * 2);
                                Cv.Circle(objColor, center, radius, CvColor.Red, 1, LineType.AntiAlias, 0);
                            }
                            watch.Stop();
                            Console.WriteLine("Drawing time = {0}ms", watch.ElapsedMilliseconds);

                            using (CvWindow windowObject = new CvWindow("Object", WindowMode.AutoSize))
                                using (CvWindow windowCorrespond = new CvWindow("Object Correspond", WindowMode.AutoSize))
                                {
                                    windowObject.ShowImage(correspond);
                                    windowCorrespond.ShowImage(objColor);
                                    Cv.WaitKey(0);
                                }
                        }
        }