gpt4 book ai didi

c++ - 如何获取属于轮廓特定一侧的点?

转载 作者:太空宇宙 更新时间:2023-11-04 13:39:10 24 4
gpt4 key购买 nike

我正在使用 OpenCV 2.9 c++,在带有 findContours 的图像中找到了一个轮廓及其边界框:

Image

下一步是获取所有轮廓点,这些点存储在 vector<Point> 中,在 S 和 E 之间,同样在对边(轮廓箭头指向的地方)。我不知道如何提取这些点,如果你能在这里帮助我,那就太好了。

提前致谢!

编辑:

点S和E没有预先给定,它们是与边界框相交的轮廓点。

最佳答案

这是一种适用于综合数据的方法,但可能不适用于真实轮廓。问题是找到角落,我只是没有找到正确的方法,虽然我觉得我应该知道怎么做。这个想法是找到某种“局部极值”,它描述了与其邻居相比的角落。

正如我所说,肯定有问题,但我现在不明白......

我生成这个输入:

enter image description here

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

enter image description here

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

enter image description here

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

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;
}

关于c++ - 如何获取属于轮廓特定一侧的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28431797/

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