Use OpenCV's VideoCapture to adjust the focus parameters of the USB camera

1. Problem introduction

        We may choose to purchase some USB cameras as acquisition devices before performing machine vision image acquisition tasks. And some USB cameras have the function of auto focus, open the camera that comes with Windows, the interface is as follows:

         After the camera with the function of adjusting the focus is connected, a focus button will appear on the far left (the red frame has been circled), click the button and drag the vertical slide bar to adjust the focus position of the camera until the clearest position we want ; You can also pull down the slide bar to the bottom, which is the auto-focus strategy, and the camera will auto-focus according to the current image resolution.

        The above operations are very simple in the Windows system camera application, but for a certain machine vision task, we often hope to use OpenCV library programming to implement manual or auto-focus commands.

2. Problem solving

        Here we use opencv-python, and it is not difficult for students who use C++ to find the corresponding functions. First, we create a camera class Camera, instantiate a self.stream with cv2.VideoCapture in the class, and then use the set method of self.stream to set the properties of the camera.

class Camera:
    def __init__(self, src=0):
        self.src = src
        self.stream = cv2.VideoCapture(src, cv2.CAP_MSMF)
        if self.stream.isOpened():
            # (2592,1944)此处根据自己相机的像素值进行修改
            self.stream.set(cv2.CAP_PROP_FRAME_WIDTH, 2592)
            self.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, 1944)
            self.stream.set(cv2.CAP_PROP_FOCUS,500) # 此处即为修改相机对焦参数的命令
        else:
            self.stream = cv2.VideoCapture(self.src + 1)
            self.stream.set(cv2.CAP_PROP_FRAME_WIDTH, 2592)
            self.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, 1944)
            self.stream.set(cv2.CAP_PROP_FOCUS,500)

        As you can see from the above code, we use self.stream.set(cv2.CAP_PROP_FOCUS, 500) to modify the camera's focus position. Through consulting the data and experiments, it is found that the value range of this parameter is 0-1023 , which represents the focus position of the camera from near to far, and 1023 represents the focus at infinity.

        Let the camera auto focus, the method is also relatively simple, just set(cv2.CAP_PROP_AUTOFOCUS, 1), where the value is 1, which means auto focus is turned on, and the value is 0, which means auto focus is turned off. code show as below:

self.stream.set(cv2.CAP_PROP_AUTOFOCUS, 1)

3. Matters needing attention

        Using the above method, you can realize the auto focus and manual focus of the USB camera. But it should be noted that the focus adjustment method is only applicable and effective on cameras with focus adjustment function. The above set method will return True after the call is successful. If the fixed-focus lens cannot be called successfully, use the set method to adjust focus and return False .

Guess you like

Origin blog.csdn.net/m0_57315535/article/details/130192451