2

I'm trying to write a code, in which I can detect different geometric shape except squares, I found : this answer here the answer is given in python, I've tried to write in c++ but my program crashes, any idea what's I'm doing wrong:

int main (){
    cv::Mat img = cv::imread("src.jpg",0);
    cv::Mat image ;
    
    std::vector<std::vector<cv::Point>> contours;
    //std::vector<std::vector<cv::Point2f>> hiararchy;
    
        cv::threshold(img,img,127,255,CV_THRESH_BINARY_INV);
        cv::findContours(img,contours,/*hiararchy,*/CV_RETR_EXTERNAL,CV_RETR_CCOMP );
    std::vector<cv::Point2f> approx; 
    for ( int i=0; i<contours.size();i++){
        cv::approxPolyDP(cv::Mat(contours[i]),approx,cv::arcLength(cv::Mat(contours[i]),true)*0.02,true);
        
    }
            
    cv::waitKey(0);
    
    return 0;
    
}

I've debugged the program, and it crashes in the cv::approxPolyDP function!

**update ** after the suggestion of C. Canberk Bacı I'Ve changed the for l

for ( int i=0; i<contours.size();i++){
    cv::Mat m(contours[i]);
    cv::approxPolyDP(m,approx,cv::arcLength(m,true)*0.02,true);
    
}

but it didn't change a lot thanks in advance for your help!

Community
  • 1
  • 1
Engine
  • 5,360
  • 18
  • 84
  • 162
  • Is there any output in the console window? – Niko Jul 18 '13 at 06:41
  • OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN (type0) && ((1 << type0) & fixedDepthMask) != 0)) in unknown function, file ..\. .\..\opencv\modules\core\src\matrix.cpp, line 1486 – Engine Jul 18 '13 at 06:43
  • you dont need to type cast "contours" as "Mat"... Just erase cv::Mat constructors of contours inside approxpoly function. – baci Jul 18 '13 at 06:47
  • also, you are finding approxpolys for all of your contours, but you have approx size = 1. vector > approx; and approx[i] inside the approxpoly method is what you need – baci Jul 18 '13 at 06:54
  • @Baci thanks for your help, please have a look at the update of my question, and about approx I changed to but it didn't change a lot – Engine Jul 18 '13 at 06:59

2 Answers2

2

got it :

int main (){
    cv::Mat img = cv::imread("src.jpg",0);
    cv::Mat image ;

    std::vector<std::vector<cv::Point>> contours;
    //std::vector<std::vector<cv::Point2f>> hiararchy;

        cv::threshold(img,img,127,255,CV_THRESH_BINARY_INV);
        cv::findContours(img,contours,/*hiararchy,*/CV_RETR_EXTERNAL,CV_RETR_CCOMP );
        std::vector<cv::Point> approx;   // this should be  1D
    for ( int i=0; i<contours.size();i++){

        cv::approxPolyDP(cv::Mat(contours[i]),approx,(cv::arcLength(cv::Mat(contours[i]),true)*0.02),true);

    }

    cv::waitKey(0);

again thanks for help

Engine
  • 5,360
  • 18
  • 84
  • 162
  • 3
    I got the same problem on 2.4.10 for Android. And the same code works fine on 3.0 beta. After I changes `std::vector approx` to `std::vector approx` Both OpenCV version (2.4.10 and 3.0) works fine. – Jack Fan Apr 09 '15 at 15:01
  • Yes, the main problem is that approxPolyDP expects (according to the documentation) an "Input vector of a 2D point". The type of the input vector (more precisely the depth of the mat) is checked at the beginning of approxPolyDP and CV_Assert fails. – Anonymous Mar 13 '17 at 10:30
0

I just came across a similar problem when using drawContours(). We need to make sure the Point type is consistent with the function declaration. If it asks for cv::Point, then passing cv::Point2f will lead to this problem.

Yiming
  • 1