2012-02-25 1 views
1
내가 CvSeq에서 만든 내 자신의 윤곽을 그리는 cvDrawContours을 사용할

에서 CvPoint의 사용자 정의 순서를 만들 수 (일반적으로, 윤곽은 OpenCV의 다른 기능에서 retured된다). 이것은 내 솔루션지만 작동하지 않습니다 :(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 sequences -- how to create a sequence of point pairs?

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

는 아마도 내가 잘못 데이터를 사용하는 이유는?

같은 벡터에와 push_back 점을 허용하지 않습니다 & 컴파일러 경고 오류..

오류는 다음과 같이이다 : 오류이 오류 C2664 : '표준 : : 벡터 < _Ty> ::와 push_back': '에 const를 표준 : : 벡터 <에서'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

첫 번째 예에서는 단일 요소로 포인트 네면의 시퀀스를 만들었다. 순서 elem_sizesizeof(CvPoint)은 (사 곱하지 않음)하고 하나 점 하나를 추가해야합니다 : 당신이 윤곽을 그릴 마지막 점을 삽입 할 필요가 없습니다

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

참고, 윤곽이 자동으로 닫힙니다.