2012-10-20 3 views
2

아래 코드에서, 작은 원으로 표시된 중심과 노란색 선으로 표시된 선으로 가장 큰 윤곽을 그릴 수 있습니다. 볼록 결함을 어떻게 그릴 수 있습니까? circle() 함수 또는 drawContours() 함수를 사용해야합니까?Drawing Convexity 결함 C++ OpenCV

Mat bw; 
Mat canny_output; 
vector<vector<Point> > contours; 
vector<Vec4i> hierarchy; 

findContours(bw, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 
int s = getBiggestContour(contours); 

Mat drawing = Mat::zeros(src.size(), CV_8UC3); //UC1 

Point2f mc = getCentroidPoint(contours[s]); 
drawContours(drawing, contours, s, Scalar(255,255,255), -1, 8, hierarchy, 0, Point()); 
circle(drawing, mc, 4, Scalar(0,0,255), 1, 8, 0); 

vector<vector<Point> >hull(contours[s].size()); 
convexHull(Mat(contours[s]), hull[s], false); 
drawContours(drawing, hull, s, Scalar(0,255,255), 1, 8, vector<Vec4i>(), 0, Point()); 

작품 위의 코드를하지만 너무 많이 선체 그래서 벡터를 사용하여 생각의 가장 큰 윤곽>는 사용하는 단 하나의 윤곽이있다. 어떻게 단순화합니까?

아래 코드는 다른 stackoverflow 질문의 것이지만 볼록 결함을 매트 이미지에 그리는 데 결함 변수를 사용하는 방법을 보여주지 않습니다. 어떻게이 일을 성취 할 수 있습니까?

vector<vector<int> > hullsI(contours.size()); 
vector<vector<Point> > hullsP(contours.size()); 
vector<vector<Vec4i> > defects(contours.size()); 

for(int i = 0; i <contours.size(); ++i){ 
    //find the hulls 
    convexHull(contours[i], hullsI[i], false, false); 
    convexHull(contours[i], hullsP[i], false, true); 
    //find the defects 
    if (contours[i].size() >3){ 
     convexityDefects(contours[i], hullsI[i], defects[i]); 
    } 
} 

IplImage를 사용하고 싶지 않습니다. 나는 매트를 선호한다.

+0

우수 답변은 여기 // 유래. com/a/14137154/616644 –

답변

1

'cvDrawContours()'를 사용하여 볼록 선체 연산의 결과를 그릴 수 있지만 가능한 경우 해당 매개 변수를 올바르게 설정해야합니다. 나는 예를 가지고 있지만 ') cvConvexHull2를 ('사용하고 IplImages하지만이 매트와 다른 볼록 포 작업에 동일한 방식으로 작동합니다 : HTTP : 내가 찾은

IplImage* src; //the image where the contours are detected on 
IplImage frm; //the image you want the results to be drawn on 

CvMemStorage* storage = cvCreateMemStorage(0); 
CvSeq* contours = NULL; 
cvFindContours(src, storage, &contours, sizeof (CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); 
for (CvSeq* c = contours; c != NULL; c = c->h_next) { 
      CvSeq* dest = NULL; 
      CvMemStorage* hullStorage = cvCreateMemStorage(0); 
      dest = cvConvexHull2(c, hullStorage, CV_CLOCKWISE, 1); 
      cvDrawContours(frm, dest, cvScalarAll(255), cvScalarAll(255), 0, 2, 8); 
      cvReleaseMemStorage(&hullStorage); 
     } 
cvReleaseMemStorage(&storage); 
+0

Mat를 사용하는 샘플 코드가 있습니까? 나는 IplImage와 CvSeq에 익숙하지 않다. –

+0

어쨌든 CvSeq을 사용해야 할 것입니다.하지만 Mat를 사용하여 어떻게 작동되는지 나중에 확인하겠습니다. 그러는 동안 매트를 IplImage *로 변환 할 수 있습니다 : 'cv :: Mat myMatrice;' 'IplImage myImage = myMatrice;' 매트로 돌아 가기 : 'cv :: Mat img (iplimg);' –