简体   繁体   English

openCV C ++查找轮廓

[英]openCV C++ find Contours

I want to get the coordinates of the contours from my picture. 我想从图片中获取轮廓的坐标。 And i want to save it as a .txt document. 我想将其另存为.txt文档。

First of all i created: 首先,我创建了:

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

And i call the function in my Main: 我在Main中调用该函数:

oTest.extract(binaryImage);

And now i want to export the coordinates as an output into a .txt 现在我想将坐标作为输出导出到.txt中

My function: 我的功能:

void Export(vector<vector<cv::Point> > coordinatesContours){

string json;
json = "{\n\t\t \"vertices\" : [\n\t\t\t\t";


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;

but how can i finish my fuction?? 但是我该如何完成我的功能?

And how to call it in my Main?? 以及如何在我的Main中调用它?

Please Help thx 请帮忙

Well I have to say that you can add the Export function directly in the Extract function. 好吧,我不得不说,您可以直接在Extract函数中添加Export函数。 You should use fstream to export the points here an example: 您应该使用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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM