轮廓分割

时间:2017-03-23 08:59:23

标签: opencv contour image-segmentation opencv-contour

我的轮廓由曲线段和直线段组成。是否有可能将轮廓分割成弯曲和直线部分? 所以这是一个轮廓的例子

original image

我希望有这样的细分:

segmented image

你知道如何解决这个问题

非常感谢和最诚挚的问候

2 个答案:

答案 0 :(得分:1)

是的,我通过链接@PSchn发布了解决方案。我只是通过轮廓点并定义了边框。边界下的所有东西都是一个“曲线段”,而另一个则是一个直线段。谢谢你的帮助!!

vector<double> getCurvature(vector<Point> const& tContourPoints, int tStepSize)
{
int iplus;
int iminus;

double acurvature;
double adivisor;

Point2f pplus;
Point2f pminus;
// erste Ableitung
Point2f a1stDerivative;
// zweite Ableitung
Point2f a2ndDerivative;

vector< double > rVecCurvature( tContourPoints.size() );

if ((int)tContourPoints.size() < tStepSize)
{
    return rVecCurvature;
}

for (int i = 0; i < (int)tContourPoints.size(); i++ )
{
    const Point2f& pos = tContourPoints[i];

    iminus  = i-tStepSize;
    iplus   = i+tStepSize;

    if(iminus < 0)
    {
        pminus = tContourPoints[iminus + tContourPoints.size()];
    }
    else
    {
        pminus = tContourPoints[iminus];
    }

    if(iplus  > (int)tContourPoints.size())
    {
        pplus = tContourPoints[iplus - (int)tContourPoints.size()];
    }
    else
    {
        pplus = tContourPoints[iplus];
    }

    a1stDerivative.x = (pplus.x -           pminus.x) / ( iplus-iminus);
    a1stDerivative.y = (pplus.y -           pminus.y) / ( iplus-iminus);
    a2ndDerivative.x = (pplus.x - 2*pos.x + pminus.x) / ((iplus-iminus)/2*(iplus-iminus)/2);
    a2ndDerivative.y = (pplus.y - 2*pos.y + pminus.y) / ((iplus-iminus)/2*(iplus-iminus)/2);


    adivisor = a2ndDerivative.x*a2ndDerivative.x + a2ndDerivative.y*a2ndDerivative.y;
    if (  abs(adivisor) > 10e-8 )
    {
        acurvature =   abs(a2ndDerivative.y*a1stDerivative.x - a2ndDerivative.x*a1stDerivative.y) / pow(adivisor, 3.0/2.0 )  ;
    }
    else
    {
        acurvature =  numeric_limits<double>::infinity();
    }

    rVecCurvature[i] = acurvature;
}
return rVecCurvature;
}

一旦我得到曲率,我就定义了一个边框,并完成了我的轮廓:

acurvature = getCurvature(aContours_img[0], 50);

if(acurvature.size() > 0)
{
    // aSegmentChange =1 --> curved segment
   // aSegmentChange =0 --> straigth segment
    if( acurvature[0] < aBorder)
    {
        aSegmentChange = 1;
    }
    else
    {
        aSegmentChange = 0;
    }

    // Kontur segmentieren
    for(int i = 0; i < (int)acurvature.size(); i++)
    {
        aSegments[aSegmentIndex].push_back(aContours_img[0][i]);
        aCurveIndex[aSegmentIndex].push_back(aSegmentChange);

        if( acurvature[i] < aBorder && aSegmentChange == 0 )
        {
            aSegmentIndex++;
            aSegmentChange = 1;
        }
        if( acurvature[i] > aBorder && aSegmentChange == 1 )
        {
            aSegmentIndex++;
            aSegmentChange = 0;
        }

        if(aSegmentIndex >= (int)aSegments.size()-1)
        {
            aSegments.resize(aSegmentIndex+1);
            aCurveIndex.resize(aSegmentIndex+1);
        }
    }
}

答案 1 :(得分:0)

This is my contour 这是我的轮廓

And thats the result 这是分割的结果