如何使用openCV for iOS检测视频中的方块?

如何使用openCV for iOS检测视频中的方块?,ios,opencv,image-processing,Ios,Opencv,Image Processing,我试图检测视频中的正方形形状,应用程序崩溃并显示以下错误 OpenCV错误:MixChannel文件/Users/alexandershishkov/opencv2.4.3rc/OpenCV/modules/core/src/convert.cpp第472行中的断言失败(j

我试图检测视频中的正方形形状,应用程序崩溃并显示以下错误

OpenCV错误:MixChannel文件/Users/alexandershishkov/opencv2.4.3rc/OpenCV/modules/core/src/convert.cpp第472行中的断言失败(j 这是代码

vector<vector<cv::Point> > squares;
cvtColor(image,image,CV_BGR2GRAY);
GaussianBlur(image,image,cv::Size(9,11),0,0);

find_squares(image, contours);


void find_squares(Mat& image, vector<vector<cv::Point> >& squares)

{

    Mat blurred(image);
    medianBlur(image, blurred, 9);

    Mat gray0(blurred.size(), CV_8U), gray;
    vector<vector<cv::Point> > contours;

    for (int c = 0; c < 3; c++)
   {
     int ch[] = {c, 0};
     mixChannels(&blurred, 1, &gray0, 1, ch, 1);

     const int threshold_level = 2;
     for (int l = 0; l < threshold_level; l++)
     {
        if (l == 0)
        {
            Canny(gray0, gray, 10, 20, 3); 
            dilate(gray, gray, Mat(), cv::Point(-1,-1));
        }
        else
        {
            gray = gray0 >= (l+1) * 255 / threshold_level;
        }

        findContours(gray, contours, CV_RETR_LIST,                 CV_CHAIN_APPROX_SIMPLE);

        vector<cv::Point> approx;
        for (size_t i = 0; i < contours.size(); i++)
        {
            approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

            if (approx.size() == 4 &&
                fabs(contourArea(Mat(approx))) > 1000 &&
                isContourConvex(Mat(approx)))
            {
                double maxCosine = 0;

                for (int j = 2; j < 5; j++)
                {
                    double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                    maxCosine = MAX(maxCosine, cosine);
                }

                if (maxCosine < 0.3)
                    squares.push_back(approx);
            }
        }
     }
  }
}
矢量平方;
CVT颜色(图像、图像、CV_BGR2GRAY);
高斯模糊(图像,图像,cv::大小(9,11),0,0);
找到方格(图像、轮廓);
无效查找方格(材质和图像、向量和方格)
{
Mat模糊(图像);
中间模糊(图像,模糊,9);
Mat灰色0(模糊的大小(),CV_8U),灰色;
矢量等值线;
对于(int c=0;c<3;c++)
{
int ch[]={c,0};
混音通道(&模糊,1,&灰色,0,1,通道,1);
const int threshold_level=2;
对于(int l=0;l=(l+1)*255/阈值\u级;
}
findContours(灰色、等高线、等高线列表、等高线链近似简单);
向量近似;
对于(size_t i=0;i1000&&
isContourConvex(材料(近似)))
{
双最大余弦=0;
对于(int j=2;j<5;j++)
{
双余弦=fabs(角度(约[j%4],约[j-2],约[j-1]);
最大余弦=最大值(最大余弦,余弦);
}
如果(最大余弦<0.3)
正方形。推回(大约);
}
}
}
}
}

模糊和灰色0是单通道图像。所以,您尝试复制第二和第三通道的模糊图像,但不退出!错误应该是因为这个


我希望有帮助。我不知道代码的其余部分以及您想做什么

你能在你的问题中添加堆栈跟踪吗?