首页 > 解决方案 > 使用特征检测时如何获取对象的位置?

问题描述

我是 Emgucv (c#) 的新程序员,我需要一些帮助。

我尝试使用特征检测并遵循示例(Emgu->emgucv-windesktop 4.0.1.3373 -> Emgu.CV.Example -> FeatureMatching)。它使用 Kaze 检测器来寻找特征。

我运行了这个例子,结果很好。(可以检测特征并给出特征区域)

但是,我尝试更改图像(新模型图像并观察从模型图像中裁剪的图像)

新结果可以检测到特征,但特征区域不正确。

我尝试同时使用彩色图像和灰度图像(已经更改 ImreadModes.ImageType )。

该区域仍然不正确。

那么,有人可以给我指导或解决方案吗?

    /// <summary>
    /// Draw the model image and observed image, the matched features and homography projection.
    /// </summary>
    /// <param name="modelImage">The model image</param>
    /// <param name="observedImage">The observed image</param>
    /// <param name="matchTime">The output total time for computing the homography matrix.</param>
    /// <returns>The model image and observed image, the matched features and homography projection.</returns>
    public static Mat Draw(Mat modelImage, Mat observedImage, out long matchTime)
    {
        Mat homography;
        VectorOfKeyPoint modelKeyPoints;
        VectorOfKeyPoint observedKeyPoints;
        using (VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch())
        {
            Mat mask;
            FindMatch(modelImage, observedImage, out matchTime, out modelKeyPoints, out observedKeyPoints, matches,
               out mask, out homography);

            //Draw the matched keypoints
            Mat result = new Mat();
            Features2DToolbox.DrawMatches(modelImage, modelKeyPoints, observedImage, observedKeyPoints,
               matches, result, new MCvScalar(255, 255, 255), new MCvScalar(255, 255, 255), mask);

            #region draw the projected region on the image

            if (homography != null)
            {
                //draw a rectangle along the projected model
                Rectangle rect = new Rectangle(Point.Empty, modelImage.Size);
                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)
                };
                pts = CvInvoke.PerspectiveTransform(pts, homography);

                Point[] points = new Point[pts.Length];
                for (int i = 0; i < points.Length; i++)
                    points[i] = Point.Round(pts[i]);

                using (VectorOfPoint vp = new VectorOfPoint(points))
                {
                    CvInvoke.Polylines(result, vp, true, new MCvScalar(255, 0, 0, 255), 5);
                }
            }
            #endregion

            return result;

        }
    }
}

}

示例结果:https ://www.img.in.th/image/E9RweT

灰度图结果:https ://www.img.in.th/image/E9RmQQ

彩色图像结果:https ://www.img.in.th/image/E9RlFJ

标签: c#image-processingemgucvaforge

解决方案


推荐阅读