Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取属于轮廓特定边的点? 我使用OpenCV 2.9 C++,在FIDCONTIONS及其边界框中找到一个轮廓:_C++_Opencv - Fatal编程技术网

如何获取属于轮廓特定边的点? 我使用OpenCV 2.9 C++,在FIDCONTIONS及其边界框中找到一个轮廓:

如何获取属于轮廓特定边的点? 我使用OpenCV 2.9 C++,在FIDCONTIONS及其边界框中找到一个轮廓:,c++,opencv,C++,Opencv,下一步是获取S和E之间以及轮廓箭头指向的对边上的所有轮廓点,这些点存储在向量中。我不知道如何提取这些点,如果你能在这里帮助我,那就太好了 提前谢谢 编辑: 点S和E不是优先给定的,它们是轮廓上与边界框相交的点。您有findContours返回的轮廓点列表。 在这些点中,找出y从S到E增长的最长序列和y对侧减小的最长序列。可能是增长和减少是交换的,这取决于findContours返回的轮廓是顺时针还是逆时针,这在图像处理中总是有点问题,因为y轴是颠倒交换的 另一种方法:找到轮廓点的中心或使用边界框

下一步是获取S和E之间以及轮廓箭头指向的对边上的所有轮廓点,这些点存储在向量中。我不知道如何提取这些点,如果你能在这里帮助我,那就太好了

提前谢谢

编辑:


点S和E不是优先给定的,它们是轮廓上与边界框相交的点。

您有findContours返回的轮廓点列表。 在这些点中,找出y从S到E增长的最长序列和y对侧减小的最长序列。可能是增长和减少是交换的,这取决于findContours返回的轮廓是顺时针还是逆时针,这在图像处理中总是有点问题,因为y轴是颠倒交换的

另一种方法:找到轮廓点的中心或使用边界框的中心,沿着轮廓点运行,查看到中心的函数距离是如何变化的:此函数的4个最大点是梯形图形的角。警告:如果点位于整数坐标中,请记住使用浮点表示的距离


当然,S及其镜像角点很容易找到,因为它们分别具有最小和最大x。如果您实现上述方法之一,这可能是一个验证步骤,尽管不完整。

在OpenCV中,轮廓基本上只是一个listwell向量,但在这种情况下,它与点无关。根据提取轮廓时使用的设置,此列表可能包含属于轮廓的所有点,或仅包含此列表的一部分,即整个列表的近似值。如果使用函数并将模式设置为CV_CHAIN_APPROX_NONE,则将以等高线中显示的相同顺序获得所有等高线点。然后,您只需在此列表中找到S点和E点的索引,并获取这两个索引之间的所有元素。

这里有一种方法适用于综合数据,但可能不适用于真实轮廓。这里的问题是找到拐角,我只是没有找到正确的方法,尽管我想我应该知道怎么做。其思想是找到某种局部极值,与它们的邻域相比,这些极值描述了角点

正如我所说,一定是出了什么问题,但我现在不明白

我生成以下输入:

使用findContours并提取仅适用于我的综合数据的极值:

然后,查找极值和边界框角点之间的对应关系,并将两者之间的所有轮廓点排序为边界框段:

如果有人解释了如何正确测量轮廓中的角点/极值,也许你可以使用代码的某些部分,也可能是整个代码。。。该代码没有任何优化,许多模运算等。。。没有封装

int main()
{
    cv::Mat input;
    input = cv::Mat::zeros(512,512,CV_8UC1);

    // generate something similar to the question:
    cv::Point2f topLeft  = 2*cv::Point2f(100,50);
    cv::Point2f topRight = 2*cv::Point2f(180,55);
    cv::Point2f bottomLeft  = 2*cv::Point2f(50,150);
    cv::Point2f bottomRight = 2*cv::Point2f(200,145);

    int lineWidth = 1;
    cv::line(input, topLeft, topRight, cv::Scalar(255),lineWidth);
    cv::line(input, topRight, bottomRight, cv::Scalar(255),lineWidth);
    cv::line(input, bottomRight, bottomLeft, cv::Scalar(255),lineWidth);
    cv::line(input, bottomLeft, topLeft, cv::Scalar(255),lineWidth);

    cv::Mat colored;
    cv::cvtColor(input,colored, CV_GRAY2BGR);


    std::vector<std::vector<cv::Point> > contours;
    cv::findContours(input,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);


    if(contours.size() == 0) return 0;

    std::vector<cv::Point> relevantContour = contours[0];
    cv::Rect bb = cv::boundingRect(relevantContour);

    // display bb
    //cv::rectangle(colored, bb, cv::Scalar(0,0,255));


    // you can encapusalte that easily!

    // first "derivative" of conour
    std::vector<cv::Point2f> contourDifferences;
    std::vector<cv::Point> contourDifferencesPoints;
    for(unsigned int i=0; i<relevantContour.size(); ++i)
    {
        contourDifferences.push_back(relevantContour[i] - relevantContour[(i+2)%relevantContour.size()]);
        contourDifferencesPoints.push_back(relevantContour[(i+1)%relevantContour.size()]);
    }

    // second "derivative"
    std::vector<cv::Point2f> contourDifferences2;
    std::vector<cv::Point> contourDifferences2Points;
    for(unsigned int i=0; i<contourDifferences.size(); ++i)
    {
        contourDifferences2.push_back(contourDifferences[i] - contourDifferences[(i+2)%contourDifferences.size()]);
        contourDifferences2Points.push_back(contourDifferencesPoints[(i+1)%contourDifferencesPoints.size()]);
    }


    // local optima:
    std::vector<cv::Point> localOptima;
    float delta = 0.0f; // add some delta to overcome noise?!? not necessary for my example, but maybe good for real contours...
    for(unsigned int i=0; i<contourDifferences2.size(); ++i)
    {
        int cIdx = (i+1)%contourDifferences2.size();
        if(cv::norm(contourDifferences2[cIdx]) - delta > cv::norm(contourDifferences2[i]) 
            && cv::norm(contourDifferences2[cIdx]) - delta > cv::norm(contourDifferences2[(i+2)%contourDifferences2.size()]))
        {
            std::cout << cv::norm(contourDifferences2[cIdx]) << std::endl;
            cv::circle(colored, contourDifferences2Points[cIdx], 5, cv::Scalar(0,255,0));
            localOptima.push_back(contourDifferences2Points[cIdx]);
        }
    }


    // this should be 4 local optima in your setting if everything works fine.
    std::cout << "number of local Optima: " << localOptima.size() << std::endl;

    // these are the 4 bounding box corners.
    std::vector<cv::Point> cornersBB;
    cornersBB.push_back(cv::Point(bb.x, bb.y));
    cornersBB.push_back(cv::Point(bb.x+bb.width, bb.y));
    cornersBB.push_back(cv::Point(bb.x+bb.width, bb.y+bb.height));
    cornersBB.push_back(cv::Point(bb.x, bb.y+bb.height));

    std::vector<int> matchingBB2Contour(cornersBB.size(), -1);
    //std::vector<int> matchingContour2BB(localOptima.size(), -1);

    for(unsigned int i=0; i<cornersBB.size(); ++i)
    {
        float bestDist = FLT_MAX;
        for(unsigned int j=0; j<localOptima.size(); ++j)
        {
            float cDist = cv::norm(cornersBB[i] - localOptima[j]);
            if(cDist < bestDist)
            {
                bestDist = cDist;
                matchingBB2Contour[i] = j;
            }
        }
    }

    // todo: compute the best matching from contour optima to bounding box corners too and compare them.

    float contourOrientation = cv::contourArea(relevantContour, true); // negative = counter-clockwise
    std::cout << contourOrientation << std::endl;
    // now visualize the result:
    for(unsigned int i=0; i<cornersBB.size(); ++i)
    {
        cv::Scalar segmentColor(i*100, 255-(i*100), (i==3)?255:0);

        // color the bounding box segment too
        cv::line(colored, cornersBB[i], cornersBB[(i+1) % cornersBB.size()], segmentColor, 1);

        cv::Point startPoint = localOptima[matchingBB2Contour[i]];
        cv::Point endPoint = localOptima[matchingBB2Contour[(i+1)%matchingBB2Contour.size()] % localOptima.size()];
        int startIndex = -1;
        int endIndex = -1;
        for(unsigned int j=0; j<relevantContour.size(); ++j)
        {
            if(startPoint == relevantContour[j])
            {
                startIndex = j;
            }
            if(endPoint == relevantContour[j])
            {
                endIndex = j;
            }
        }

        // swap start and end if orientation is negative = contour is counter clockwise
        if(contourOrientation < 0)
        {
            int tmp = startIndex;
            startIndex = endIndex;
            endIndex = tmp;
        }

        std::cout << "start index: " << startIndex << std::endl;
        std::cout << "end index: " << endIndex << std::endl;

        //while(relevantContour[startIndex%relevantContour.size()] != endPoint)
        while(startIndex % relevantContour.size() != endIndex)
        {
            cv::circle(colored, relevantContour[startIndex%relevantContour.size()], 1, segmentColor);
            startIndex++;
        }

    }

    // show bb-to-contour correspondences:
    /*
    for(unsigned int i=0; i<matchingBB2Contour.size(); ++i)
    {
        cv::line(colored, localOptima[matchingBB2Contour[i]], cornersBB[i], cv::Scalar(0,0,255),2);
    }
    */


    cv::imshow("colored", colored);
    cv::waitKey(0);
    return 0;
}

使用cv::findContours,您将收到一份点数列表。只需使用列表中两个选定点之间的所有点。我猜在本例中S可能是左下边界矩形角,但我如何获得点E?啊,好的,你已经提取了S和E。。。可以通过向前/向后/中心差在密集轮廓点列表中查找角点。在检测到角点后,您可以说与边界框角点距离最小的角点对应于它。请注意,S和/或E不一定必须与边界框相交!