Ask Your Question
0

capture and save with 2 webcams C++

asked 2017-05-14 08:18:22 -0500

thh15 gravatar image

updated 2017-05-15 13:54:26 -0500

berak gravatar image

what do i need to add to my code in order to take the second picture as well ? right now it works only with one webcam, but i can see both on my screen. Thanks !

#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
    //initialize and allocate memory to load the video stream from camera 
    cv::VideoCapture camera0(2);
    cv::VideoCapture camera1(1);

    if( !camera0.isOpened() ) return 1;
    if( !camera1.isOpened() ) return 1;

    while(true) {
        //grab and retrieve each frames of the video sequentially 
        cv::Mat3b frame0;
        camera0 >> frame0;
        cv::Mat3b frame1;
        camera1 >> frame1;

        cv::imshow("Video0", frame0);
        cv::imshow("Video1", frame1);

        //wait for 40 milliseconds
        int c = cvWaitKey(40);

        //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
        if(27 == char(c)) break;
    }

    // Get the frame
Mat save_img; camera0 >> save_img;
if(save_img.empty())
{
  std::cerr << "Something is wrong with the webcam, could not get frame." << std::endl;
}
// Save the frame into a file
imwrite("test1.jpg", save_img); // A JPG FILE IS BEING SAVED

    return 0;
}
edit retag flag offensive close merge delete

Comments

index starts at 0. Have you got three webcams ?

cv::VideoCapture camera0(2);
cv::VideoCapture camera1(1);

. It means that you don't use first webcam. try :

cv::VideoCapture camera0(0);
cv::VideoCapture camera1(1);

and it is better to use cv::Mat frame0; instead of cv::Mat3b frame0; camera0 >> frame0; manage memory.

and here

Mat save_img; camera0 >> save_img;
if(save_img.empty())
{
  std::cerr << "Something is wrong with the webcam, could not get frame." << std::endl;
}
// Save the frame into a file
imwrite("test1.jpg", save_img); // A JPG FILE IS BEING SAVED

    return 0;
}

save_img is empty and test1.jpg must be an empty file (or only a header)

LBerger gravatar imageLBerger ( 2017-05-14 08:42:32 -0500 )edit

thanks for replying. im using 1,2 because i dont want my laptops webcam. i didnt really understand, i want to save one picture from each webcam, so what i should add to the code? thanks !

thh15 gravatar imagethh15 ( 2017-05-14 11:32:43 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-05-14 12:03:12 -0500

LBerger gravatar image

Something like this should work :

int main()
{

//initialize and allocate memory to load the video stream from camera 
cv::VideoCapture camera0(2);
cv::VideoCapture camera1(1);

if( !camera0.isOpened() ) return 1;
if( !camera1.isOpened() ) return 1;
int nbImage=0;
while(true) {
    //grab and retrieve each frames of the video sequentially 
    cv::Mat frame0;
    camera0 >> frame0;
    cv::Mat frame1;
    camera1 >> frame1;

    cv::imshow("Video0", frame0);
    cv::imshow("Video1", frame1);

    //wait for 40 milliseconds
    int c = cvWaitKey(40);
    if ((static_cast<char>(c)=='s')
   {
       imwrite(format("image0_%d.jpg",nbImage),frame0);
       imwrite(format("image1_%d.jpg",nbImage++),frame1);
   }
    //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
    if(27 == char(c)) break;
}

platform and os opencv_version ?

edit flag offensive delete link more

Comments

thanks again for helping, its working ! :) im using visual studio 2012 & opencv 2.4.13.

thh15 gravatar imagethh15 ( 2017-05-14 12:12:00 -0500 )edit

What do i need to change to get higher quality pictures ?

thh15 gravatar imagethh15 ( 2017-05-15 07:08:19 -0500 )edit

You mean resolution ? example :

camera0.set(CV_CAP_PROP_FRAME_WIDTH ,1280);
camera0.set(CV_CAP_PROP_FRAME_HEIGHT ,960);
LBerger gravatar imageLBerger ( 2017-05-15 07:14:50 -0500 )edit
1

thanks ! !

thh15 gravatar imagethh15 ( 2017-05-15 07:32:03 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-05-14 08:17:09 -0500

Seen: 2,460 times

Last updated: May 14 '17