一千萬個為什麽

搜索

Emgu CV - 我如何在Image中獲得所有模式

嗨已經有功能解決方案但有一個問題:

           //The screenshot will be stored in this bitmap.
            Bitmap capture = new Bitmap(rec.Width, rec.Height, PixelFormat.Format24bppRgb);
            using (Graphics g = Graphics.FromImage(capture))
            {
                g.CopyFromScreen(rec.Location, new System.Drawing.Point(0, 0), rec.Size);
            }

            MCvSURFParams surfParam = new MCvSURFParams(500, false);
            SURFDetector surfDetector = new SURFDetector(surfParam);

           //Template image 
            Image modelImage = new Image("template.jpg");
           //Extract features from the object image
            ImageFeature[] modelFeatures = surfDetector.DetectFeatures(modelImage, null);

           //Prepare current frame
            Image observedImage = new Image(capture);
            ImageFeature[] imageFeatures = surfDetector.DetectFeatures(observedImage, null);


           //Create a SURF Tracker using k-d Tree
            Features2DTracker tracker = new Features2DTracker(modelFeatures);

            Features2DTracker.MatchedImageFeature[] matchedFeatures = tracker.MatchFeature(imageFeatures, 2);
            matchedFeatures = Features2DTracker.VoteForUniqueness(matchedFeatures, 0.8);
            matchedFeatures = Features2DTracker.VoteForSizeAndOrientation(matchedFeatures, 1.5, 20);
            HomographyMatrix homography = Features2DTracker.GetHomographyMatrixFromMatchedFeatures(matchedFeatures);

           //Merge the object image and the observed image into one image for display
            Image res = modelImage.ConcateVertical(observedImage);

            #region draw lines between the matched features

            foreach (Features2DTracker.MatchedImageFeature matchedFeature in matchedFeatures)
            {
                PointF p = matchedFeature.ObservedFeature.KeyPoint.Point;
                p.Y += modelImage.Height;
                res.Draw(new LineSegment2DF(matchedFeature.SimilarFeatures[0].Feature.KeyPoint.Point, p), new Gray(0), 1);
            }

            #endregion

            #region draw the project region on the image

            if (homography != null)
            {
               //draw a rectangle along the projected model
                Rectangle rect = modelImage.ROI;
                PointF[] pts = new PointF[] { 
                    new PointF(rect.Left, rect.Bottom),
                    new PointF(rect.Right, rect.Bottom),
                    new PointF(rect.Right, rect.Top),
                    new PointF(rect.Left, rect.Top)
                };

                homography.ProjectPoints(pts);

                for (int i = 0; i < pts.Length; i++)
                    pts[i].Y += modelImage.Height;

                res.DrawPolyline(Array.ConvertAll(pts, Point.Round), true, new Gray(255.0), 2);
            }

            #endregion

            pictureBoxScreen.Image = res.ToBitmap();

結果是:

enter image description here

我的問題是,函數 homography.ProjectPoints(pts); 僅獲取第一次出現的圖案(上圖中的白色矩形)

我怎麽能投影所有出現的模板,分別如何在圖像中出現模板矩形

最佳答案

在我的碩士論文中,我面臨著與你類似的問題。基本上你有兩個選擇:

  1. 使用群集,例如分層k-means 或點密度,如 DBSCAN (它取決於兩個參數,但你可以使它達到閾值免費的二維R ^ 2空間)
  2. 使用多個健壯的模型擬合估算技術,例如 JLinkage 。在這種更先進的技術中,您聚集了共享單應性的點,而不是在歐幾裏德空間中彼此接近的聚類點。

在“群集”中對匹配進行分區後,您可以估計屬於對應群集的匹配之間的單應性。

轉載註明原文: Emgu CV - 我如何在Image中獲得所有模式