在OpenCv中创建自定义的CvPoint序列

在OpenCv中创建自定义的CvPoint序列,opencv,sequence,contour,Opencv,Sequence,Contour,我想使用cvDrawContours来绘制我自己从CvSeq创建的轮廓(通常,轮廓是从OpenCV的其他函数中检索的)。这是我的解决方案,但不起作用:( 我从这篇文章中选择了从CvPoint创建自定义轮廓序列的方法 在第二次尝试中,我使用了Cpp OpenCV: vector<vector<Point2i>> contours; Point2i P; P.x = 0; P.y = 0; contours.push_back(P); P.x = 50; P.y = 10;

我想使用cvDrawContours来绘制我自己从CvSeq创建的轮廓(通常,轮廓是从OpenCV的其他函数中检索的)。这是我的解决方案,但不起作用:(

我从这篇文章中选择了从CvPoint创建自定义轮廓序列的方法

在第二次尝试中,我使用了Cpp OpenCV:

vector<vector<Point2i>> contours;
Point2i P;
P.x = 0;
P.y = 0;
contours.push_back(P);
P.x = 50;
P.y = 10;
contours.push_back(P);
P.x = 20;
P.y = 100;
contours.push_back(P);

Mat img = imread(file, 1);
drawContours(img, contours, -1, CV_RGB(0,0,255), 5, 8);
矢量轮廓;
点2i P;
P.x=0;
P.y=0;
等高线。推回(P);
P.x=50;
P.y=10;
等高线。推回(P);
P.x=20;
P.y=100;
等高线。推回(P);
Mat img=imread(文件,1);
等高线图(img,等高线图,-1,等高线图(0,0255),等高线图5,8);
可能我使用的数据不正确。编译器会发出错误警报&不允许像这样将点推回向量。为什么

错误如下所示:
错误2错误C2664:'std::vector::push_back':无法将参数1从'cv::Point2i'转换为'const std::vector&'

我终于完成了它

Mat g_gray_cpp = imread(file, 0);  

vector<vector<Point2i>> contours; 
vector<Point2i> pvect;
Point2i P(0,0);

pvect.push_back(P);

P.x = 50;
P.y = 10;   
pvect.push_back(P);

P.x = 20;
P.y = 100;
pvect.push_back(P);

contours.push_back(pvect);

Mat img = imread(file, 1);

drawContours(img, contours, -1, CV_RGB(0,0,255), 5, 8);

namedWindow( "Contours", 0 );
imshow( "Contours", img );  
matg\u gray\u cpp=imread(文件,0);
矢量等值线;
向量pvect;
点2i P(0,0);
pvect.推回(P);
P.x=50;
P.y=10;
pvect.推回(P);
P.x=20;
P.y=100;
pvect.推回(P);
轮廓。推回(pvect);
Mat img=imread(文件,1);
等高线图(img,等高线图,-1,等高线图(0,0255),等高线图5,8);
namedWindow(“等高线”,0);
imshow(“轮廓”,img);
因为“等高线”是矢量>,所以等高线.push_-back(var)->var应该是矢量


谢谢!我学到了一个bug

在您的第一个示例中,您创建了一个具有单个元素的点四人序列。序列
elem_size
应为
sizeof(CvPoint)
(不乘以四)并逐个添加点:

CvMemStorage*memStorage=cvCreateMemStorage(0);
//没有这些标志,Debug CONTURSOR()方法不考虑序列
//就像轮廓一样,什么也不画
CvSeq*seq=cvCreateSeq(CV_32SC2 | CV_seq_KIND_曲线,
sizeof(CvSeq)、sizeof(CvPoint)、memStorage;
cvSeqPush(cvPoint(10,10));
cvSeqPush(cvPoint(1,1));
cvSeqPush(cvPoint(20,50));

注意,你不需要插入最后一点来绘制轮廓,轮廓自动关闭。< /P>我已经尝试了OpenCV C++。但是仍然不能解决。也许我用错了。我在这个问题中添加了我的试验。

Mat g_gray_cpp = imread(file, 0);  

vector<vector<Point2i>> contours; 
vector<Point2i> pvect;
Point2i P(0,0);

pvect.push_back(P);

P.x = 50;
P.y = 10;   
pvect.push_back(P);

P.x = 20;
P.y = 100;
pvect.push_back(P);

contours.push_back(pvect);

Mat img = imread(file, 1);

drawContours(img, contours, -1, CV_RGB(0,0,255), 5, 8);

namedWindow( "Contours", 0 );
imshow( "Contours", img );