Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to use cv::matchShapes method from coding point of view in c or c++

Hi all, My objective is to find the pattern in the image captured from camera for this I found the cv::matchShapes method of opencv to implement it.

When I have implemented this method it is making the app crash on matchShapes method.Might be I have done something wrong:-

I exactly do not know how to use this method in order to find the match shape in my query image.This is what I tried.

Please help me in providing some code that can run properly.

Here is my code:-

- (std::vector<std::vector<cv::Point> >)tryingMatchShapes:(cv::Mat)_image _image1:(cv::Mat)_image1
{
    std::vector<std::vector<cv::Point> > squares;

    cv::Mat pyr, timg, gray0(_image.size(), CV_8U), gray;
    int thresh = 50, N = 11;
    cv::pyrDown(_image, pyr, cv::Size(_image.cols/2, _image.rows/2));
    cv::pyrUp(pyr, timg, _image.size());
    std::vector<std::vector<cv::Point> > contours;
    for( int c = 0; c < 3; c++ ) {
        int ch[] = {c, 0};
        mixChannels(&timg, 1, &gray0, 1, ch, 1);
        for( int l = 0; l < N; l++ ) {
            if( l == 0 ) {
                cv::Canny(gray0, gray, 0, thresh, 5);
                cv::dilate(gray, gray, cv::Mat(), cv::Point(-1,-1));
            }
            else {
                gray = gray0 >= (l+1)*255/N;
            }
            cv::findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

        }
    }
            cv::Mat pyr1, timg1, gray2(_image1.size(), CV_8U), gray1;
            cv::pyrDown(_image1, pyr1, cv::Size(_image1.cols/2, _image1.rows/2));
            cv::pyrUp(pyr1, timg1, _image1.size());
            std::vector<std::vector<cv::Point> > contours1;
            for( int c = 0; c < 3; c++ ) {
                int ch[] = {c, 0};
                mixChannels(&timg1, 1, &gray2, 1, ch, 1);
                for( int l = 0; l < N; l++ ) {
                    if( l == 0 ) {
                        cv::Canny(gray2, gray1, 0, thresh, 5);
                        cv::dilate(gray1, gray1, cv::Mat(), cv::Point(-1,-1));
                    }
                    else {
                        gray1 = gray2 >= (l+1)*255/N;
                    }
                    cv::findContours(gray1, contours1, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);



                }
            }
    cv::matchShapes(contours, contours1, CV_CONTOURS_MATCH_I1, 0);
 return squares;
}


UIImage *testImage = [UIImage imageNamed:@"note_with_marks (1).png"];    
[self.imageView setImage:testImage];
cv::Mat forground = [testImage CVMat]; 
UIImage *testImage2 = [UIImage imageNamed:@"1.png"];    
cv::Mat forground2 = [testImage2 CVMat];
[self tryingMatchShapes:forground _image1:forground2];
The app is getting crashed on this line:-

    cv::matchShapes(contours, contours1, CV_CONTOURS_MATCH_I1, 0);

Error:-

OpenCV Error: Assertion failed (0 <= i && i < (int)vv.size()) in getMat, file /Users/Aziz/Documents/Projects/opencv_sources/trunk/modules/core/src/matrix.cpp, line 922

I am sure I am doing something wrong in the code but exactly what that I am not able to figure out.

Please some body help me in implementing this method from coding point of view. Please I need some coding help.I have already gone through I lot of theoretical concepts.

Thanks in advance!

How to use cv::matchShapes method from coding point of view in c or c++

Hi all, My objective is to find the pattern in the image captured from camera for this I found the cv::matchShapes method of opencv to implement it.

When I have implemented this method it is making the app crash on matchShapes method.Might be I have done something wrong:-

I exactly do not know how to use this method in order to find the match shape in my query image.This is what I tried.

Please help me in providing some code that can run properly.

Here is my code:-

 - (std::vector<std::vector<cv::Point> >)tryingMatchShapes:(cv::Mat)_image _image1:(cv::Mat)_image1
 {
     std::vector<std::vector<cv::Point> > squares;

     cv::Mat pyr, timg, gray0(_image.size(), CV_8U), gray;
     int thresh = 50, N = 11;
     cv::pyrDown(_image, pyr, cv::Size(_image.cols/2, _image.rows/2));
     cv::pyrUp(pyr, timg, _image.size());
     std::vector<std::vector<cv::Point> > contours;
     for( int c = 0; c < 3; c++ ) {
         int ch[] = {c, 0};
         mixChannels(&timg, 1, &gray0, 1, ch, 1);
         for( int l = 0; l < N; l++ ) {
             if( l == 0 ) {
                 cv::Canny(gray0, gray, 0, thresh, 5);
                 cv::dilate(gray, gray, cv::Mat(), cv::Point(-1,-1));
             }
             else {
                 gray = gray0 >= (l+1)*255/N;
             }
             cv::findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

         }
     }
             cv::Mat pyr1, timg1, gray2(_image1.size(), CV_8U), gray1;
             cv::pyrDown(_image1, pyr1, cv::Size(_image1.cols/2, _image1.rows/2));
             cv::pyrUp(pyr1, timg1, _image1.size());
             std::vector<std::vector<cv::Point> > contours1;
             for( int c = 0; c < 3; c++ ) {
                 int ch[] = {c, 0};
                 mixChannels(&timg1, 1, &gray2, 1, ch, 1);
                 for( int l = 0; l < N; l++ ) {
                     if( l == 0 ) {
                         cv::Canny(gray2, gray1, 0, thresh, 5);
                         cv::dilate(gray1, gray1, cv::Mat(), cv::Point(-1,-1));
                     }
                     else {
                         gray1 = gray2 >= (l+1)*255/N;
                     }
                     cv::findContours(gray1, contours1, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);



                 }
             }
    cv::matchShapes(contours, contours1, for( size_t i = 0; i < contours.size(); i++ )
    {
  double value=  cv::matchShapes(contours[i], contours1[i], CV_CONTOURS_MATCH_I1, 0);
        NSLog(@"%f",value);
    }  
   return squares;
 }


 UIImage *testImage = [UIImage imageNamed:@"note_with_marks (1).png"];    
 [self.imageView setImage:testImage];
 cv::Mat forground = [testImage CVMat]; 
 UIImage *testImage2 = [UIImage imageNamed:@"1.png"];    
 cv::Mat forground2 = [testImage2 CVMat];
 [self tryingMatchShapes:forground _image1:forground2];
The app is getting crashed on this line:-

    cv::matchShapes(contours, contours1, CV_CONTOURS_MATCH_I1, 0);

 Error:-

 OpenCV Error: Assertion failed (0 <= i (contour1.checkVector(2) >= 0 && i < (int)vv.size()) contour2.checkVector(2) >= 0 && (contour1.depth() == CV_32F || contour1.depth() == CV_32S) && contour1.depth() == contour2.depth()) in getMat, matchShapes, file /Users/Aziz/Documents/Projects/opencv_sources/trunk/modules/core/src/matrix.cpp, /Users/Aziz/Documents/Projects/opencv_sources/trunk/modules/imgproc/src/contours.cpp, line 922
1705

I am sure I am doing something wrong in the code but exactly what that I am not able to figure out.

Please some body help me in implementing this method from coding point of view. Please I need some coding help.I have already gone through I lot of theoretical concepts.

Thanks in advance!