Example #1
0
        public void Run(Mat gray1, Mat gray2, Mat dst2, bool useBFMatcher, int pointsToMatch)
        {
            var sift = SIFT.Create(pointsToMatch);

            KeyPoint[] keypoints1, keypoints2;
            var        descriptors1 = new Mat();
            var        descriptors2 = new Mat();

            sift.DetectAndCompute(gray1, null, out keypoints1, descriptors1);
            sift.DetectAndCompute(gray2, null, out keypoints2, descriptors2);

            if (useBFMatcher)
            {
                var      bfMatcher = new BFMatcher(NormTypes.L2, false);
                DMatch[] bfMatches = bfMatcher.Match(descriptors1, descriptors2);
                Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, bfMatches, dst2);
            }
            else
            {
                var      flannMatcher = new FlannBasedMatcher();
                DMatch[] flannMatches = flannMatcher.Match(descriptors1, descriptors2);
                Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, flannMatches, dst2);
            }
            kp1 = keypoints1;
            kp2 = keypoints2;
        }
Example #2
0
        public static IDictionary <string, MatOfFloat> CreateHashes(IEnumerable <string> pathes, int thumbSize)
        {
            var hashesDict = new ConcurrentDictionary <string, MatOfFloat>();
            var tasks      = new List <Task>();

            foreach (var path in pathes)
            {
                var task = new Task(() =>
                {
                    var sourceMat = new Mat(path);

                    var scale = (double)thumbSize / Max(sourceMat.Width, sourceMat.Height);
                    sourceMat = sourceMat.Resize(new Size(0, 0), scale, scale, InterpolationFlags.Nearest);
                    var gray  = new Mat();


                    Cv2.CvtColor(sourceMat, gray, ColorConversionCodes.BGR2GRAY);

                    var sift = SIFT.Create();

                    var descriptors = new MatOfFloat();

                    Console.WriteLine("Creating hash for " + path);
                    //var keypoints = sift.Detect(gray).Take(KEYPOINTS_NUMBER).ToArray();
                    //sift.Compute(gray, ref keypoints, descriptors);
                    sift.DetectAndCompute(gray, null, out KeyPoint[] keypoints, descriptors);
                    hashesDict.TryAdd(path, descriptors);
                });
                tasks.Add(task);

                task.Start();
            }
            Task.WaitAll(tasks.ToArray());
            return(hashesDict);
        }
Example #3
0
    public KeyPoint[] getKeyPoints(Mat camMat, int nKeyPoints)
    {
        orb = SIFT.Create(nKeyPoints);
        KeyPoint[] keyPoints = orb.Detect(camMat);

        return(keyPoints);
    }
Example #4
0
        private void MatchBySift(Mat src1, Mat src2)
        {
            using var gray1 = new Mat();
            using var gray2 = new Mat();

            Cv2.CvtColor(src1, gray1, ColorConversionCodes.BGR2GRAY);
            Cv2.CvtColor(src2, gray2, ColorConversionCodes.BGR2GRAY);

            using var sift = SIFT.Create();

            // Detect the keypoints and generate their descriptors using SIFT
            KeyPoint[] keypoints1, keypoints2;
            using var descriptors1 = new Mat <float>();
            using var descriptors2 = new Mat <float>();
            sift.DetectAndCompute(gray1, null, out keypoints1, descriptors1);
            sift.DetectAndCompute(gray2, null, out keypoints2, descriptors2);

            // Match descriptor vectors
            using var bfMatcher    = new BFMatcher(NormTypes.L2, false);
            using var flannMatcher = new FlannBasedMatcher();
            DMatch[] bfMatches    = bfMatcher.Match(descriptors1, descriptors2);
            DMatch[] flannMatches = flannMatcher.Match(descriptors1, descriptors2);

            // Draw matches
            using var bfView = new Mat();
            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, bfMatches, bfView);
            using var flannView = new Mat();
            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, flannMatches, flannView);

            using (new Window("SIFT matching (by BFMather)", WindowMode.AutoSize, bfView))
                using (new Window("SIFT matching (by FlannBasedMatcher)", WindowMode.AutoSize, flannView))
                {
                    Cv2.WaitKey();
                }
        }
Example #5
0
 public static Mat GetHomography(Mat mMain, Mat mSecondary)
 {
     KeyPoint[] keypoints  = null;
     KeyPoint[] keypoints2 = null;
     using (SIFT sIFT = SIFT.Create(1000))
     {
         using (Mat mat = new Mat())
         {
             using (Mat mat2 = new Mat())
             {
                 sIFT.DetectAndCompute(mMain, new Mat(), out keypoints, mat);
                 sIFT.DetectAndCompute(mSecondary, new Mat(), out keypoints2, mat2);
                 FlannBasedMatcher flannBasedMatcher = new FlannBasedMatcher();
                 DMatch[]          array             = new DMatch[0];
                 array = flannBasedMatcher.Match(mat, mat2);
                 List <Point2f> list  = new List <Point2f>();
                 List <Point2f> list2 = new List <Point2f>();
                 for (int i = 0; i < array.Length; i++)
                 {
                     list.Add(keypoints[array[i].QueryIdx].Pt);
                     list2.Add(keypoints2[array[i].TrainIdx].Pt);
                 }
                 return(Cv2.FindHomography(InputArray.Create(list2), InputArray.Create(list), HomographyMethods.Ransac));
             }
         }
     }
 }
Example #6
0
 public void DefaultNorm()
 {
     using (var alg = SIFT.Create())
     {
         var defnorm = alg.DefaultNorm;
         Assert.Equal(4, defnorm);
     }
 }
Example #7
0
 public void DescriptorType()
 {
     using (var alg = SIFT.Create())
     {
         var dtype = alg.DescriptorType;
         Assert.Equal(MatType.CV_32F, dtype);
     }
 }
Example #8
0
 public void DescriptorSize()
 {
     using (var alg = SIFT.Create())
     {
         var sz = alg.DescriptorSize;
         Assert.Equal(128, sz);
     }
 }
Example #9
0
        public void Detect()
        {
            KeyPoint[] keyPoints;
            using (var gray = Image("lenna.png", 0))
                using (var alg = SIFT.Create(500))
                    keyPoints = alg.Detect(gray);

            testOutputHelper.WriteLine($"KeyPoint has {keyPoints.Length} items.");
        }
Example #10
0
        public void Detect()
        {
            KeyPoint[] keyPoints = null;
            using (var gray = Image("lenna.png", 0))
                using (var surf = SIFT.Create(500))
                    keyPoints = surf.Detect(gray);

            Console.WriteLine($"KeyPoint has {keyPoints.Length} items.");
        }
Example #11
0
        public static List <System.Drawing.Point> func(Bitmap bitmap1, Bitmap bitmap2)
        {
            //Mat img1 = new Mat(@"roll/0.png", ImreadModes.Unchanged);
            //Mat img2 = new Mat(@"roll/1.png", ImreadModes.Unchanged);
            Mat  img1 = BitmapToMat(bitmap1);
            Mat  img2 = BitmapToMat(bitmap2);
            SIFT sift = SIFT.Create(20);

            //KeyPoint[] k = sift.Detect(img1);
            // Detect the keypoints and generate their descriptors using SIFT
            KeyPoint[] keypoints1, keypoints2;
            var        descriptors1 = new Mat <float>();
            var        descriptors2 = new Mat <float>();

            sift.DetectAndCompute(img1, null, out keypoints1, descriptors1);
            sift.DetectAndCompute(img2, null, out keypoints2, descriptors2);

            // Match descriptor vectors
            var bfMatcher    = new BFMatcher(NormTypes.L2, false);
            var flannMatcher = new FlannBasedMatcher();

            DMatch[] bfMatches    = bfMatcher.Match(descriptors1, descriptors2);
            DMatch[] flannMatches = flannMatcher.Match(descriptors1, descriptors2);

            // Draw matches
            var bfView = new Mat();

            Cv2.DrawMatches(img1, keypoints1, img2, keypoints2, bfMatches, bfView);
            var flannView = new Mat();

            Cv2.DrawMatches(img1, keypoints1, img2, keypoints2, flannMatches, flannView);

            using (new Window("SIFT matching (by BFMather)", bfView))
                using (new Window("SIFT matching (by FlannBasedMatcher)", flannView))
                {
                    Cv2.WaitKey();
                }
            List <System.Drawing.Point> points = new List <System.Drawing.Point>();

            foreach (DMatch match in bfMatches)
            {
                System.Drawing.Point p = new System.Drawing.Point();
                p.X = (int)(keypoints1[match.QueryIdx].Pt.X - keypoints2[match.TrainIdx].Pt.X);
                p.Y = (int)(keypoints1[match.QueryIdx].Pt.Y - keypoints2[match.TrainIdx].Pt.Y);
                points.Add(p);
            }

            return(points);
        }
        public void RunTest()
        {
            using (var descriptorExtractor = SIFT.Create(500))
                //using (var descriptorMatcher = new FlannBasedMatcher(ip, sp))
                using (var descriptorMatcher = new BFMatcher())
                    using (var img = Image("lenna.png"))
                    {
                        KeyPoint[] keypoints;
                        Mat        dictionary;
                        var        tc = new TermCriteria(CriteriaType.MaxIter, 100, 0.001d);
                        using (var bowTrainer = new BOWKMeansTrainer(200, tc, 1, KMeansFlags.PpCenters))
                        {
                            var descriptors = new Mat();
                            descriptorExtractor.DetectAndCompute(img, null, out keypoints, descriptors);

                            Mat featuresUnclustered = new Mat();
                            featuresUnclustered.PushBack(descriptors);
                            featuresUnclustered.ConvertTo(featuresUnclustered, MatType.CV_32F);
                            dictionary = bowTrainer.Cluster(featuresUnclustered);
                        }

                        using (var bowDE = new BOWImgDescriptorExtractor(descriptorExtractor, descriptorMatcher))
                        {
                            bowDE.SetVocabulary(dictionary);

                            try
                            {
                                using (Mat descriptors = new Mat())
                                {
                                    descriptorExtractor.Compute(img, ref keypoints, descriptors);
                                    descriptors.ConvertTo(descriptors, MatType.CV_32F);
                                    bowDE.Compute(img, ref keypoints, descriptors, out var arr);
                                    Console.WriteLine(arr.Length);
                                    Console.WriteLine(arr[0].Length);
                                }
                            }
                            catch (OpenCVException ex)
                            {
                                Console.WriteLine(ex.FileName);
                                Console.WriteLine(ex.FuncName);
                                Console.WriteLine(ex.Line);
                                throw;
                            }
                        }

                        dictionary.Dispose();
                    }
        }
Example #13
0
        public void ComputeImageFeatures()
        {
            using var featuresFinder = SIFT.Create();
            using var image          = Image("abbey_road.jpg", ImreadModes.Grayscale);

            CvDetail.ComputeImageFeatures(featuresFinder, image, out var features);
            using (features)
            {
                Assert.NotNull(features);
                Assert.NotEqual(0, features.ImgIdx);
                Assert.Equal(image.Size(), features.ImgSize);
                Assert.NotEmpty(features.Keypoints);
                Assert.NotNull(features.Descriptors);
                Assert.False(features.Descriptors.Empty());
            }
        }
Example #14
0
        private static void Feature()
        {
            Mat img = new Mat("data/lenna.png", ImreadModes.GrayScale);
            //var feature2d = KAZE.Create();
            var feature2d = SIFT.Create();

            KeyPoint[] keyPoints;
            Mat        descriptors = new Mat();

            feature2d.DetectAndCompute(img, null, out keyPoints, descriptors);

            Mat dst = new Mat();

            Cv2.DrawKeypoints(img, keyPoints, dst);
            Window.ShowImages(dst);
        }
Example #15
0
        public void RunTest()
        {
            using var descriptorExtractor = SIFT.Create(500);
            using var descriptorMatcher   = new BFMatcher();
            using var img = Image("lenna.png");
            KeyPoint[] keypoints;
            Mat        dictionary;
            var        tc = new TermCriteria(CriteriaTypes.MaxIter, 100, 0.001d);

            using (var bowTrainer = new BOWKMeansTrainer(200, tc, 1, KMeansFlags.PpCenters))
            {
                var descriptors = new Mat();
                descriptorExtractor.DetectAndCompute(img, null, out keypoints, descriptors);

                using var featuresUnclustered = new Mat();
                featuresUnclustered.PushBack(descriptors);
                featuresUnclustered.ConvertTo(featuresUnclustered, MatType.CV_32F);
                dictionary = bowTrainer.Cluster(featuresUnclustered);
            }

            using (var bowDe = new BOWImgDescriptorExtractor(descriptorExtractor, descriptorMatcher))
            {
                bowDe.SetVocabulary(dictionary);

                try
                {
                    using var descriptors = new Mat();
                    descriptorExtractor.Compute(img, ref keypoints, descriptors);
                    descriptors.ConvertTo(descriptors, MatType.CV_32F);
                    bowDe.Compute(img, ref keypoints, descriptors, out var arr);
                    testOutputHelper.WriteLine(arr.Length.ToString(CultureInfo.InvariantCulture));
                    testOutputHelper.WriteLine(arr[0].Length.ToString(CultureInfo.InvariantCulture));
                }
                catch (OpenCVException ex)
                {
                    testOutputHelper.WriteLine(ex.FileName);
                    testOutputHelper.WriteLine(ex.FuncName);
                    testOutputHelper.WriteLine(ex.Line.ToString(CultureInfo.InvariantCulture));
                    throw;
                }
            }

            dictionary.Dispose();
        }
Example #16
0
        public override FeatureMatchResult Match(Mat sourceMat, Mat searchMat, FeatureMatchArgument argument)
        {
            //创建SIFT
            using var sift = SIFT.Create();

            //创建特征点描述对象,为下边的特征点匹配做准备
            using var sourceDescriptors = new Mat();
            using var searchDescriptors = new Mat();

            //提取特征点,并进行特征点描述
            sift.DetectAndCompute(sourceMat, null, out var sourceKeyPoints, sourceDescriptors);
            sift.DetectAndCompute(searchMat, null, out var searchKeyPoints, searchDescriptors);

            //创建Brute-force descriptor matcher
            using var bfMatcher = new BFMatcher();
            //对原图特征点描述加入训练
            bfMatcher.Add(new List <Mat>()
            {
                sourceDescriptors
            });
            bfMatcher.Train();
            //获得匹配特征点,并提取最优配对
            var matches = bfMatcher.KnnMatch(sourceDescriptors, searchDescriptors, (int)argument.MatchPoints);

            argument.OutputDebugMessage($"[FeatureMatch] [SIFT] The number of matching points is ({matches.Length}).");

            //即使使用SIFT算法,但此时没有经过点筛选的匹配效果同样糟糕,所进一步获取优秀匹配点
            var goodMatches = SelectGoodMatches(matches, argument, sourceKeyPoints, searchKeyPoints);

            //获取匹配结果
            var matchResult = GetMatchResult(goodMatches, sourceKeyPoints, searchKeyPoints);

            argument.OutputDebugMessage($"[FeatureMatch] [SIFT] The result of the match is ({matchResult.Success}) ({matchResult.MatchItems.Count}).");
            if (matchResult.Success)
            {
                var bestMatch = matchResult.MatchItems[0];
                argument.OutputDebugMessage($"[FeatureMatch] [SIFT] The center point of the best match is ({bestMatch.Point}), and the rect is {bestMatch.Rectangle}.");
            }
            argument.PreviewDebugFeatureMatchResult(matchResult, sourceMat, searchMat, sourceKeyPoints, searchKeyPoints, goodMatches);
            return(matchResult);
        }
Example #17
0
        public void siftcharacterors(Mat src1, Mat src2)
        {
            Mat gray1 = new Mat();
            Mat gray2 = new Mat();

            Cv2.CvtColor(src1, gray1, ColorConversionCodes.BGR2GRAY);
            Cv2.CvtColor(src2, gray2, ColorConversionCodes.BGR2GRAY);

            var siftdemo = SIFT.Create();

            //寻找特征点
            KeyPoint[] keypoints1, keypoints2;
            var        descriptors1 = new MatOfFloat();
            var        descriptors2 = new MatOfFloat();

            siftdemo.DetectAndCompute(gray1, null, out keypoints1, descriptors1);
            siftdemo.DetectAndCompute(gray2, null, out keypoints2, descriptors2);

            var bfMatcher    = new BFMatcher(NormTypes.L2, false);
            var flannMatcher = new FlannBasedMatcher();

            DMatch[] bfMatches    = bfMatcher.Match(descriptors1, descriptors2);
            DMatch[] flannMatches = flannMatcher.Match(descriptors1, descriptors2);

            // Draw matches
            var bfView = new Mat();

            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, bfMatches, bfView);
            var flannView = new Mat();

            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, flannMatches, flannView);

            using (new Window("SIFT matching (by BFMather)", WindowMode.AutoSize, bfView))
            //using (new Window("SIFT matching (by FlannBasedMatcher)", WindowMode.AutoSize, flannView))
            {
                Cv2.WaitKey();
            }
        }
Example #18
0
        public Mat Stitch()
        {
            Mat src1Gray = new Mat();
            Mat src2Gray = new Mat();

            Cv2.CvtColor(src1Color, src1Gray, ColorConversionCodes.BGR2GRAY);
            Cv2.CvtColor(src2Color, src2Gray, ColorConversionCodes.BGR2GRAY);

            // Setting hyperparameters
            int numBestMatch = 10;

            // Detect the keypoints and generate their descriptors using SIFT
            SIFT sift = SIFT.Create();

            KeyPoint[] keypoints1, keypoints2;
            MatOfFloat descriptors1 = new MatOfFloat();
            MatOfFloat descriptors2 = new MatOfFloat();

            sift.DetectAndCompute(src1Gray, null, out keypoints1, descriptors1);
            sift.DetectAndCompute(src2Gray, null, out keypoints2, descriptors2);

            // Matching descriptor vectors with a brute force matcher
            BFMatcher matcher = new BFMatcher();

            DMatch[] matches = matcher.Match(descriptors1, descriptors2);

            // Sort the match points
            Comparison <DMatch> DMatchComparison = delegate(DMatch match1, DMatch match2)
            {
                if (match1 < match2)
                {
                    return(-1);
                }
                else
                {
                    return(1);
                }
            };

            Array.Sort(matches, DMatchComparison);

            // Get the best n match points
            int n = Math.Min(numBestMatch, keypoints1.Length);

            Point2f[] imagePoints1 = new Point2f[n];
            Point2f[] imagePoints2 = new Point2f[n];
            DMatch[]  bestMatches  = new DMatch[n];
            for (int i = 0; i < n; i++)
            {
                imagePoints1[i] = keypoints1[matches[i].QueryIdx].Pt;
                imagePoints2[i] = keypoints2[matches[i].TrainIdx].Pt;
                bestMatches[i]  = matches[i];
            }

            // visiualize match result
            Mat matchImg = new Mat();

            Cv2.DrawMatches(src1Color, keypoints1, src2Color, keypoints2, bestMatches, matchImg, Scalar.All(-1), Scalar.All(-1), null, DrawMatchesFlags.NotDrawSinglePoints);
            using (new OpenCvSharp.Window("SIFT matching", WindowMode.AutoSize, matchImg))
            {
                Cv2.WaitKey();
            }

            // Get homographic matrix that represents a transformation.
            // The size of such matrix is 3x3, which can represents every possible matrix transformation in 2-D plane.
            Mat h**o = Cv2.FindHomography(InputArray.Create <Point2f>(imagePoints2), InputArray.Create <Point2f>(imagePoints1));

            // calculate the transformed location of the second image's conor
            // use this value to calculate the size of result image
            Point2f[] transfromedConors = transfromConors(src2Color.Size(), h**o);

            // make sure the result image is large enough
            double maxWidth  = src1Color.Width;
            double maxHeight = src1Color.Height;

            for (int i = 0; i < 4; i++)
            {
                if (transfromedConors[i].X > maxWidth)
                {
                    maxWidth = transfromedConors[i].X;
                }
                if (transfromedConors[i].Y > maxHeight)
                {
                    maxHeight = transfromedConors[i].Y;
                }
            }
            OpenCvSharp.Size resultSize = new OpenCvSharp.Size(maxWidth, maxHeight);

            // the position that the first image should be copied to in the final result
            int src1StartPositonY = 0;
            int src1StartPositonX = 0;

            // if still some X coordinate is less than 0, do shift operation along x-axis
            bool   shouldShiftX   = false;
            double shiftDistanceX = double.MinValue;

            for (int i = 0; i < 4; i++)
            {
                if (transfromedConors[i].X < 0)
                {
                    shouldShiftX   = true;
                    shiftDistanceX = Math.Max(shiftDistanceX, -transfromedConors[i].X);
                }
            }
            if (shouldShiftX)
            {
                /*
                 * matrix for shifting algong x-axis
                 * 1 0 d
                 * 0 1 0
                 * 0 0 1
                 */
                Mat shiftMatrix = new Mat(3, 3, h**o.Type());
                shiftMatrix.Set <double>(0, 0, 1);
                shiftMatrix.Set <double>(0, 1, 0);
                shiftMatrix.Set <double>(0, 2, shiftDistanceX);
                shiftMatrix.Set <double>(1, 0, 0);
                shiftMatrix.Set <double>(1, 1, 1);
                shiftMatrix.Set <double>(1, 2, 0);
                shiftMatrix.Set <double>(2, 0, 0);
                shiftMatrix.Set <double>(2, 1, 0);
                shiftMatrix.Set <double>(2, 2, 1);
                h**o              = shiftMatrix * h**o;
                resultSize.Width  = resultSize.Width + (int)shiftDistanceX;
                src1StartPositonX = (int)shiftDistanceX;
            }

            // if still some Y coordinate is less than 0, do shift operation along y-axis
            bool   shouldShiftY   = false;
            double shiftDistanceY = double.MinValue;

            for (int i = 0; i < 4; i++)
            {
                if (transfromedConors[i].Y < 0)
                {
                    shouldShiftY   = true;
                    shiftDistanceY = Math.Max(shiftDistanceY, -transfromedConors[i].Y);
                }
            }
            if (shouldShiftY)
            {
                /*
                 * matrix for shifting algong y-axis
                 * 1 0 0
                 * 0 1 d
                 * 0 0 1
                 */
                Mat shiftMatrix = new Mat(3, 3, h**o.Type());
                shiftMatrix.Set <double>(0, 0, 1);
                shiftMatrix.Set <double>(0, 1, 0);
                shiftMatrix.Set <double>(0, 2, 0);
                shiftMatrix.Set <double>(1, 0, 0);
                shiftMatrix.Set <double>(1, 1, 1);
                shiftMatrix.Set <double>(1, 2, shiftDistanceY);
                shiftMatrix.Set <double>(2, 0, 0);
                shiftMatrix.Set <double>(2, 1, 0);
                shiftMatrix.Set <double>(2, 2, 1);
                h**o = shiftMatrix * h**o;
                resultSize.Height = resultSize.Height + (int)shiftDistanceY;
                src1StartPositonY = (int)shiftDistanceY;
            }

            Mat result = new Mat();

            Cv2.WarpPerspective(src2Color, result, h**o, resultSize);
            src1Color.CopyTo(new Mat(result, new OpenCvSharp.Rect(src1StartPositonX, src1StartPositonY, src1Gray.Cols, src1Gray.Rows)));

            return(result);
        }
Example #19
0
        public ConcurrentDictionary <string, MatOfFloat> CalcSiftHashes(IEnumerable <ImageInfo> infos, out Task result, int thumbSize = 100)
        {
            Trace.WriteLine("CalcSiftHashes started");

            EnablePublishingProgress();

            var hashesDict = new ConcurrentDictionary <string, MatOfFloat>();

            var tasks = new List <Task>();

            foreach (ImageInfo info in infos)
            {
                var task = new Task(() =>
                {
                    Thread.CurrentThread.Priority = ThreadPriority.Lowest;

                    //Thread.Sleep(1);

                    //var mem = new MemoryStream();

                    //// copy to byte array
                    //int stride = ((BitmapImage)info.Image).PixelWidth * 4;
                    //byte[] buffer = new byte[stride * ((BitmapImage)info.Image).PixelHeight];
                    //((BitmapImage)info.Image).CopyPixels(buffer, stride, 0);

                    //// create bitmap
                    //Bitmap bitmap = new Bitmap(((BitmapImage)info.Image).PixelWidth, ((BitmapImage)info.Image).PixelHeight, PixelFormat.Format32bppArgb);

                    //// lock bitmap data
                    //BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);

                    //// copy byte array to bitmap data
                    //Marshal.Copy(buffer, 0, bitmapData.Scan0, buffer.Length);

                    //// unlock
                    //bitmap.UnlockBits(bitmapData);

                    //bitmap.Save(mem,ImageFormat.Bmp);


                    //Mat sourceMat = Cv2.ImDecode(mem.GetBuffer(), ImreadModes.Unchanged);

                    //var resizedMat = sourceMat.Resize(new OpenCvSharp.Size(thumbSize, thumbSize), 0, 0, InterpolationFlags.Nearest);

                    //var scale = (double)thumbSize / Max(sourceMat.Width, sourceMat.Height);
                    //var resizedMat = sourceMat.Resize(new OpenCvSharp.Size(0, 0), scale, scale, InterpolationFlags.Nearest);

                    //var grayScaledMat = new Mat();
                    //Cv2.CvtColor(resizedMat, grayScaledMat, ColorConversionCodes.BGR2GRAY);

                    //var siftPoints = SURF.Create(400);
                    SIFT siftPoints = SIFT.Create();

                    MatOfFloat descriptors = new MatOfFloat();

                    //var keypoints = siftPoints.Detect(info.StoredMat).ToArray();
                    //siftPoints.Compute(info.StoredMat, ref keypoints, descriptors);

                    double scale = Math.Min((float)thumbSize / info.StoredMat.Width, (float)thumbSize / info.StoredMat.Height);
                    Mat resized  = info.StoredMat.Resize(new OpenCvSharp.Size(0, 0), scale, scale, InterpolationFlags.Area);
                    siftPoints.DetectAndCompute(resized, null, out KeyPoint[] keypoints, descriptors);
                    resized.Release();

                    if (!ValidateDescriptor(descriptors))
                    {
                        descriptors.Release();
                        descriptors = new MatOfFloat(thumbSize, thumbSize);
                    }


                    hashesDict.TryAdd(info.FilePath, descriptors);

                    //resizedMat?.Dispose();
                    siftPoints.Dispose();
                    //grayScaledMat.Dispose();
                    //resizedMat.Release();
                    //sourceMat.Release();
                    UpdateIterationsCount();
                });

                tasks.Add(task);
            }

            SetProgressIterationsScope(tasks);

            foreach (var task in tasks)
            {
                task.Start();
            }

            result = Task.WhenAll(tasks.ToArray());
            result.ContinueWith(o => DisiblePublishingProgress());

            return(hashesDict);
        }
    void Start()
    {
        //thread = new Thread(new ThreadStart(ThreadMainFunc));
        //thread = new Thread(ThreadMainFunc);
        //thread.IsBackground = true;
        //thread = Loom.RunAsync(ThreadMainFunc);
        //thread.Start();
        //textTex = Resources.Load<Texture2D>("test-IMG_0204-text");
        //rawImageRI.texture = textTex;

        //TextAsset binary = (TextAsset)AssetDatabase.LoadAssetAtPath("Assets/img1.bytes", typeof(TextAsset));

        //inputTex = Resources.Load<Texture2D>("test-IMG_0204");
        //rawImageRI.texture = inputTex;
        //Debug.Log("inputTex.width: "+inputTex.width);
        //Debug.Log("inputTex.height: "+inputTex.height);

        //tex.LoadImage(binary.bytes);
        //Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/img.PNG", typeof(Texture2D));

        //StartCoroutine(GetTextImg());

        //Size texSize = new Size(tex.width, tex.height);
        //Mat mat = new Mat(texSize, CvType.CV_8UC4);
        //Utils.texture2DToMat(tex, mat);

        //inputImg = Cv2.ImRead(imgPath);
        //Cv2.ImShow("inputImg", inputImg);

        //tex = Unity.MatToTexture(inputImg);
        //rawImageRI.texture = tex;

        /*inputTex = new Texture2D(2,2);
         * string imgPath = "../signboard-rectangle/test-IMG_0204.PNG";
         * byte [] binaryImageData = File.ReadAllBytes(imgPath);
         * inputTex.LoadImage(binaryImageData);*/

        //inputTex = Resources.Load<Texture2D>("forAddText");

        //必要 防止記憶體爆炸
        Texture2D inputTex1 = TextureGray.ToGray("1");
        Texture2D inputTex2 = TextureGray.ToGray("2");

        //Texture2D inputTex3 = TextureGray.ToGray("3");
        //Debug.Log("inputTex.width: "+inputTex.width);
        //Debug.Log("inputTex.height: "+inputTex.height);

        //rawImageRI.texture = inputTex;

        OpenCvSharp.Mat inputImg1 = Unity.TextureToMat(inputTex1);
        OpenCvSharp.Mat inputImg2 = Unity.TextureToMat(inputTex2);
        //OpenCvSharp.Mat inputImg3 = Unity.TextureToMat(inputTex3);
        //OpenCvSharp.Mat inputImg2 = Unity.TextureToMat(inputTex);
        //Cv2.ImShow("img", inputImg);

        InputArray img1 = InputArray.Create(inputImg1);
        InputArray img2 = InputArray.Create(inputImg2);

        //InputArray img3 = InputArray.Create(inputImg3);
        //Debug.Log("inputImg: "+inputImg.ToString());
        //InputArray mask = null;
        //OpenCvSharp.KeyPoint[] kp1 = null;
        des1 = OutputArray.Create(inputImg1);
        des2 = OutputArray.Create(inputImg2);
        //des3 = OutputArray.Create(inputImg3);
        //Debug.Log("des1: "+des1);

        //Initiate SIFT detector and extractor
        //siftDetect = FeatureDetector.create(3);
        //siftExtract = DescriptorExtractor.create(1);

        sift = SIFT.Create();
        //surf = SURF.Create((double)100);
        //orb = OpenCvSharp.ORB.Create();
        //brief = OpenCvSharp.XFeatures2D.BriefDescriptorExtractor.Create();

        //if image too large will cause app Terminated due to memory error
        kp1 = sift.Detect(inputImg1);
        kp2 = sift.Detect(inputImg2);
        //kp3 = sift.Detect(inputImg3);
        //kp1 = surf.Detect(inputImg);
        //kp1 = orb.Detect(inputImg);
        //kp1 = brief.Detect(inputImg);

        //Cv2.ImShow("img", inputImg); ok

        sift.Compute(img1, ref kp1, des1);
        sift.Compute(img2, ref kp2, des2);
        //sift.Compute(img3, ref kp3, des3);
        //surf.Compute(img1, ref kp1, des1);
        //orb.Compute(img1, ref kp1, des1);
        //brief.Compute(img1, ref kp1, des1);

        //Cv2.ImShow("img", inputImg); 亂碼圖
        //Cv2.ImShow("img", inputImg2); ok

        //foreach (OpenCvSharp.KeyPoint kp in kp1)
        // Debug.Log("kp: "+kp.ToString());

        //用flannbased的話unity會掛掉
        descriptorMatcher = OpenCvSharp.DescriptorMatcher.Create("BruteForce");

        //sift.DetectAndCompute(img1, mask, out kp1, des1);

        //MatOfKeyPoint kp1 = new MatOfKeyPoint();
        //Mat des1 = new Mat();
        //siftDetect.detect(inputImg, kp1);
        //siftExtract.compute(inputImg, kp1, des1);

        //StartCoroutine(OpenCamera());//開啟攝影機鏡頭
        //StartCoroutine(CalculateHomography());

        /*Texture2D sourceTex = ScreenCapture.CaptureScreenshotAsTexture();
         * Color[] pix = sourceTex.GetPixels((int)rectBotLeft.x, (int)rectBotLeft.y, width, height);
         * tex = new Texture2D(width, height);
         * tex.SetPixels(pix);
         * tex.Apply();
         *
         * tex = TextureGray.ToGray(tex);
         *
         * mat = Unity.TextureToMat(tex);
         *
         * InputArray img2 = InputArray.Create(mat);
         * desCam = OutputArray.Create(mat);
         *
         * kpCam = sift.Detect(mat);*/
    }
Example #21
0
        /// <summary>
        /// Sift match
        /// </summary>
        /// <param name="wantBitmap">Want match bitmap</param>
        /// <param name="bitmap">target bitmap</param>
        /// <param name="cancellationToken">cancellationToken</param>
        /// <returns>Target bitmap location</returns>
        private async Task <Rectangle?> SiftMatchLocation(Bitmap wantBitmap, Bitmap bitmap,
                                                          CancellationToken cancellationToken)
        {
            return(await Task.Run(() =>
            {
                try
                {
                    using var matSrc = bitmap.ToMat();
                    using var matTo = wantBitmap.ToMat();
                    using var matSrcRet = new Mat();
                    using var matToRet = new Mat();

                    cancellationToken.ThrowIfCancellationRequested();

                    KeyPoint[] keyPointsSrc, keyPointsTo;
                    using (var sift = SIFT.Create())
                    {
                        sift.DetectAndCompute(matSrc, null, out keyPointsSrc, matSrcRet);
                        sift.DetectAndCompute(matTo, null, out keyPointsTo, matToRet);
                    }

                    cancellationToken.ThrowIfCancellationRequested();

                    using (var bfMatcher = new BFMatcher())
                    {
                        var matches = bfMatcher.KnnMatch(matSrcRet, matToRet, k: 2);

                        cancellationToken.ThrowIfCancellationRequested();

                        var pointsSrc = new List <Point2f>();
                        var pointsDst = new List <Point2f>();
                        foreach (var items in matches.Where(x => x.Length > 1))
                        {
                            if (items[0].Distance < 0.5 * items[1].Distance)
                            {
                                pointsSrc.Add(keyPointsSrc[items[0].QueryIdx].Pt);
                                pointsDst.Add(keyPointsTo[items[0].TrainIdx].Pt);
                            }
                        }

                        if (pointsSrc.Count > 0 && pointsDst.Count > 0)
                        {
                            var location = pointsSrc[0] - pointsDst[0];

                            var rectangle =
                                new Rectangle?(new Rectangle((int)location.X, (int)location.Y, wantBitmap.Width,
                                                             wantBitmap.Height));
                            WindowsApi.WriteLog(
                                $"{nameof(SiftMatchLocation)} match success, match count:{pointsSrc.Count}, {rectangle}");

                            return rectangle;
                        }
                        else
                        {
                            WindowsApi.WriteLog(
                                $"{nameof(SiftMatchLocation)} match failed");
                        }
                    }
                }
                catch (Exception ex)
                {
                    WindowsApi.WriteLog($"{nameof(SiftMatchLocation)} ErrorMessage:{ex.Message}");
                }

                return null;
            }, cancellationToken));
        }
Example #22
0
        public void CreateAndDispose()
        {
            var surf = SIFT.Create(400);

            surf.Dispose();
        }