Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Around empty() you should use AND instead OR so if (! (frame.empty() || frame2.empty()) ) as alternative you could check and show the frame separately

if (!frame.empty())
{
    imshow("Frame of Device 0", frame);
    //imwrite("imageold.jpeg", frame);
}
if (!frame2.empty())
{
    imshow("Frame of Device 1", frame2);
    //imwrite("imagenew.jpeg", frame2);
}

Around empty() you should use AND instead OR so if (! (frame.empty() || frame2.empty()) ) as alternative you could check and show the frame separately

cap >> frame;
if (!frame.empty())
{
    imshow("Frame of Device 0", frame);
    //imwrite("imageold.jpeg", frame);
}
cap2 >> frame2;
if (!frame2.empty())
{
    imshow("Frame of Device 1", frame2);
    //imwrite("imagenew.jpeg", frame2);
}

Around empty() you should use AND instead OR so if (! (frame.empty() || frame2.empty()) ) as alternative you could check and show the frame separately

Edit: Open device is done by object declaration . Below is clear code to check your issue

VideoCapture cap(0);  // open the default camera
VideoCapture cap2(1);   // open camera id=1 (if it exists)

if ( !cap.isOpened() ) // check if we succeeded with 1st cam
{
    std::cout << " --(!) Unable to open 1st cam" << std::endl;  return -1;
}

if ( !cap2.isOpened() ) // check if we succeeded with 2nd cam
{
    // if don't ...try some other device num 
    for(int device = 1; device<10; device++) 
    {
      cap2.open(device);
      if (cap2.isOpened())
      {
          std::cout << " --(i) 2nd cam found as "<<itoa(device) <<"device num" << std::endl;
          break;
      }
    }
}
if ( !cap2.isOpened() ) // check if we succeeded with 2nd cam
{
   std::cout << " --(!) Unable to open 2nd cam" << std::endl;   return -1;
}

Mat frame;  Mat frame2;

cap >> frame;
if (!frame.empty())
frame;    //grab a frame from 1st cam
cap2 >> frame2;  //grab a frame from 2nd cam

if (frame.empty())
{
    std::cout << " --(!) Unable to gram from 1st cam" << std::endl;
}
else 
{
    imshow("Frame of Device 0", frame);
    //imwrite("imageold.jpeg", frame);
}
cap2 >> frame2;
}

if (!frame2.empty())
{
    std::cout << " --(!) Unable to gram from 2nd cam" << std::endl;
else 
{
    imshow("Frame of Device 1", frame2);
    //imwrite("imagenew.jpeg", frame2);
}

Around empty() you should use AND instead OR so if (! (frame.empty() || frame2.empty()) ) as alternative you could check and show the frame separately

Edit: Open device is done by object declaration . Below is clear code to check your issue

VideoCapture cap(0);  // open the default camera
VideoCapture cap2(1);   // open camera id=1 (if it exists)

if ( !cap.isOpened() ) // check if we succeeded with 1st cam
{
    std::cout << " --(!) Unable to open 1st cam" << std::endl;  return -1;
}

if ( !cap2.isOpened() ) // check if we succeeded with 2nd cam
{
    // if don't ...try some other device num 
    for(int device = 1; device<10; device++) 
    {
       cap2.open(device);
       if (cap2.isOpened())
       {
          std::cout << " --(i) 2nd cam found as "<<itoa(device) <<"device num" << std::endl;
          break;
      }
    }
}
  }
    }
    if ( !cap2.isOpened() ) // check again if we succeeded with 2nd cam
{
    {
       std::cout << " --(!) Unable to open 2nd cam" << std::endl;   return -1;
    }
}

Mat frame;  Mat frame2;

cap >> frame;    //grab a frame from 1st cam
cap2 >> frame2;  //grab a frame from 2nd cam

if (frame.empty())
{
    std::cout << " --(!) Unable to gram from 1st cam" << std::endl;
}
else 
{
    imshow("Frame of Device 0", frame);
}

if (!frame2.empty())
{
    std::cout << " --(!) Unable to gram from 2nd cam" << std::endl;
else 
{
    imshow("Frame of Device 1", frame2);
}

Around empty() you should use AND instead OR so if (! (frame.empty() || frame2.empty()) ) as alternative you could check and show the frame separately

Edit: Open device is done by object declaration . Below is clear code to check your issue

VideoCapture cap(0);  // open the default camera
VideoCapture cap2(1);   // open camera id=1 (if it exists)

if ( !cap.isOpened() ) // check if we succeeded with 1st cam
{
    std::cout << " --(!) Unable to open 1st cam" << std::endl;  return -1;
}

if ( !cap2.isOpened() ) // check if we succeeded with 2nd cam
{
    // if don't ...try some other device num 
    for(int device = 1; device<10; device++) 
    {
        cap2.open(device);
        if (cap2.isOpened())
        {
          std::cout << " --(i) 2nd cam found as "<<itoa(device) <<"device num" << std::endl;
          break;
        }
    }
    if ( !cap2.isOpened() ) // check again if we succeeded with 2nd cam
    {
       std::cout << " --(!) Unable to open 2nd cam" << std::endl;   return -1;
    }
}

for(;;)
{
  Mat frame;  Mat frame2;

 cap >> frame;    //grab a frame from 1st cam
 cap2 >> frame2;  //grab a frame from 2nd cam

if (frame.empty())
{
      if (frame.empty()) std::cout << " --(!) Unable to gram from 1st cam" << std::endl;
}
 else 
{
    imshow("Frame of Device 0", frame);
}

if (!frame2.empty())
{
    
  if (frame2.empty()) std::cout << " --(!) Unable to gram from 2nd cam" << std::endl;
 else 
{
    imshow("Frame of Device 1", frame2);

  if(waitKey(30) >= 0) break;
}

Around empty() you should use AND instead OR so if (! (frame.empty() || frame2.empty()) ) as alternative you could check and show the frame separately

Edit: Open device is done by object declaration . Below is clear code to check your issue

VideoCapture cap(0);  // open the default camera
VideoCapture cap2(1);   // open camera id=1 (if it exists)

if ( !cap.isOpened() ) // check if we succeeded with 1st cam
{
    std::cout << " --(!) Unable to open 1st cam" << std::endl;  return -1;
}

if ( !cap2.isOpened() ) // check if we succeeded with 2nd cam
{
    // if don't ...try some other device num 
    for(int device = 1; device<10; device++) 
    {
        cap2.open(device);
        if (cap2.isOpened())
        {
          std::cout << " --(i) 2nd cam found as "<<itoa(device) <<"device num" << std::endl;
          break;
        }
    }
    if ( !cap2.isOpened() ) // check again if we succeeded with 2nd cam
    {
       std::cout << " --(!) Unable to open 2nd cam" << std::endl;   return -1;
    }
}

for(;;)
{
  Mat frame;  Mat frame2;

  cap >> frame;    //grab a frame from 1st cam
  cap2 >> frame2;  //grab a frame from 2nd cam

  if (frame.empty()) std::cout << " --(!) Unable to gram from 1st cam" << std::endl;
  else  imshow("Frame of Device 0", from 1st cam", frame);

  if (frame2.empty()) std::cout << " --(!) Unable to gram from 2nd cam" << std::endl;
  else imshow("Frame of Device 1", from 2nd cam", frame2);

  if(waitKey(30) >= 0) break;
}