openCV C++查找轮廓

openCV C++ find Contours

本文关键字:轮廓 查找 C++ openCV      更新时间:2023-10-16

我想从我的图片中获得轮廓的坐标。我想把它保存为.txt文档。

首先我创建了:

vector<vector<cv:Pont> >extract<cv::Mat &binaryImage){
vector<vector<cv::Point> > coordinatesContours;
cv::findContours(binaryImage, coordinatesContours,  CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
return coordinatesContours;
}

我调用Main中的函数:

oTest.extract(binaryImage);

现在我想将坐标作为输出导出到.txt 中

我的功能:

void Export(vector<vector<cv::Point> > coordinatesContours){
string json;
json = "{ntt "vertices" : [ntttt";

for(int i=0; i< coordinatesContours.size; i++)
    for(int j=0; i< coordinatesContours.size; j++)
        cout << i << "(" << coordinatesContours[i][j].x << ", " << coordinatesContours[i][j].y << ")" << endl;

但是我怎样才能完成我的任务呢??

在我的Main中如何称呼它??

请帮助thx

我不得不说,您可以直接在Extract函数中添加Export函数。您应该使用fstream导出此处的点,例如:

#indlude fstream
// edit : typo corrected Pont -> Point
... vector<vector<cv::Point> >extract<cv::Mat &binaryImage){
    vector<vector<cv::Point> > coordinatesContours;
    cv::findContours(binaryImage, coordinatesContours,  CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
fstream export_file;
export_file.open("Some_Name.txt",std::ios::out);
for(int i=0; i< coordinatesContours.size; i++)
    for(int j=0; i< coordinatesContours[i].size; j++)
        export_file << "Contour: " << i << ", Point: " << j << ",(" << coordinatesContours[i][j].x << ", " << coordinatesContours[i][j].y << ")" << endl;
export_file.close();
}