gpt4 book ai didi

opencv - 轮廓分割

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:50 26 4
gpt4 key购买 nike

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

original image

我想要这样的分割:

segmented image

你知道我怎么能解决这样的问题吗

非常感谢,并致以最诚挚的问候

最佳答案

是的,我通过@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);
}
}
}

关于opencv - 轮廓分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42971318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com