C++ OpenCV:findContours函数错误
我正在使用opencv的2.4.3版本,并尝试在canny边缘检测后使用“FindContentours”功能,如下所示:C++ OpenCV:findContours函数错误,c++,image,image-processing,opencv,error-handling,C++,Image,Image Processing,Opencv,Error Handling,我正在使用opencv的2.4.3版本,并尝试在canny边缘检测后使用“FindContentours”功能,如下所示: struct Component { cv::Rect boundingBox; double area; double circularity; } cv::vector < Component > components; cv::vector < cv::Vec4i > hierarchy; cv::findContou
struct Component
{
cv::Rect boundingBox;
double area;
double circularity;
}
cv::vector < Component > components;
cv::vector < cv::Vec4i > hierarchy;
cv::findContours ( cannyEdges, components, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
我怎样才能解决这个问题 cv::findcontours将每个轮廓作为点向量返回(请参见) 您必须自己将这些向量转换为数据结构(组件),就像我创建的这个最小示例:
#include <opencv2/opencv.hpp>
#include <iostream>
struct Component
{
cv::Rect boundingBox;
double area;
double circularity;
};
int main()
{
// Create a small image with a circle in it.
cv::Mat image(256, 256, CV_8UC3, cv::Scalar(0, 0, 0));
cv::circle(image, cv::Point(80, 110), 42, cv::Scalar(255,127, 63), -1);
// Find canny edges.
cv::Mat cannyEdges;
cv::Canny(image, cannyEdges, 80, 60);
// Show the images.
cv::imshow("img", image);
cv::imshow("cannyEdges", cannyEdges);
// Find the contours in the canny image.
cv::vector<cv::Vec4i> hierarchy;
// "Each contour is stored as a vector of points."
// http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours
typedef cv::vector<cv::vector<cv::Point> > TContours;
TContours contours;
cv::findContours(cannyEdges, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
// cannyEdges is destroyed after calling cv::findContours
// Print number of found contours.
std::cout << "Found " << contours.size() << " contours." << std::endl;
// Convert contours to Components.
typedef cv::vector<Component> TComponents;
TComponents components;
for (TContours::const_iterator it( contours.begin() ); it != contours.end(); ++it)
{
Component c;
c.area = cv::contourArea(*it);
c.boundingBox = cv::boundingRect(*it);
c.circularity = 0.0; // Insert whatever you mean by circularity;
components.push_back(c);
}
for (TComponents::const_iterator it( components.begin() ); it != components.end(); ++it)
std::cout << it->area << std::endl; // and whatever you want.
// Wait for user input.
cv::waitKey();
}
#包括
#包括
结构组件
{
cv::Rect边界框;
双区;
双圆度;
};
int main()
{
//创建一个带有圆圈的小图像。
cv::Mat图像(256,256,cv_8UC3,cv::标量(0,0,0));
cv::圆(图像,cv::点(80110),42,cv::标量(255127,63),-1);
//找到精明的边缘。
cv::Mat cannyEdges;
cv::Canny(图像,cannyEdges,80,60);
//显示图像。
cv::imshow(“img”,图像);
cv::imshow(“cannyEdges”,cannyEdges);
//在canny图像中找到轮廓。
向量层次;
//“每个轮廓都存储为点的矢量。”
// http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours
typedef cv::向量TContours;
轮廓线;
cv::findContours(CannyEdge、等高线、层次、cv_RETR_CCOMP、cv_CHAIN_approw_NONE);
//调用cv::findContours后,CannyEdge将被销毁
//打印找到的轮廓数。
std::我们不能用您显示的内容重现错误。请为该链接提供一个@karlphillip:+1亲爱的Dobi:非常感谢您的回答。我尝试了您的代码,它在“typedef cv::vector(…)”行中给出了一个错误,说“错误:类模板的参数列表”std::vector“丢失”。很抱歉,由于internet访问问题,我的答复迟了。我猜您使用的是旧的编译器,它不支持模板类型中的>>。我在上面的代码中更正了这一点,例如,我将“typedef cv::vector TContours;”改为“typedef cv::vector TContours;”。请重试。
#include <opencv2/opencv.hpp>
#include <iostream>
struct Component
{
cv::Rect boundingBox;
double area;
double circularity;
};
int main()
{
// Create a small image with a circle in it.
cv::Mat image(256, 256, CV_8UC3, cv::Scalar(0, 0, 0));
cv::circle(image, cv::Point(80, 110), 42, cv::Scalar(255,127, 63), -1);
// Find canny edges.
cv::Mat cannyEdges;
cv::Canny(image, cannyEdges, 80, 60);
// Show the images.
cv::imshow("img", image);
cv::imshow("cannyEdges", cannyEdges);
// Find the contours in the canny image.
cv::vector<cv::Vec4i> hierarchy;
// "Each contour is stored as a vector of points."
// http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours
typedef cv::vector<cv::vector<cv::Point> > TContours;
TContours contours;
cv::findContours(cannyEdges, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
// cannyEdges is destroyed after calling cv::findContours
// Print number of found contours.
std::cout << "Found " << contours.size() << " contours." << std::endl;
// Convert contours to Components.
typedef cv::vector<Component> TComponents;
TComponents components;
for (TContours::const_iterator it( contours.begin() ); it != contours.end(); ++it)
{
Component c;
c.area = cv::contourArea(*it);
c.boundingBox = cv::boundingRect(*it);
c.circularity = 0.0; // Insert whatever you mean by circularity;
components.push_back(c);
}
for (TComponents::const_iterator it( components.begin() ); it != components.end(); ++it)
std::cout << it->area << std::endl; // and whatever you want.
// Wait for user input.
cv::waitKey();
}