Ask Your Question
1

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

asked 2012-08-28 08:09:54 -0500

anyomyous gravatar image

updated 2012-08-29 06:18:28 -0500

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



                    }
                }
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];


    Error:-

    OpenCV Error: Assertion failed (contour1.checkVector(2) >= 0 && contour2.checkVector(2) >= 0 && (contour1.depth() == CV_32F || contour1.depth() == CV_32S) && contour1.depth() == contour2.depth()) in matchShapes, file /Users/Aziz/Documents/Projects/opencv_sources/trunk/modules/imgproc/src/contours.cpp, line 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!

edit retag flag offensive close merge delete

Comments

You mentioned "My objective is to find the pattern in the image captured from camera". Do you mean that you are trying to extract a shape/pattern from the image?

imran gravatar imageimran ( 2012-08-29 16:16:05 -0500 )edit

@imran I have predefined patterns and in the image I captured from camera , I need to detect if those pattern exist or not and if then their position in image.

anyomyous gravatar imageanyomyous ( 2012-08-29 23:02:13 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
-1

answered 2012-09-09 17:07:36 -0500

imran gravatar image

I would recommend you use the Chamfer Distance Transformation for that. There is an example of the chamfer method in the samples folder.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2012-08-28 08:09:54 -0500

Seen: 3,643 times

Last updated: Sep 09 '12