Example: 01-Camera/07-Sensor-Control/sensor_auto_gain_control.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 传感器自动增益控制
#
# 此示例展示了如何控制传感器的增益
# 使用自动增益控制算法。
# What's the difference between gain and exposure control?
#
# Well, by increasing the exposure time for the image you're getting more
# 相机上的光线。这为您提供了最佳的信噪比。
# in general always want to increase the expsoure time... except, when you
# increase the exposure time you decrease the maximum possible frame rate
# and if anything moves in the image it will start to blur more with a
# higher exposure time. Gain control allows you to increase the output per
# pixel using analog and digital multipliers... however, it also amplifies
# noise. So, it's best to let the exposure increase as much as possible
# and then use gain control to make up any remaining ground.
# We can achieve the above by setting a gain ceiling on the automatic
# gain control algorithm. Once this is set the algorithm will have to
# increase the exposure time to meet any gain needs versus using gain
# to do so. However, this comes at the price of the exposure time varying
# more when the lighting changes versus the exposure being constant and
# the gain changing.
import sensor
import time
sensor.reset() # 重置并初始化传感器。
sensor.set_pixformat(sensor.RGB565) # 将像素格式设置为RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # 将帧大小设置为QVGA (320x240)
# OV7725传感器的增益db上限约为24 db。
sensor.set_auto_gain(True, gain_db_ceiling=16.0) # 默认增益。
# Note! If you set the gain ceiling to low without adjusting the exposure control
# target value then you'll just get a lot of oscillation from the exposure
# control if it's on.
sensor.skip_frames(time=2000) # 等待设置生效。
clock = time.clock() # 创建一个时钟对象来跟踪FPS。
while True:
clock.tick() # 更新FPS时钟。
img = sensor.snapshot() # 拍照并返回图像。
print(
"FPS %f, Gain %f dB, Exposure %d us"
% (clock.fps(), sensor.get_gain_db(), sensor.get_exposure_us())
)