【问题标题】:How to detect squares in a video using openCV for iOS?如何使用适用于 iOS 的 openCV 检测视频中的正方形?
【发布时间】:2015-03-20 11:55:05
【问题描述】:

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

OpenCV 错误:mixChannels 中的断言失败 (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);
            }
        }
     }
  }
}

【问题讨论】:

  • 你可以在你的问题中添加堆栈跟踪吗?

标签: ios opencv image-processing


【解决方案1】:

blurred 和 gray0 是 1 通道图像。因此,您尝试复制不退出的模糊图像的第二和第三通道!错误应该是因为这个。

我希望它有所帮助。我对其余代码以及您想做什么一无所知。

【讨论】:

    猜你喜欢
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多