找到blob最大长度的最快方法

时间:2014-05-27 10:48:43

标签: image-processing aforge

使用BlobCounterBase.GetObjectInformation,我可以从图像中检索斑点。找到blob中最大长度的最快方法是什么?它不仅仅是blob的Rectangle的斜边。

我可以提取所有边缘点(使用GetBlobsEdgePoints方法),计算每对边缘点之间的长度并找到最大长度。但是这种方法很慢,特别是在处理可能有数千个斑点的图像时。

1 个答案:

答案 0 :(得分:0)

这是一个适用于我的应用程序的解决方案(再次感谢Roger Rowland提出的导致解决方案的建议):

    private double GetGreatestLength(Blob blob)
    {
        try
        {
            GrahamConvexHull hullFinder = new GrahamConvexHull();

            var hullPoints = hullFinder.FindHull(_blobCounter.GetBlobsEdgePoints(blob));
            var maxPoints = GetMaxPoints(hullPoints);

            return maxPoints.distance * _micronsPerPixel;
        }
        catch (Exception)
        {
            return 0;
        }
    }

    struct MaxPoints
    {
        public IntPoint firstPoint;
        public IntPoint secondPoint;
        public double distance;

        public MaxPoints(IntPoint first, IntPoint second, double dist)
        {
            this.firstPoint = first;
            this.secondPoint = second;
            this.distance = dist;
        }
    }

    private MaxPoints GetMaxPoints(IEnumerable<IntPoint> points)
    {
        var data = from a in points
                   from b in points
                   select new MaxPoints(a, b, GetDistance(a, b));

        double maxDistance = data.Max(d => d.distance);

        return data.First(d => d.distance == maxDistance);
    }

    private double GetDistance(IntPoint a, IntPoint b)
    {
        return Math.Sqrt(Math.Pow((a.X - b.X), 2) + Math.Pow((a.Y - b.Y), 2));
    }