2014-03-04 39 views
0

我現在正在使用OpenCV和圖像處理。我正在經歷的當前話題是凸面缺陷。下面的代碼是簡單的程序,可以說明凸性缺陷,但我得到的代碼後顯示的錯誤是錯誤的。所以,請幫我工作錯誤cvConvexityDefect

代碼: -

#include <cv.h> 
#include <highgui.h> 
#include <stdio.h> 

int g_thresh = 128; 

int main(int argc, char **argv) 
{ 
     CvMemStorage *hull = cvCreateMemStorage(0); 
     CvMemStorage *mem = NULL; 
     CvMemStorage *defe_mem = cvCreateMemStorage(0); 
     mem = cvCreateMemStorage(0); 
     CvSeq *contours = NULL; 
     CvSeq *poly; 
     CvSeq *convex; 
     CvSeq *i; 
     CvSeq *defect; 
     IplImage *img = cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR); 
     IplImage *gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); 
     cvCvtColor(img, gray, CV_RGB2GRAY); 
     cvShowImage("Source", img); 
     cvWaitKey(0); 
     cvShowImage("DEstination", gray); 
     cvWaitKey(0); 
     cvThreshold(gray, gray, g_thresh, 255, CV_THRESH_OTSU); 
     cvShowImage("BinaryImage", gray); 
     cvWaitKey(0); 

     int a = cvFindContours(gray, mem, &contours, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0)); 

     cvDrawContours(gray, contours, cvScalarAll(255), cvScalarAll(255), 1, 1, 8, cvPoint(0, 0)); 
     cvShowImage("Output",gray); 
     cvWaitKey(0); 
     convex = cvConvexHull2(contours, hull, CV_CLOCKWISE, 1); 
     cvDrawContours(gray, convex, cvScalarAll(255), cvScalarAll(255), 1, 1, 8, cvPoint(0, 0)); 
     cvShowImage("Convex_Hull", gray); 
     cvWaitKey(0); 
//Program Completely Works till here, every thing massed up after this line.. 


     defect = cvConvexityDefects(contours, convex, defe_mem); 

     for(i = contours; i != 0; i = i->h_next){ 
       cvDrawContours(gray, defect, cvScalarAll(255), cvScalarAll(255), 1, 1, 8, cvPoint(0, 0)); 
     } 

     //fprintf(stderr, "Convexity Defect::%d\n", cvCheckContourConvexity(contours)); 
     cvShowImage("Convexity", gray); 
     cvWaitKey(0); 

} 

錯誤: -

OpenCV Error: Unsupported format or combination of formats (Convex hull must represented as a sequence of indices or sequence of pointers) in cvConvexityDefects, file /home/akshit/OpenCV/OpenCV-2.4.3/modules/imgproc/src/convhull.cpp, line 544 
terminate called after throwing an instance of 'cv::Exception' 
    what(): /home/akshit/OpenCV/OpenCV-2.4.3/modules/imgproc/src/convhull.cpp:544: error: (-210) Convex hull must represented as a sequence of indices or sequence of pointers in function cvConvexityDefects 

凸包中發現了與輸出正常,但凸性缺陷沒有發現正確

任何建議?

+1

旁註:嘗試使用新的API OpenCV的替代。 – herohuyongtao

+0

我可以得到版本號,現在使用2.4.3 –

+0

2.4.3已經支持新的API。 – herohuyongtao

回答

3

我覺得這個代碼段應幫助:

#include <stdlib.h> 
#include <stdio.h> 
#include <iostream> 
#include <ctype.h> 
#include <time.h> 

#include <opencv2/opencv.hpp> 

using namespace cv; 
using namespace std; 

String window_name = "Hand_HSV"; 
Mat frame,copyFrame; 

// Detect Skin from YCrCb 
Mat DetectYCrCb(Mat img,Scalar min, Scalar max) 
{ 
    Mat skin; 
    cvtColor(img, skin, cv::COLOR_RGB2YCrCb); 
    inRange(skin, min, max, skin); 
    Mat rect_12 = getStructuringElement(cv::MORPH_RECT, Size(12,12) , Point(6,6)); 
    erode(skin, skin, rect_12,Point(),1); 
    Mat rect_6 = getStructuringElement(cv::MORPH_RECT, Size(6,6) , Point(3,3)); 
    dilate(skin,skin,rect_6,Point(),2); 
    return skin;  
} 

void DetectContour(Mat img) 
{ 
    Mat drawing = Mat::zeros(img.size(), CV_8UC3); 
    vector<vector<Point> > contours; 
    vector<vector<Point> > bigContours; 
    vector<Vec4i> hierarchy; 


    findContours(img,contours, hierarchy, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE, Point()); 

    if(contours.size()>0) 
    { 
     vector<vector<int> >hull(contours.size()); 
     vector<vector<Vec4i>> convDef(contours.size()); 
     vector<vector<Point>> hull_points(contours.size()); 
     vector<vector<Point>> defect_points(contours.size()); 


     for(int i = 0; i < contours.size(); i++) 
     { 
      if(contourArea(contours[i])>5000) 
      { 
       convexHull(contours[i], hull[i], false); 
       convexityDefects(contours[i],hull[i], convDef[i]); 

       // start_index, end_index, farthest_pt_index, fixpt_depth 

       for(int k=0;k<hull[i].size();k++) 
       {   
        int ind=hull[i][k]; 
        hull_points[i].push_back(contours[i][ind]); 
       } 

       for(int k=0;k<convDef[i].size();k++) 
       {   
        if(convDef[i][k][3]>20*256) 
        { 
         int ind_0=convDef[i][k][0]; 
         int ind_1=convDef[i][k][1]; 
         int ind_2=convDef[i][k][2]; 
         defect_points[i].push_back(contours[i][ind_2]); 
         cv::circle(drawing,contours[i][ind_0],5,Scalar(0,255,0),-1); 
         cv::circle(drawing,contours[i][ind_1],5,Scalar(0,255,0),-1); 
         cv::circle(drawing,contours[i][ind_2],5,Scalar(0,0,255),-1); 
         cv::line(drawing,contours[i][ind_2],contours[i][ind_0],Scalar(0,0,255),1); 
         cv::line(drawing,contours[i][ind_2],contours[i][ind_1],Scalar(0,0,255),1); 
        } 
       } 

       drawContours(drawing, contours, i, Scalar(0,255,0), 1, 8, vector<Vec4i>(), 0, Point()); 
       drawContours(drawing, hull_points, i, Scalar(255,0,0), 1, 8, vector<Vec4i>(), 0, Point()); 
      } 
     } 
    } 
    namedWindow("Hull demo",cv::WINDOW_AUTOSIZE); 
    imshow("Hull demo", drawing); 

} 


int main(int argc, char** argv) 
{ 
    VideoCapture capture(0); 
    //VideoCapture capture("Video_Hand.MPG"); 
    namedWindow(window_name, cv::WINDOW_AUTOSIZE); 
    if (capture.isOpened()){ 
     while(true) 
     { 
      //capture.read(frame); 
      //flip(frame,frame,1); 
      capture >> frame; 
      imshow(window_name, frame); 

      Mat skinYCrCb = DetectYCrCb(frame,Scalar(0, 100, 80), Scalar(255, 185, 135)); 
      imshow("Result",skinYCrCb); 

      DetectContour(skinYCrCb); 

      int c = waitKey(10); 
      if((char)c == 27) 
      { 
       break; 
      } 
     } 
    } 
    return 0; 
} 
+0

PLZ任何解決方案與我的程序? –

+1

你最好去新的API。但關於你的代碼:1)cvConvexityDefects接受作爲參數不是點,但它們的索引。 2)convex = cvConvexHull2(輪廓,船體,CV_CLOCKWISE,1);應該是凸= cvConvexHull2(輪廓,船體,CV_CLOCKWISE,0);我記得,最後一個參數定義了返回值的類型。 3)缺陷也是指數,而不是點,也不是輪廓線,所以你不能將它們畫成輪廓線。 –

+1

不確定關於點3):我發現了一篇文章:http://www.codeproject.com/Questions/288557/Finger-tracking-counting-using-openCv-convexity-de並且那裏的作者將缺陷畫成圓圈。 –