示例#1
0
        public void RemoveInliersFromKeypointsAndDescriptors(VectorOfDMatch inliers, ref VectorOfKeyPoint keypointsQueryImageInOut, ref Mat descriptorsQueryImageInOut)
        {
            List <int> inliersKeypointsPositions = new List <int>();

            for (int inliersIndex = 0; inliersIndex < inliers.Size; ++inliersIndex)
            {
                MDMatch match = inliers[inliersIndex];
                inliersKeypointsPositions.Add(match.QueryIdx);
            }

            inliersKeypointsPositions.Sort();

            VectorOfKeyPoint keypointsQueryImageBackup = null;

            keypointsQueryImageBackup = keypointsQueryImageInOut;
            keypointsQueryImageInOut  = new VectorOfKeyPoint();
            Mat filteredDescriptors = new Mat();

            for (int rowIndex = 0; rowIndex < descriptorsQueryImageInOut.Rows; ++rowIndex)
            {
                if (!inliersKeypointsPositions.Exists(i => i == rowIndex))
                {
                    keypointsQueryImageInOut.Push(new MKeyPoint[] { keypointsQueryImageBackup[rowIndex] });

                    Matrix <float> matrix = new Matrix <float>(descriptorsQueryImageInOut.Size);
                    descriptorsQueryImageInOut.ConvertTo(matrix, Emgu.CV.CvEnum.DepthType.Cv32F);
                    var linha = matrix.GetRow(rowIndex).Mat;
                    filteredDescriptors.PushBack(linha);
                }
            }
            filteredDescriptors.CopyTo(descriptorsQueryImageInOut);
        }
示例#2
0
 /// <summary>
 /// Get the item in the specific index
 /// </summary>
 /// <param name="index">The index</param>
 /// <returns>The item in the specific index</returns>
 public MDMatch this[int index]
 {
     get
     {
         MDMatch result = new MDMatch();
         VectorOfDMatchGetItem(_ptr, index, ref result);
         return(result);
     }
 }
示例#3
0
 /// <summary>
 /// Convert the standard vector to an array of DMatch
 /// </summary>
 /// <returns>An array of DMatch</returns>
 public MDMatch[] ToArray()
 {
     MDMatch[] res = new MDMatch[Size];
     if (res.Length > 0)
     {
         GCHandle handle = GCHandle.Alloc(res, GCHandleType.Pinned);
         CvInvoke.VectorOfDMatchCopyData(_ptr, handle.AddrOfPinnedObject());
         handle.Free();
     }
     return(res);
 }
示例#4
0
        public String GetLabelFromMatches(VectorOfVectorOfDMatch vDMatch, Mat uniqueMask)
        {
            Dictionary <String, int> labelCount = new Dictionary <string, int>();

            for (int i = 0; i < vDMatch.Size; i++)
            {
                // Not need to use uniqueMask because we have multiple train feature for one class
                //if (uniqueMask.GetData(i)[0] == 0) continue;

                VectorOfDMatch vMatch = vDMatch[i];
                for (int j = 0; j < vMatch.Size; j++)
                {
                    MDMatch dmatch = vMatch[j];
                    //sb.Append("\n\t" + JsonConvert.SerializeObject(dmatch) + " " + );
                    String label = GetLabel(dmatch.ImgIdx);
                    if (labelCount.ContainsKey(label))
                    {
                        labelCount[label] += 1;
                    }
                    else
                    {
                        labelCount[label] = 1;
                    }
                }
            }
            String ret = labelCount.Keys.Aggregate((i, j) => labelCount[i] >= labelCount[j] ? i : j);
            // Scan to find best imgIndex
            Dictionary <int, int> imgIndexCount = new Dictionary <int, int>();

            for (int i = 0; i < vDMatch.Size; i++)
            {
                VectorOfDMatch vMatch = vDMatch[i];
                for (int j = 0; j < vMatch.Size; j++)
                {
                    MDMatch dmatch = vMatch[j];
                    if (GetLabel(dmatch.ImgIdx) == ret)
                    {
                        if (imgIndexCount.ContainsKey(dmatch.ImgIdx))
                        {
                            imgIndexCount[dmatch.ImgIdx] += 1;
                        }
                        else
                        {
                            imgIndexCount[dmatch.ImgIdx] = 1;
                        }
                    }
                }
            }
            int imgIndex = imgIndexCount.Keys.Aggregate((i, j) => imgIndexCount[i] >= imgIndexCount[j] ? i : j);

            Console.WriteLine("imgIndex=" + imgIndex);
            lastMatchFeatureData = this[imgIndex];
            return(ret);
        }
	   /// <summary>
      /// Convert the standard vector to arrays of int
      /// </summary>
      /// <returns>Arrays of int</returns>
      public MDMatch[][] ToArrayOfArray()
      {
         int size = Size;
         MDMatch[][] res = new MDMatch[size][];
         for (int i = 0; i < size; i++)
         {
            using (VectorOfDMatch v = this[i])
            {
               res[i] = v.ToArray();
            }
         }
         return res;
      }
示例#6
0
        /// <summary>
        /// Convert the standard vector to arrays of int
        /// </summary>
        /// <returns>Arrays of int</returns>
        public MDMatch[][] ToArrayOfArray()
        {
            int size = Size;

            MDMatch[][] res = new MDMatch[size][];
            for (int i = 0; i < size; i++)
            {
                using (GpuMat v = this[i])
                {
                    res[i] = v.ToArray();
                }
            }
            return(res);
        }
示例#7
0
        public VectorOfKeyPoint GetInliersKeypoints()
        {
            if (_inliersKeyPoints.Size == 0)
            {
                for (int i = 0; i < _inliers.Size; ++i)
                {
                    MDMatch match = _inliers[i];

                    if (match.QueryIdx < _keypointsEvalImag.Size)
                    {
                        _inliersKeyPoints.Push(new MKeyPoint[] { _keypointsEvalImag[match.QueryIdx] });
                    }
                }
            }
            return(_inliersKeyPoints);
        }
示例#8
0
        public String MatchesToString(VectorOfVectorOfDMatch vDMatch, String indent = "")
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(indent + "[VectorOfVectorOfDMatch Size=" + vDMatch.Size);
            for (int i = 0; i < vDMatch.Size; i++)
            {
                VectorOfDMatch vMatch = vDMatch[i];
                for (int j = 0; j < vMatch.Size; j++)
                {
                    MDMatch dmatch = vMatch[j];
                    sb.Append("\n\t" + JsonConvert.SerializeObject(dmatch) + " " + GetLabel(dmatch.ImgIdx));
                }
                //sb.Append("\n" + ToString(vDMatch[i], indent + "\t"));
            }
            sb.Append("\n" + indent + "]");
            return(sb.ToString());
        }
示例#9
0
        private void FindMinMaxDistInMatches(MDMatch[][] matchesArray, ref float ms_MAX_DIST, ref float ms_MIN_DIST)
        {
            for (int i = 0; i < matchesArray.Length; i++)
            {
                if (matchesArray[i].Length == 0)
                {
                    continue;
                }

                MDMatch first = matchesArray[i][0];
                float   dist1 = matchesArray[i][0].Distance;

                if (ms_MAX_DIST < dist1)
                {
                    ms_MAX_DIST = dist1;
                }

                if (ms_MIN_DIST > dist1)
                {
                    ms_MIN_DIST = dist1;
                }
            }
        }
示例#10
0
        public void TestThreeViewsCorrespondences()
        {
            MKeyPoint[] kps1 = new MKeyPoint[]
            {
                new MKeyPoint()
                {
                    Point = new PointF(0, 0)
                },
                new MKeyPoint()
                {
                    Point = new PointF(5, 2)
                },
                new MKeyPoint()
                {
                    Point = new PointF(5, 7)
                },
                new MKeyPoint()
                {
                    Point = new PointF(2, 1)
                },
                new MKeyPoint()
                {
                    Point = new PointF(-4, 2)
                },
            };

            MKeyPoint[] kps2a = new MKeyPoint[]
            {
                new MKeyPoint()
                {
                    Point = new PointF(0, 2)
                },
                new MKeyPoint()
                {
                    Point = new PointF(9, 0)
                },
                new MKeyPoint()
                {
                    Point = new PointF(9, -4)
                },
                new MKeyPoint()
                {
                    Point = new PointF(6, 1)
                },
                new MKeyPoint()
                {
                    Point = new PointF(-3, -3)
                },
            };

            MKeyPoint[] kps2b = new MKeyPoint[]
            {
                new MKeyPoint()
                {
                    Point = new PointF(0, 2)
                },
                new MKeyPoint()
                {
                    Point = new PointF(9, 0)
                },
                new MKeyPoint()
                {
                    Point = new PointF(9, -4)
                },
                new MKeyPoint()
                {
                    Point = new PointF(6, 1)
                },
                new MKeyPoint()
                {
                    Point = new PointF(-3, -3)
                },
            };

            MKeyPoint[] kps3 = new MKeyPoint[]
            {
                new MKeyPoint()
                {
                    Point = new PointF(0, 4)
                },
                new MKeyPoint()
                {
                    Point = new PointF(11, 2)
                },
                new MKeyPoint()
                {
                    Point = new PointF(11, -6)
                },
                new MKeyPoint()
                {
                    Point = new PointF(8, 1)
                },
                new MKeyPoint()
                {
                    Point = new PointF(-7, 0)
                },
            };

            MDMatch[] matches12 = new MDMatch[4]
            {
                new MDMatch {
                    QueryIdx = 1, TrainIdx = 0
                },
                new MDMatch {
                    QueryIdx = 0, TrainIdx = 3
                },
                new MDMatch {
                    QueryIdx = 2, TrainIdx = 1
                },
                new MDMatch {
                    QueryIdx = 4, TrainIdx = 2
                },
            };

            MDMatch[] matches23 = new MDMatch[4]
            {
                new MDMatch {
                    QueryIdx = 0, TrainIdx = 4
                },
                new MDMatch {
                    QueryIdx = 1, TrainIdx = 2
                },
                new MDMatch {
                    QueryIdx = 4, TrainIdx = 0
                },
                new MDMatch {
                    QueryIdx = 2, TrainIdx = 1
                },
            };

            MatchingResult match12 = new MatchingResult()
            {
                LeftKps  = kps1,
                RightKps = kps2a,
                Matches  = new Emgu.CV.Util.VectorOfDMatch(matches12)
            };
            MatchingResult match23 = new MatchingResult()
            {
                LeftKps  = kps2b,
                RightKps = kps3,
                Matches  = new Emgu.CV.Util.VectorOfDMatch(matches23)
            };

            List <Correspondences.MatchPair> correspondences = Correspondences.FindCorrespondences12to23(match12, match23);

            Assert.AreEqual(3, correspondences.Count);
            // They are sorted same as matches23
            var c = correspondences[0];

            Assert.AreEqual(1, c.Match12.QueryIdx);
            Assert.AreEqual(0, c.Match12.TrainIdx);
            Assert.AreEqual(0, c.Match23.QueryIdx);
            Assert.AreEqual(4, c.Match23.TrainIdx);

            Assert.AreEqual(kps1[1].Point, c.Kp1.Point);
            Assert.AreEqual(kps2a[0].Point, c.Kp2.Point);
            Assert.AreEqual(kps3[4].Point, c.Kp3.Point);

            c = correspondences[1];
            Assert.AreEqual(2, c.Match12.QueryIdx);
            Assert.AreEqual(1, c.Match12.TrainIdx);
            Assert.AreEqual(1, c.Match23.QueryIdx);
            Assert.AreEqual(2, c.Match23.TrainIdx);

            c = correspondences[2];
            Assert.AreEqual(4, c.Match12.QueryIdx);
            Assert.AreEqual(2, c.Match12.TrainIdx);
            Assert.AreEqual(2, c.Match23.QueryIdx);
            Assert.AreEqual(1, c.Match23.TrainIdx);
        }
示例#11
0
 internal static extern void VectorOfDMatchGetItem(IntPtr vec, int index, ref MDMatch element);
示例#12
0
 public static String ToString(MDMatch dMatch, String indent = "")
 {
     return(indent + JsonConvert.SerializeObject(dMatch));
 }
示例#13
0
        void NewORBDetector()
        {
            float ms_MIN_RATIO = 100;
            float ms_MAX_DIST  = 100;

            (Image <Bgr, byte> Image, VectorOfKeyPoint Keypoints, Mat Descriptors)_imgModel = (new Image <Bgr, byte>(@"C:\Images\ImgModel.jpg").Resize(0.2, Inter.Area), new VectorOfKeyPoint(), new Mat());
            (Image <Bgr, byte> Image, VectorOfKeyPoint Keypoints, Mat Descriptors)_imgTest  = (new Image <Bgr, byte>(@"C:\Images\ImgTest.jpg").Resize(0.2, Inter.Area), new VectorOfKeyPoint(), new Mat());
            Mat imgKeypointsModel                      = new Mat();
            Mat imgKeypointsTest                       = new Mat();
            Mat imgMatches                             = new Mat();
            Mat imgWarped                              = new Mat();
            VectorOfVectorOfDMatch matches             = new VectorOfVectorOfDMatch();
            VectorOfVectorOfDMatch filteredMatches     = new VectorOfVectorOfDMatch();
            List <MDMatch[]>       filteredMatchesList = new List <MDMatch[]>();

            ORBDetector _ORB       = new ORBDetector();
            BFMatcher   _BFMatcher = new BFMatcher(DistanceType.Hamming2);

            _ORB.DetectAndCompute(_imgModel.Image, null, _imgModel.Keypoints, _imgModel.Descriptors, false);
            _ORB.DetectAndCompute(_imgTest.Image, null, _imgTest.Keypoints, _imgTest.Descriptors, false);

            _BFMatcher.Add(_imgModel.Descriptors);
            _BFMatcher.KnnMatch(_imgTest.Descriptors, matches, k: 2, mask: null);

            MDMatch[][] matchesArray = matches.ToArrayOfArray();

            //Apply ratio test
            for (int i = 0; i < matchesArray.Length; i++)
            {
                MDMatch first = matchesArray[i][0];
                float   dist1 = matchesArray[i][0].Distance;
                float   dist2 = matchesArray[i][1].Distance;

                if (dist1 < ms_MIN_RATIO * dist2)
                {
                    filteredMatchesList.Add(matchesArray[i]);
                }
            }

            //Filter by threshold
            MDMatch[][] defCopy = new MDMatch[filteredMatchesList.Count][];
            filteredMatchesList.CopyTo(defCopy);
            filteredMatchesList = new List <MDMatch[]>();

            foreach (var item in defCopy)
            {
                if (item[0].Distance < ms_MAX_DIST)
                {
                    filteredMatchesList.Add(item);
                }
            }

            filteredMatches = new VectorOfVectorOfDMatch(filteredMatchesList.ToArray());


            Mat homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(_imgModel.Keypoints, _imgTest.Keypoints, filteredMatches, null, 10);

            CvInvoke.WarpPerspective(_imgTest.Image, imgWarped, homography, _imgTest.Image.Size);

            Features2DToolbox.DrawKeypoints(_imgModel.Image, _imgModel.Keypoints, imgKeypointsModel, new Bgr(0, 0, 255));
            Features2DToolbox.DrawKeypoints(_imgTest.Image, _imgTest.Keypoints, imgKeypointsTest, new Bgr(0, 0, 255));
            Features2DToolbox.DrawMatches(_imgModel.Image, _imgModel.Keypoints, _imgTest.Image, _imgTest.Keypoints, filteredMatches, imgMatches, new MCvScalar(0, 255, 0), new MCvScalar(0, 0, 255), null, Features2DToolbox.KeypointDrawType.Default);

            Task.Factory.StartNew(() => Emgu.CV.UI.ImageViewer.Show(imgKeypointsModel, "Keypoints Model"));
            Task.Factory.StartNew(() => Emgu.CV.UI.ImageViewer.Show(imgKeypointsTest, "Keypoints Test"));
            Task.Factory.StartNew(() => Emgu.CV.UI.ImageViewer.Show(imgMatches, "Matches"));
            Task.Factory.StartNew(() => Emgu.CV.UI.ImageViewer.Show(imgWarped, "Warp"));
        }