iphone - cv::matchShapes方法实现产生错误

我的目标是在从相机捕获的图像中找到图案,为此,我找到了实现它的opencv的cv::matchShapes方法。

当我实现此方法时,它会使应用程序在matchShapes方法上崩溃。可能是我做错了:-

我完全不知道如何使用此方法在查询图像中找到匹配形状。这就是我尝试过的方法。

这是我的代码:

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

    }     
    }


    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.

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

从编码的 Angular 来看,请一些机构帮助我实现此方法。
请我需要一些编码帮助。我已经研究了很多理论概念。

提前致谢!

最佳答案

std::vector<std::vector<cv::Point> > contours1;

您不能将matchShapes应用于此类类型。您可以将其应用于每个(一个)轮廓,而不适用于轮廓的组(阵列)。例如:
cv::matchShapes(contours[0], contours1[0], CV_CONTOURS_MATCH_I1, 0);

https://stackoverflow.com/questions/12142976/

相关文章:

python - 使用simplecv在多个窗口中显示视频

opencv - 我在哪里可以找到与OpenCV一起使用的面部基准点

opengl - setOpenGlDrawCallback如何调整OpenCV窗口的大小

.net - 如何在图表控件中显示EMGU直方图?

android - opencv,链接C静态库../lib/armeabi-v7a/liblibjp

matlab - 快速图像扫描

docker - 如何忽略要在 docker build 上下文中发送的文件夹

python - typeerror参数 'image'必须为iplimage

python - openCV的特征脸

android - 在Android中使用OpenCV运行C/C++代码