Ask Your Question
0

[OpenCV]How to detect and connect camera automatically

asked 2016-12-06 20:24:03 -0600

Jack_Wellem gravatar image

How can i detect and connect camera automatically?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2016-12-07 01:43:48 -0600

pi-null-mezon gravatar image

updated 2016-12-07 01:45:57 -0600

For the instance you can write a function that will try to open video capture devices by enurator started from the 0:

cv::VideoCapture _videocapture;
std::vector<std::pair<int,cv::String>> v_videodevices
int _devid = 0;
while(true) {
   if(_videocapture.open(_devid)) { 
      v_devices.push_back( std::make_pair<int,cv::String>(_devid, "Device " + std::to_string(_devid)) );
      _devid++;
   } else {
      break;
   }        
}
_videocapture.release();

Alternativelly you can use Qt library and QCamera class to enumerate all available video devices.

edit flag offensive delete link more
0

answered 2016-12-19 05:39:18 -0600

KirtiSwagat gravatar image

updated 2016-12-19 05:40:16 -0600

VideoCapture capture(0);

if(!capture.isOpened())
{
return (-1);
}
double width= capture.get(CV_CAP_PROP_FRAME_WIDTH);//u can get the width of the frame
double height= capture.get(CV_CAP_PROP_FRAME_HEIGHT);;//u can get the height of the frame
Mat Frame;
for( ; ; )
{
capture>> Frame;
if(!Frame.empty())
{
do what ever u want with the captured frames
}
if(waitKey(30) == 27)//since esc key ASCII Value is 27 and if it is pressed for 30 ms it will stop the camera
{
cout<<"Esc key is pressed"<<endl;
break;
}

By default set the device Id= 0. So that automatically it will detect the camera.

edit flag offensive delete link more

Comments

Thank you for your answer. But if I connect the device like this:not device --->connect device---->disconnect device-->connect device Could you give me some advices?

Jack_Wellem gravatar imageJack_Wellem ( 2016-12-22 23:42:03 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-12-06 20:24:03 -0600

Seen: 5,526 times

Last updated: Dec 19 '16