簡體   English   中英

如何使用iOS的openCV檢測視頻中的正方形?

[英]How to detect squares in a video using openCV for iOS?

我正在嘗試檢測視頻中的正方形形狀,應用程序崩潰並顯示以下錯誤。

OpenCV錯誤:混合通道中的聲明失敗(j <nsrcs && src [j] .depth()==深度),文件/Users/alexandershishkov/opencv2.4.3rc/opencv/modules/core/src/convert.cpp,第472行libc ++ abi.dylib:以cv :: Exception類型的未捕獲異常終止:/Users/alexandershishkov/opencv2.4.3rc/opencv/modules/core/src/convert.cpp:472:錯誤:(-215)j < nsrcs && src [j] .depth()==函數mixChannels中的深度

這是代碼。

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

模糊和gray0是1通道圖像。 因此,您嘗試復制不會退出的模糊圖像的第二和第三通道! 該錯誤應該是因為這樣。

希望對您有所幫助。 我對其余的代碼以及您想做什么一無所知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM