寻找轮廓上凸点的索引

时间:2014-08-07 22:30:49

标签: c++ opencv contour convex

我有一个有序点的向量,它构成了蠕虫的轮廓(用opencv找到)。我试图沿着蠕虫的骨架获得点数。我想这么快就做到这一点,因此有一个简单的分段功能:

void Worm::segmentWorm(void)
{
    int jump = 5;
    int numPoints = wormContour.size();

    int currentIndex = headIndex; //large circle in image w/overlay
    int endIndex = tailIndex;     //small circle in image w/overlay
    int matchingIndex;

    int direction = (endIndex - currentIndex)/abs(endIndex - currentIndex);

    int thisSideLength = abs(endIndex - currentIndex);
    int otherSideLength = numPoints - thisSideLength;

    double lengthPercentage;

    if (direction > 0) {
        while (currentIndex < endIndex - jump) {
            currentIndex += jump;

            lengthPercentage = (double)(endIndex - currentIndex)/(double)thisSideLength;
            matchingIndex = boundCheck((int)((lengthPercentage * otherSideLength) + endIndex), numPoints - 1);

            segments.push_back(pair<int, int>(currentIndex, matchingIndex));
        }
    } else if (direction < 0) {
        while (currentIndex > endIndex + jump) {
            currentIndex -= jump;

            lengthPercentage = (double)(currentIndex - endIndex)/(double)thisSideLength;
            matchingIndex = boundCheck((int)(-(lengthPercentage * otherSideLength) + endIndex), numPoints - 1);

            segments.push_back(pair<int, int>(currentIndex, matchingIndex));
        }
    }
}

这个功能的问题在于当蠕虫弯曲很多时,即轮廓在一侧变得凹陷,骨架会切割角落而不再代表蠕虫的中心。我的解决方案是如果它们是凹的那么移动段末端,校正段和骨架。

有关非常有效的功能的任何建议,可以找到轮廓上的所有凹(或凸)点吗?

问题形象:

enter image description here

1 个答案:

答案 0 :(得分:1)

如果没有几何计算,就无法从该数组中获得正确的点对。

一种解决方案是沿着一侧迭代,然后使用法线来找到一个点的对应物。我想如果蠕虫的宽度没有太大变化,你可以使用固定偏移长度来搜索另一个点,另一个点使用另一个点的子集,这意味着BF匹配应该非常快。然后,您可以在迭代时更新偏移量和子集。

编辑:如果对方的索引的初始猜测不是很糟糕,那么甚至不需要强力匹配,因为你可以遍历一侧,直到没有点再接近。

相关问题