logo

在不同颜色卡纸面前打开摄像头,画面会变不同颜色

2020-10-14 12:54
M
xnum

我openmv颜色检测,发现一个问题,如果镜头面前没有任何颜色的卡纸,连接以后画面颜色正常,如果把黄色卡纸放到镜头面前,再连接,画面必变蓝,放绿色卡纸,画面必变红,这是什么情况?运行hello world也是一样变色

0e66bdd9-1a9b-4017-9455-e4ab0dd10794.jpg
9a552377-177d-4558-b58b-db79cdb80881.jpg
6bb81cf8-18d2-4bb0-8d25-3a07390f0f8d.jpg
import sensor, image, time, json, pyb
from pyb import UART

#led = pyb.LED(3)
#usb = pyb.USB_VCP()
#while (usb.isconnected()==False):
   #led.on()
   #time.sleep(150)
   #led.off()
   #time.sleep(100)
   #led.on()
   #time.sleep(150)
   #led.off()
   #time.sleep(600)

green_threshold  = (25, 50, -48, -25, 12, 37) #定义绿色阈值
yellow_threshold = (45, 65, -10, 9, 27, 60)
gray_threshold = (22, 34, -12, 4, -4, 30)
sensor.reset()
sensor.set_pixformat(sensor.RGB565) #使用RGB565
sensor.set_framesize(sensor.QVGA) #使用QQVGA
sensor.skip_frames(10) #延迟
sensor.set_auto_whitebal(False)
clock = time.clock()
uart = UART(3, 9600)  #定义波特率

def find_max(blobs):     #寻找最大色块
    max_size=0
    for blob in blobs:
        if blob[2]*blob[3] > max_size:
            max_blob=blob
            max_size = blob[2]*blob[3]
    return max_blob

while(True):
    clock.tick()
    img = sensor.snapshot()
    time.sleep(1500)
    blobs1 = img.find_blobs([green_threshold],area_threshold=2000)
    blobs2 = img.find_blobs([yellow_threshold],area_threshold=2000)
    blobs3 = img.find_blobs([gray_threshold],area_threshold=2000)
    if blobs1:
        max_blob = find_max(blobs1)   #调用函数
        img.draw_rectangle(max_blob.rect())    #框出图案
        img.draw_cross(max_blob.cx(), max_blob.cy())
        output_str = '1'     #发出信号
        print(output_str)
        uart.write(output_str+'\r\n')
    elif blobs2:
        max_blob = find_max(blobs2)   #调用函数
        img.draw_rectangle(max_blob.rect())    #框出图案
        img.draw_cross(max_blob.cx(), max_blob.cy())
        output_str = '2'     #发出信号
        print(output_str)
        uart.write(output_str+'\r\n')
    #elif blobs3:
        #max_blob = find_max(blobs3)   #调用函数
        #img.draw_rectangle(max_blob.rect())    #框出图案
        #img.draw_cross(max_blob.cx(), max_blob.cy())
        #output_str = '3'     #发出信号
        #print(output_str)
        #uart.write(output_str+'\r\n')
    else:
        output_str = '4'     #发出信号
        print(output_str)
        uart.write(output_str+'\r\n')
2020-10-14 14:43
I
网红小智智 星瞳科技

是白平衡的问题。OpenMV开机会自动调节白平衡,但是你的代码没有给足够时间。

22行改为

sensor.skip_frames(time = 1000) #延迟
2020-10-14 17:37
M
xnum

@kidswong999 改好后,依然变色,只不过是卡纸一放过去就变色,拿开就变回正常

M
xnum

@kidswong999 而且颜色也不识别了

2020-10-15 13:28
I
网红小智智 星瞳科技

确实是白平衡的问题。因为OpenMV开机会自动调节白平衡,如果一开机就全部是红色的,白平衡就会自动偏蓝。

解决办法:手动设置白平衡。

代码:

import sensor,time
sensor.reset()
sensor.set_pixformat(sensor.RGB565) #使用RGB565
sensor.set_framesize(sensor.QVGA) #使用QQVGA
sensor.set_auto_whitebal(False, rgb_gain_db = (-6.02073, -5.886325, -4.30291))
clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    print(clock.fps())

这个rgb_gain_db的参数是我的环境正常的,颜色比较鲜艳。如果你想修改,可以运行这个代码获得:https://book.openmv.cc/example/21-Sensor-Control/sensor-manual-whitebal-control.html