1

cvDrawContours を使用して、CvSeq から作成された独自の輪郭を描画したいと考えています (通常、輪郭は OpenCV の他の関数から返されます)。これは私の解決策ですが、うまくいきません:(

IplImage*    g_gray    = NULL;

CvMemStorage *memStorage = cvCreateMemStorage(0); 
CvSeq* seq = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint)*4, memStorage); 


CvPoint points[4]; 
points[0].x = 10;
points[0].y = 10;
points[1].x = 1;
points[1].y = 1;
points[2].x = 20;
points[2].y = 50;
points[3].x = 10;
points[3].y = 10;

cvSeqPush(seq, &points); 

g_gray = cvCreateImage( cvSize(300,300), 8, 1 );

cvNamedWindow( "MyContour", CV_WINDOW_AUTOSIZE );

cvDrawContours( 
    g_gray, 
    seq, 
    cvScalarAll(100),
    cvScalarAll(255),
    0,
    3);

cvShowImage( "MyContour", g_gray );

cvWaitKey(0);  

cvReleaseImage( &g_gray );
cvDestroyWindow("MyContour");

return 0;

この投稿OpenCV シーケンス - ポイントペアのシーケンスを作成する方法?から、CvPoint からカスタマイズされた輪郭シーケンスを作成する方法を選択し ました。

2回目の試行では、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);

データの使い方が間違っていたのかもしれません。コンパイラはエラーを警告し、そのようなベクトルへの push_back ポイントを許可しません。どうして??

エラーは次のようになります: エラー 2 エラー C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'cv::Point2i' to 'const std::vector<_Ty> &'

4

2 に答える 2

1

やっと完成しました。

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

'contours'はvector>であるため、contours.push_back(var)->varはベクトルである必要があります

ありがとう!バグを学びました

于 2012-03-01T15:43:17.697 に答える
1

最初の例では、単一の要素を持つポイント フォーサムのシーケンスを作成しました。シーケンスelem_sizesizeof(CvPoint)(4 倍ではなく) で、ポイントを 1 つずつ追加する必要があります。

CvMemStorage *memStorage = cvCreateMemStorage(0); 
// without these flags the drawContours() method does not consider the sequence
// as contour and just draws nothing
CvSeq* seq = cvCreateSeq(CV_32SC2 | CV_SEQ_KIND_CURVE, 
      sizeof(CvSeq), sizeof(CvPoint), memStorage); 

cvSeqPush(cvPoint(10, 10));
cvSeqPush(cvPoint(1, 1));
cvSeqPush(cvPoint(20, 50));

輪郭を描くために最後の点を挿入する必要がないことに注意してください。輪郭は自動的に閉じられます。

于 2013-04-04T05:12:11.340 に答える