capture and save with 2 webcams C++
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;
}
index starts at 0. Have you got three webcams ?
. It means that you don't use first webcam. try :
and it is better to use cv::Mat frame0; instead of cv::Mat3b frame0; camera0 >> frame0; manage memory.
and here
save_img is empty and test1.jpg must be an empty file (or only a header)
thanks for replying. im using 1,2 because i don
t want my laptops webcam. i didn
t really understand, i want to save one picture from each webcam, so what i should add to the code? thanks !