Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use the raspberry pi camera as video input in c++ openCV?

Tags:

c++

opencv

This is how I usually process video's in openCV.

#include <iostream>
#include<opencv2/opencv.hpp>

int main(int argc, char** argv)
{

        Mat output;
        VideoCapture cap(CV_CAP_ANY);


        if( !cap.isOpened() )
        {
            cout << "Could not initialize capturing...\n";
            return 0;
        }


        while(1){
            cap >> output;

            imshow("webcam input", output);
            char c = (char)waitKey(10);
            if( c == 27 ) break;
          }
}

Now I have a raspberry pi camera and I have the following minimal:

#include <iostream>
#include<opencv2/opencv.hpp>
#include <raspicam/raspicam_cv.h>


int main(int argc, char** argv)
{

        Mat image, output;
        //VideoCapture cap(CV_CAP_ANY);
    raspicam::RaspiCam_Cv cap;

        if( !cap.isOpened() )
        {
            cout << "Could not initialize capturing...\n";
            return 0;
        }

        while(1){
            cap >> output;

            imshow("webcam input", output);
            char c = (char)waitKey(10);
            if( c == 27 ) break;
         }
}

However the latter doesn't work, this is what is output to the terminal when I compile it: http://paste.ubuntu.com/24324541/

Could someone tell me what the correct way to do this is?

Thank you

like image 978
ohiliouh Avatar asked Jan 17 '26 19:01

ohiliouh


1 Answers

According to the documentation, you cannot use it as a stream, but instead must do this:

while(1){
    cap.grab();
    cap.retrieve(output);

    imshow("webcam input", output);
    char c = (char)waitKey(10);
    if( c == 27 ) break;
}
like image 161
Ken Y-N Avatar answered Jan 19 '26 08:01

Ken Y-N



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!