# Hello World Example
#
# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!
import sensor, image, time, pyb
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240)
sensor.set_auto_gain(False)
clock = time.clock() # Create a clock object to track the FPS.
red_led = pyb.LED(2)
red_led.off()
#定义引脚,P1为out口,输出检测结果;P5为in口,接收拍照信号
p_1 = pyb.Pin("P1", pyb.Pin.OUT_PP)
p_1.low()
p_5 = pyb.Pin('P5', pyb.Pin.OUT_PP, pyb.Pin.PULL_DOWN)
p_5.low()
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob.pixels() #获取到目标颜色的像素个数
return max_blob
flag = 0 #传感器状态标记位,0表示未触发,1表示已触发;
#当p_5.value()>0,表示传感器开始触发,相机拍照;
#当p_5.value()<1,表示传感器已经触发过,将flag=1,防止重复拍照
i=0 #缺陷计数
sensor.set_auto_whitebal(False,rgb_gain_db = (0,0,0)) #可调试参数,白平衡,只在改变曝光后使用,只用做一次就好
sensor.set_auto_exposure(False,600) #可调试参数,设置相机曝光
good_1=(30,70,-20,10,-40,-10)## good处想应用RGB颜色通道,应该如何调用?:thinking_face:
#可调试参数
#正常颜色的LAB通道的区间阈值
#L通道为亮度通道,A通道和B通道为检测参数,表示正常颜色的区间阈值
#颜色阈值设定的范围越大,检测越松
#要求:OK片不误检,不报警
#设置检测区域ROI,在初次调试时注释掉,选择合适的ROI
sensor.set_windowing((18,19,211,204))
while(True):
clock.tick()
#print(sensor.get_rgb_gain_db()) #可调试参数,只在调试模式下,白平衡参数获取,只使用一次
if p_5.value()>0 and flag==0: #可调试参数,检测时使用,表示外触发模式
#if 1>0: #可调试参数,测试的时候,在上句if前加 # ,当前if的 # 去掉,表示内触发模式
#内触发/外触发模式,不能同时启用;检测时只能使用外触发
flag = 1
area_1 = 0
img = sensor.snapshot() # 获取当前相机拍摄的一张图像img
#time.sleep_ms(1000) #可调试参数,测试的时候用,检测时注释掉
tongdao = img.get_statistics()
blobs_1=img.find_blobs([good_1], merge=True)
if blobs_1: #查找正常颜色
max_blob_1 = find_max(blobs_1)
img.draw_rectangle(max_blob_1[0:4]) # rect
img.draw_cross(max_blob_1[5], max_blob_1[6]) # cx, cy
area_1 = max_blob_1.pixels() #获取正常颜色的面积,单位:像素
quexian = 0
if tongdao.l_stdev()>13 or (tongdao.l_stdev()>12 and tongdao.b_stdev()>8) : #可调试参数,对于特殊情况的漏检,增加的检测参数
quexian = 0 #quexian = 1代表NG, =0 代表OK
if area_1<20000 or quexian == 1: #如果正常颜色的面积小于设定值,如10000,剔除
#面积设定越大越严,越小越松
red_led.on() #写入的是red红灯,实际是控制的状态灯绿灯
p_1.high() #指示灯-绿灯亮,输出高电平
i=i+1
print(i,"****NG-area:****",area_1)
#缺陷计数,并将缺陷面积显示出来,方便调试
print("a-lq:",tongdao.a_lq(),"a-uq:",tongdao.a_uq(),"a-min:",tongdao.a_min(),"a-max:",tongdao.a_max(),"a-StDev:",tongdao.a_stdev())
print("b-lq:",tongdao.b_lq(),"b-uq:",tongdao.b_uq(),"b-min:",tongdao.a_min(),"b-max:",tongdao.b_max(),"b-StDev:",tongdao.b_stdev())
time.sleep_ms(50) #给出50ms的高电平信号
red_led.off() #拉低电平,恢复默认状态,防止连续剔除
p_1.low()
else:
red_led.off()
p_1.low()
if p_5.value()<1:
flag = 0
@kidswong999 麻烦帮忙看看,怎么使用RGB颜色通道进行色彩识别
https://forum.singtown.com/topic/5858/lab转rgb函数报错lab_to_rgb
注意,RGB是人眼的颜色,对于物体检测的阈值范围不是最好。
print(image.lab_to_rgb((22, -26, 23))),里面传入的是什么参数?LAB的均值、LQ、UQ还是StDev值
@kidswong999 这个print(image.lab_to_rgb((22, -26, 23))),能够详细说说它的构造函数吗?需要传入什么参数,输出什么值?拜托了:folded_hands_light_skin_tone:
@dcqu 这个就是一个公式,把lab转成rgb
你传LAB的均值,结果就是这个均值的rgb的值。
https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=lab_to_rgb#image.lab_to_rgb
import sensor, image, time
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000) # Wait for settings take effect.
clock = time.clock() # Create a clock object to track the FPS.
rgb_threould_low = (10,10,10) #深灰色
rgb_threould_up = (100,100,100) #浅灰色
lab_threould_low = image.rgb_to_lab(rgb_threould_low)
lab_threould_up = image.rgb_to_lab(rgb_threould_up)
while(True):
clock.tick() # Update the FPS clock.
img = sensor.snapshot() # Take a picture and return the image.
print(img.find_blobs([(lab_threould_low[0], lab_threould_up[0]),
(lab_threould_low[1],lab_threould_up[1]),
(lab_threould_low[2],lab_threould_up[2])]))如果你要从图像中计算颜色阈值的话,看这个代码。
https://book.openmv.cc/example/10-Color-Tracking/automatic-rgb565-color-tracking.html