【问题标题】:Contour segmentation轮廓分割
【发布时间】:2017-08-15 17:28:50
【问题描述】:

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

我想要这样的细分:

你知道我该如何解决这样的问题

非常感谢您的问候

【问题讨论】:

  • 使用形态学操作来细化曲线。使用曲率来估计它在局部的“不直”程度。
  • 感谢您的回答。你知道是否有一个计算曲率的opencv函数?
  • 请参阅this 答案以获取用于曲率计算的示例和代码
  • @sjanko 你有解决办法吗?如果你这样做了,请发布它

标签: opencv contour image-segmentation opencv-contour


【解决方案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);
        }
    }
}

【讨论】:

    【解决方案2】:

    这是我的轮廓

    这就是分割的结果

    【讨论】:

      猜你喜欢
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 2021-08-03
      • 2017-06-11
      相关资源
      最近更新 更多