Ask Your Question

How to get camera id for VideoCapture?

asked 2015-12-21 00:00:40 -0500

happycoder gravatar image

From the specification page of "VideoCapture" , we know there is one constuctor "VideoCapture (int index);", and "index" here is the camera id which is provided by operating systems. In my computer, there are several cameros, so, how to get their id-s for feeding VideoCapture?

edit retag flag offensive close merge delete


B.T.W, My Operating System is Windows.

happycoder gravatar imagehappycoder ( 2015-12-21 00:36:27 -0500 )edit

EnumerateCameras function in my answer should help

pklab gravatar imagepklab ( 2015-12-21 02:52:13 -0500 )edit

Thanks for your code. It seems a traversing way. It is good! I want to know if there is some other way, e.g. utilize Windows SDK (e.g. DirectShow API or MS Media Foundation API) to get the camera id-s in integer type? (As far as I know, it seems DirectShow API or MF API cannot do this. I'm new to OpenCV or video processing. I may be wrong.) Anyway, you can paste the above code here as an answer if you want.

happycoder gravatar imagehappycoder ( 2015-12-21 03:49:00 -0500 )edit

Yes I used brute force to enumerate all working ID in OpenCV. I can't find any other way to get camera ID for VideoCapture. The function returns a vector of all available ID, you can easily get all IDs as int. Out of OpenCV should be some solution but OpenCV uses CV_CAP_driver as base for cam ID that is specific.

pklab gravatar imagepklab ( 2015-12-21 04:06:54 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-12-21 00:52:16 -0500

nbsrujan gravatar image

From the experience, camera id '0' is for default camera. IN your case it will be 'webcam', if it's working fine. If there are several cameras, then id will me related to USB port number.

edit flag offensive delete link more


Yeah, the machine has several cameras. So, the id-s are the same as the USB port numbers? Thanks! I will do some checking and get back.

happycoder gravatar imagehappycoder ( 2015-12-21 01:04:48 -0500 )edit

Many times it was the same to my surprise. But its better to try once.

nbsrujan gravatar imagenbsrujan ( 2015-12-21 01:06:54 -0500 )edit

did you try it? is that true? I have the same problem.

german_iris gravatar imagegerman_iris ( 2015-12-28 03:10:47 -0500 )edit

Yes, I did try it. Before 2 years.

nbsrujan gravatar imagenbsrujan ( 2016-01-04 01:08:18 -0500 )edit

Question Tools

1 follower


Asked: 2015-12-21 00:00:40 -0500

Seen: 35,007 times

Last updated: Dec 21 '15