使用 OpenCV (Emgu) 的 PGH

发布于 2025-01-04 05:29:13 字数 2401 浏览 1 评论 0原文

有人可以分享他们计算 PGH(成对几何直方图)相似度的代码吗?我需要从图像列表中找到最相似的对象。

我写了下面的代码,但结果没有任何意义。我敢打赌我犯了一个愚蠢的错误,而且我被困住了。

有什么建议吗?

 public double GetBestPGHMatch(Contour<Point> currContour, List<Contour<Point>> ContoursList)
    {
        double match = -1.0d;
        DenseHistogram histCurrContour = new DenseHistogram(
                                                       new int[2]
                                                                {
                                                                    currContour.Total,
                                                                    currContour.Total,
                                                                },
                                                        new RangeF[2]
                                                                {
                                                                    new RangeF(0, 100),
                                                                    new RangeF(0, 100)
                                                                }
                                                   );

        CvInvoke.cvCalcPGH(currContour.Ptr, histCurrContour.Ptr);
        foreach (Contour<Point> contour in ContoursList)
        {
            DenseHistogram hist = new DenseHistogram(
                                              new int[2]
                                                                {
                                                                    currContour.Total,
                                                                    currContour.Total,
                                                                },
                                                new RangeF[2]
                                                                {
                                                                    new RangeF(0, 100),
                                                                    new RangeF(0, 100)
                                                                }
                                          );

            CvInvoke.cvCalcPGH(contour.Ptr, hist.Ptr);
            double c = CvInvoke.cvCompareHist(histCurrContour.Ptr, hist.Ptr, Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD.CV_COMP_CORREL);
            if (c > match) match = c;
        }

        return match;
    }

Could anyone share their code for calculating the PGH (Pairwise Geometric Histogram) similarity ? I need to find the most similar object from within a list of images.

I have written up the following code, but the results make no sense. I'd bet I am doing a silly error, and I am stuck.

Any suggestions?

 public double GetBestPGHMatch(Contour<Point> currContour, List<Contour<Point>> ContoursList)
    {
        double match = -1.0d;
        DenseHistogram histCurrContour = new DenseHistogram(
                                                       new int[2]
                                                                {
                                                                    currContour.Total,
                                                                    currContour.Total,
                                                                },
                                                        new RangeF[2]
                                                                {
                                                                    new RangeF(0, 100),
                                                                    new RangeF(0, 100)
                                                                }
                                                   );

        CvInvoke.cvCalcPGH(currContour.Ptr, histCurrContour.Ptr);
        foreach (Contour<Point> contour in ContoursList)
        {
            DenseHistogram hist = new DenseHistogram(
                                              new int[2]
                                                                {
                                                                    currContour.Total,
                                                                    currContour.Total,
                                                                },
                                                new RangeF[2]
                                                                {
                                                                    new RangeF(0, 100),
                                                                    new RangeF(0, 100)
                                                                }
                                          );

            CvInvoke.cvCalcPGH(contour.Ptr, hist.Ptr);
            double c = CvInvoke.cvCompareHist(histCurrContour.Ptr, hist.Ptr, Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD.CV_COMP_CORREL);
            if (c > match) match = c;
        }

        return match;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此生挚爱伱 2025-01-11 05:29:13

希望这对某人有帮助,这就是我的计算方法,我使用的是 Bhattacharya 距离,因此值越大,匹配度越低。还有其他指标,但我发现 B 距离最适合我的需求。

 public double pghMatchShape(Contour<Point> shape1, Contour<Point> shape2)
    {
        DenseHistogram hist1 = new DenseHistogram(
           new int[2] { 8, 8 },
           new RangeF[2] { new RangeF(-180, 180), new RangeF(100, 100) });
        DenseHistogram hist2 = new DenseHistogram(
           new int[2] { 8, 8 },
           new RangeF[2] { new RangeF(-180, 180), new RangeF(100, 100) });
        CvInvoke.cvCalcPGH(shape1, hist1.Ptr);
        CvInvoke.cvCalcPGH(shape2, hist2.Ptr);
        CvInvoke.cvNormalizeHist(hist1.Ptr, 100.0);
        CvInvoke.cvNormalizeHist(hist2.Ptr, 100.0);
        double corr = CvInvoke.cvCompareHist(hist1, hist2, HISTOGRAM_COMP_METHOD.CV_COMP_BHATTACHARYYA);

        return corr;
    }

Hope this helps someone, here is how I worked it out, I am using Bhattacharya distance, hence the larger the value, the lower the match. There are other metrics as well, but I found B-distance best for my needs.

 public double pghMatchShape(Contour<Point> shape1, Contour<Point> shape2)
    {
        DenseHistogram hist1 = new DenseHistogram(
           new int[2] { 8, 8 },
           new RangeF[2] { new RangeF(-180, 180), new RangeF(100, 100) });
        DenseHistogram hist2 = new DenseHistogram(
           new int[2] { 8, 8 },
           new RangeF[2] { new RangeF(-180, 180), new RangeF(100, 100) });
        CvInvoke.cvCalcPGH(shape1, hist1.Ptr);
        CvInvoke.cvCalcPGH(shape2, hist2.Ptr);
        CvInvoke.cvNormalizeHist(hist1.Ptr, 100.0);
        CvInvoke.cvNormalizeHist(hist2.Ptr, 100.0);
        double corr = CvInvoke.cvCompareHist(hist1, hist2, HISTOGRAM_COMP_METHOD.CV_COMP_BHATTACHARYYA);

        return corr;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文