簡體   English   中英

如何使用opencv和c ++從網絡攝像頭框架中找到輪廓?

[英]How to find contours from a webcam frame using opencv and c++?

我的目標是通過從攝像頭捕獲幀來找到輪廓。 我能夠使用靜態圖像來做到這一點,但是后來我嘗試在網絡攝像頭框架中使用相同的概念,這給了我這個錯誤:

"OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN
(type0) && ((1 << type0) & fixedDepthMask) != 0)) in cv::_OutputArray::create, f
ile C:\builds\2_4_PackSlave-win64-vc11-shared\opencv\modules\core\src\matrix.cpp
, line 1486"

這是我用來在程序中查找輪廓的代碼。

 Rng rng(12345);

    Mat captureframe,con,threshold_output;

    vector<vector<Point> > contours; //
   vector<Vec4i> hierarchy;

    while(true)
            {
                capturedevice>>captureframe;
                con = captureframe.clone();
                cvtColor(captureframe,con,CV_BGR2GRAY);

    threshold( con, threshold_output, thresh, 255, THRESH_BINARY );
     findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

     Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );

    for( int i = 0; i< contours.size(); i++ )
    {
    Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );

     drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
    }


    imshow("contour drawing",drawing);
    }

我認為問題出在以下兩行:

con = captureframe.clone();
cvtColor(captureframe,con,CV_BGR2GRAY);

第一行中 ,您將con作為captureFrame的克隆,這意味着con 是一個3通道圖像 ,在第二行中,您正在嘗試將con 設為1通道grayScale圖像,因此您遇到了與圖像類型。

您應該嘗試執行以下操作(我不確定您的代碼是否會在此之后運行,但是在此之后您不應得到當前錯誤):

con.create(captureframe.rows , captureframe.cols, CV_8UC1);
cvtColor(captureframe,con,CV_BGR2GRAY);

伙計們非常感謝您的幫助。 我終於發現我的錯誤,我的聲明中有問題。 我在網上尋找一些參考,然后偶然發現了該代碼以進行對象檢測。 這個人實際上是這樣聲明“輪廓”的:“ std :: vector <std :: vector <cv :: Point>> contours;”而我的聲明是“矢量輪廓”。 我的聲明適用於靜態圖像,但是從網絡攝像頭中找到輪廓時卻給了我這個錯誤。 誰能解釋以上兩個聲明之間的區別? 另外,如skm所建議,我使用con.create(frame.rows,frame.cols,cv_8uc1)將幀捕獲圖像轉換為1通道深度圖像,然后將其轉換為灰度圖像。 這一步非常關鍵。 所以,這是我完整的工作代碼! 謝謝

     VideoCapture capturedevice;
        capturedevice.open(0);

        Mat frame,con;
        Mat grayframe;
         std::vector < std::vector < cv::Point > >contours; //this is very important decalartion


while(true)
        {
            capturedevice>>frame;


            con.create(frame.rows,frame.cols,CV_8UC1);
            cvtColor(frame,con,CV_BGR2GRAY);

            cv::findContours (con, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
            cv::drawContours (frame, contours, -1, cv::Scalar (0, 0, 255), 2);


            imshow("frame", frame);

waitKey(33);
        }

暫無
暫無

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

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