Ask Your Question
0

accessing cameras- abort() call

asked 2015-08-21 10:30:25 -0600

Salman gravatar image

updated 2015-08-21 11:01:59 -0600

pklab gravatar image

Hi, I've this simple code which worked fine for my 2 U.S.B webcams, but now for some strange reasons it sometimes stuck and it shows Runtime library error "R6010 abort() has been called". Detaching one of the camera, running the code again makes everything fine and sometimes just closing VS13 repairs it. I find it really absurd pls give any solution.

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int argc, char** argv)
{
VideoCapture cap(1);
VideoCapture cap2(0);

if (!cap.open(1) || !cap2.open(0))
{
    std::cout << " --(!) Error reading images " << std::endl;
    return -1;
}

Mat frame;
Mat frame2;

cap >> frame;
cap2 >> frame2;

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

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-08-21 10:43:34 -0600

pklab gravatar image

updated 2015-08-21 13:12:15 -0600

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 from 1st cam", frame);

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

  if(waitKey(30) >= 0) break;
}
edit flag offensive delete link more

Comments

@pklab with the same logic (without even a single change) it worked precised but now it gives an error :/... Anyhow according to your suggestion I've altered the code, surprisingly the error is removed :) but ... only frame of device 0 is captured, nothing is taken from Device 1 :/ :(

Salman gravatar imageSalman ( 2015-08-21 11:00:44 -0600 )edit

and what is the logic of using AND around the empty(), why not OR? what difference does it make ?

Salman gravatar imageSalman ( 2015-08-21 11:01:06 -0600 )edit

Is device1 currently working ? did you try to use just device 1. Finally try to give a bit of time between grabs with a sleep(5) or simply grab and show from 0 than grab and show from 1. About the logic it's very basic please try it yourself

pklab gravatar imagepklab ( 2015-08-21 11:07:01 -0600 )edit

@pklab the error is somewhere else... its still not working ... for a test, i just tried to run another fine and working code while only laptop builtin camera was working, this code (written below) worked. The moment i plugged in my 1st webcam (now 1 webcam and builtin cam is connected) this code also give a call to abort() .. the same error for previous code.. closing VS13 running code again , makes everything correct again...so its not the code, the error is some where else?? am i right ? 2nd Code:

int main(int, char**)

{ VideoCa1pture cap(0); if (!cap.isOpened()) { std::cerr << "ERROR: Could not open camera" << std::endl; } namedWindow("video", 1); for (;;) { Mat frame; cap >> frame; if (waitKey(30) >= 0) break; }

Salman gravatar imageSalman ( 2015-08-21 11:16:50 -0600 )edit

Correct logic is needed to prevent errors with NULL device and NULL frames. In addiction because device opening done by VideoCapture obj declaration you don't have to open them again... Change this line too if (!cap.isOpened() || !cap2.isOpened()). Finally when you connect a second camera device enumerations would change causing troubles.

pklab gravatar imagepklab ( 2015-08-21 12:02:54 -0600 )edit

@pklab: its still not done :( this is what I've made in accordance with your suggestion, the problem is still the same, either of the 2 cams captures the frame. I want to take picture from both the webcams simultaneously :(

#include "opencv2/opencv.hpp"
using namespace cv;

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

    VideoCapture cap(2);

    if (!cap.isOpened())
    {
        std::cout << " --(!) Error reading image from Device 2 " << std::endl;
        return -1;
    }
    Mat frame;
    cap >> frame;

    if (!frame.empty())
    imshow("Frame of Device 1", frame);

    VideoCapture cap2(0);

    if (!cap2.isOpened())
    {
        std::cout << " --(!) Error reading image from Device 0 " << std::endl;
        return -1;
    }
    Mat frame2;
    cap2 >> frame2;

    if (!frame2.empty())
        imshow("Frame of Device 0", frame2)

    waitKey();
    return;
}
Salman gravatar imageSalman ( 2015-08-21 12:50:03 -0600 )edit

i'm new to OpenCV one thing i just don't understand, one code worked fine one day and the very next day the same code for same cameras gives such errors lol how is that possible.

Salman gravatar imageSalman ( 2015-08-21 13:07:12 -0600 )edit

You can grab the frames consecutively but you will always have a little timeshift because of grabbing time and the cams aren't synchronized. Anyway try the code in my answer it should work.

I think your issues is coming from usb cam device enumeration. As stated when you connect a second camera device enumerations would change (by DirectShow on Windows) causing troubles if not right managed.

pklab gravatar imagepklab ( 2015-08-21 13:11:18 -0600 )edit

@pklab: so how would i add the time delay in between? sleep() method isn't recognized by the editor.

Salman gravatar imageSalman ( 2015-08-21 13:53:40 -0600 )edit

Again... try the code in my answer, it should work. If Ok acept the answer. In case of errors, do some test and/or modification and give us detailed info about. Help us to help you, remeber that this is not a chat.

pklab gravatar imagepklab ( 2015-08-22 03:53:30 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-08-21 10:30:25 -0600

Seen: 1,049 times

Last updated: Aug 21 '15