Sensor 模式查询 + 手动增益调节,图像控制更精准

Viewed 79

问题描述


大家好!CanMV K230/K230D 又双叒叕更新啦!本次我们带来了两个超实用的新功能,让你的图像采集控制更加得心应手:

  1. Sensor.list_mode() - 无需初始化,提前查看传感器支持的所有模式
  2. 手动增益调节 - 支持直接设置模拟增益(Again),光线控制更精准

话不多说,让我们一起来看看吧!

一、新功能一:Sensor.list_mode() - 模式查询不求人

痛点场景
你是否曾经遇到过这样的困扰:

# 以前的方式:必须先初始化 sensor,默认使用1920x1080,30fps
sensor = Sensor(id=2, width=xxx, height=xxx, fps=xxx)
sensor.reset()

新功能来了!
现在,无需初始化,直接查询传感器支持的所有模式:

make from media.sensor import Sensor

# 在传感器初始化之前调用
sensor_name, modes = Sensor.list_mode(id=2)
print(f"传感器:{sensor_name}")

if modes:
    print(f"共支持 {len(modes)} 种模式:")
    for i, mode in enumerate(modes):
        print(f"{i}: {mode['width']}x{mode['height']}@{mode['fps']}fps")from media.sensor import Sensor

# 在传感器初始化之前调用
sensor_name, modes = Sensor.list_mode(id=2)
print(f"传感器:{sensor_name}")

if modes:
    print(f"共支持 {len(modes)} 种模式:")
    for i, mode in enumerate(modes):
        print(f"{i}: {mode['width']}x{mode['height']}@{mode['fps']}fps")

输出示例


Sensor Mode List (CSI 2, gc2093_csi2):
---------------------------------------------
Index  Resolution      FPS   
---------------------------------------------
0      1920x1080       30    
1      1920x1080       60    
2      1280x960        90    
3      1280x720        90    
---------------------------------------------
Total: 4 modes
传感器:gc2093_csi2
1920x1080@30fps
1920x1080@60fps
1280x960@90fps
1280x720@90fps

使用技巧

根据查询结果,选择合适的分辨率:

# 选择 1080P30 模式
for mode in modes:
    if mode['width'] == 1920 and mode['height'] == 1080 and mode['fps'] == 30:
        print("找到 1080P30 模式,开始初始化...")
        break

# 然后再初始化传感器
sensor = Sensor(id=2, width=mode['width'], height=mode['height'], fps=mode['fps'])
sensor.reset()

二、新功能二:(Again)调节 - 光线控制更精准

为什么需要手动增益?

在低光环境下,自动增益可能会让画面噪点增多;在强光下,又可能曝光不足。手动调节增益,让你对画面亮度有完全的控制权!

新增 API

1)sensor.get_again_range() - 获取增益范围

from media.sensor import Sensor

sensor = Sensor(id=2)
sensor.reset()
sensor.run()

# 获取增益范围
gain_range = sensor.get_again_range()
if gain_range:
    min_gain = gain_range['min']
    max_gain = gain_range['max']
    step = gain_range['step']
    print(f"增益范围:{min_gain:.2f} - {max_gain:.2f}")
    print(f"步进值:{step:.6f}")

2)sensor.again(value) - 设置增益值

直接传 float 值!

# 获取当前增益
current_gain = sensor.again()
print("当前增益:", current_gain.gain[0])

# 设置增益为 4.0
sensor.again(4.0)

手动增益调节 Demo

SDK中有完整示例

/src/canmv/resources/examples/17-Sensor/camera_manual_again_lcd.py

img = sensor.snapshot(chn=CAM_CHN_ID_0)
Display.show_image(img)

# 每 60 帧(约 2 秒)调整一次增益
if frame_count % 60 == 0:
    current_gain = sensor.again()
    print(f"[帧 {frame_count}] 当前增益:{current_gain.gain[0]:.2f}")
    
    # 设置新增益值
    print(f"  → 设置增益:{current_gain_value:.2f}")
    sensor.again(current_gain_value)
    
    # 计算下一个增益值(按步长递增/递减)
    current_gain_value += gain_range['step'] * gain_direction
    
    # 检查是否超出范围
    if current_gain_value >= gain_range['max']:
        current_gain_value = gain_range['max']
        gain_direction = -1
        print(f"  → 达到最大增益,开始递减")
    elif current_gain_value <= gain_range['min']:
        current_gain_value = gain_range['min']
        gain_direction = 1
        print(f"  → 达到最小增益,开始递增")

三、注意事项

1. 关于list_mode()

  • 静态方法,使用 Sensor.list_mode() 调用

  • 无需初始化,可在 Sensor() 之前调用

  • 需要传入正确的 id 参数(0-2,对应不同的 CSI 接口)

2. 关于增益调节

  • 必须在 sensor.run() 之后调用

  • 建议先调用 get_again_range() 获取范围,避免设置无效值

  • 增益过大可能会增加画面噪点,请根据实际场景调整

  • 在使用之前可以用sensor.auto_exposure(False)关闭曝光和增益调节,否则手动设置的值会被自动设置的值盖掉

赶快更新你的固件,体验新功能吧!

四、参考文档

CanMV sensor API 文档:

https://www.kendryte.com/k230_canmv/zh/main/zh/api/mpp/K230_CanMV_Sensor%E6%A8%A1%E5%9D%97API%E6%89%8B%E5%86%8C.html

CanMV最新固件:

https://kendryte-download.canaan-creative.com/developer/releases/canmv_k230_micropython/daily_build/

1 Answers