【问题标题】:openCV C++ find ContoursopenCV C++ 查找轮廓
【发布时间】:2017-02-26 04:36:02
【问题描述】:

我想从我的图片中获取轮廓的坐标。我想将其保存为 .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 = "{\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;

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

以及如何在我的 Main 中调用它??

请帮忙 谢谢

【问题讨论】:

  • 为什么你的第一个函数不完整?如果您发布所有功能,无论您是否在课堂上使用它都会有所帮助
  • 顺便说一句,在您发布的功能中,您在这里有一个拼写错误:vector&lt;vector&lt;cv:Pont&gt; &gt;extract&lt;cv::Mat &amp;binaryImage){ 您需要将Pont 更改为Point

标签: c++ opencv


【解决方案1】:

好吧,我不得不说,你可以直接在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();

}

【讨论】:

  • 我试过了,但我*不起作用。我的查找轮廓函数有可能出错吗?
  • 你用的是什么版本的opencv?
猜你喜欢
  • 2012-11-06
  • 2015-06-24
  • 2018-01-18
  • 1970-01-01
  • 2014-07-12
  • 2012-12-28
  • 2011-06-13
  • 1970-01-01
  • 2016-08-04
相关资源
最近更新 更多