How to count cameras in OpenCV 2.3?

后端 未结 6 1202
难免孤独
难免孤独 2020-12-01 14:36

I want to get the number of available cameras.

I tried to count cameras like this:

for(int device = 0; device<10; device++) 
{
    VideoCapture ca         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 14:46

    I do this in Python:

    def count_cameras():
        for i in range(10):
            temp_camera = cv.CreateCameraCapture(i-1)
            temp_frame = cv.QueryFrame(temp_camera)
            del(temp_camera)
            if temp_frame==None:
                del(temp_frame)
                return i-1 #MacbookPro counts embedded webcam twice
    

    Sadly Opencv opens the Camera object anyway, even if there is nothing there, but if you try to extract its content, there will be nothing to attribute to. You can use that to check your number of cameras. It works in every platform I tested so it is good.

    The reason for returning i-1 is that MacBookPro Counts its own embedded camera twice.

提交回复
热议问题