2012-02-25 6 views
1

私はCvSeqから作成した私自身の輪郭を描画するためにcvDrawContoursを使用したいと考えています(通常、輪郭はOpenCVの他の関数から戻されます)。これが私の解決策であるが、それは動作しません:(私はこのポストから2回目の試行のための OpenCV sequences -- how to create a sequence of point pairs?OpenCvでCvPointのカスタマイズされたシーケンスを作成する

をCvPointからカスタマイズされた輪郭のシーケンスを作成する方法を選んだ

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; 

、私は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); 

はおそらく、私が誤ったデータを使用しているのはなぜ?

のようなベクトルに一backポイントを許可していません&コンパイラの警告・エラー。

エラーは、このようなものです: エラー2エラーC2664: 'のstd ::ベクトル< _Ty> ::一back':「へのconstのstd ::ベクトル<から 'CV :: Point2iを' パラメータ1を変換することはできません_Ty> & '

+0

私はOpenCV C++を試しました。しかし、まだ解決できません。たぶん私はそれを間違って使用しました。私は質問に裁判を追加した –

答えて

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.push_back(VAR) - > varが

おかげベクトルでなければなりません!私はバグを学んだ。

1

最初の例では、単一の要素を持つポイントfoursomesのシーケンスを作成した。シーケンスelem_sizesizeof(CvPoint)こと(4倍ではない)との点を一つずつ追加してください:

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

あなたが輪郭を描くために最後のポイントを挿入する必要はありません、輪郭は自動的にクローズされます。

関連する問題