@kidswong999 之前使用的就是阈值编辑器,但是因为光源有一点的变化就不能识别了,所以现在用直方图,怎么更好的取最大最小值
S
soc1 发布的帖子
-
RE: openMV IDE右侧的LAB直方图怎么取最大最小值?
-
怎么更好的识别橙色球?
当识别橙色球的时候,总是和红色球分不清,请问怎么解决?
import sensor, image, time import json from pyb import UART from pyb import LED#定义led灯 red_led = LED(1) green_led = LED(2) blue_led = LED(3) #颜色的阈值需要根据现场的环境不同而做出更改(thresholds = (minL, maxL, minA, maxA, minB, maxB)) thresholds = [(31, 53, 31, 127, 22, 126), #赤 (30, 55, 3, 46, 28, 58), #橙 (55, 87, -13, 35, 14, 32), #黄 (13, 28, -128, -7, -128, 12), #绿 (51, 95, -58, -3, -56, -13), #蓝 ] #(RGB颜色值代码) color_thresholds=[(255,0,0), #赤 (255,215,0), #橙 (255,255,0), #黄 (0,255,0), #绿 (0,0,255), #蓝 ] sensor.reset() #初始化感光元件 sensor.set_pixformat(sensor.RGB565)#设置图像为彩色 sensor.set_framesize(sensor.QQVGA) #设置图像格式为QVGA:320X240 或者:QQVGA:160X120 sensor.skip_frames(time = 2000) #让新设置生效 sensor.set_auto_gain(False) #必须关闭颜色跟踪 sensor.set_auto_whitebal(False) #必须关闭颜色跟踪 #关闭白平衡,白平衡默认是开启的,在颜色识别中,需要关闭白平衡 clock = time.clock() uart = UART(3, 115200)#开启串口3,把球体得坐标发送出去,使用特定的波特率初始化 uart.init(115200, bits=8, parity=None, stop=1)#串口初始化 clock = time.clock() # Tracks FPS. new_roi=(0,0,160,120) ''' 扩宽roi ''' def expand_roi(roi): #找到特征区域颜色 # set for QQVGA 160*120或者使用 QVGA:320*240 extra = 5 win_size = (160, 120) (x, y, width, height) = roi new_roi = [x-extra, y-extra, width+2*extra, height+2*extra] if new_roi[0] < 0: new_roi[0] = 0 if new_roi[1] < 0: new_roi[1] = 0 if new_roi[2] > win_size[0]: new_roi[2] = win_size[0] if new_roi[3] > win_size[1]: new_roi[3] = win_size[1] return tuple(new_roi) def Uart_str(blobs,j): #串口发送字符压缩函数 if blobs.x() < 10: str1="00%d"%blobs.x() elif blobs.x()<100: str1="0%d"%blobs.x() else: str1="%d"%blobs.x() if blobs.y()<10: str2="00%d"%blobs.y() elif blobs.y()<100: str2="0%d"%blobs.y() else: str2="%d"%blobs.y() str3="S"+"X"+str1+"Y"+str2+"%d"%j+"s" return str3 i=0 #定义变量i,即为颜色代表0-赤 10-粉 while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot().lens_corr(1.8) #读取图片 if i >= 5: #10种颜色 i = 0 #大于10 归0 blobs = img.find_blobs([thresholds[i]],area_threshold=20,pixels_threshold=50,x_stride=2,y_stride=2) #找到对应颜色区域,area_threshold表示面积最小值,可通过面积过滤一些微小的颜色点 #print(blobs) i=i+1 red_led.on() #正常工作状态红灯亮起 if blobs: red_led.off() green_led.off() blue_led.on() for blob in blobs: is_circle = False max_circle = None max_radius = -1 new_roi = expand_roi(blob.rect())#限定识别区域 for c in img.find_circles(threshold =50, x_margin = 2, y_margin = 2, r_margin = 2,r_min = 1, r_max =10, roi=new_roi): #找圆函数threshold = 3500比较合适。如果视野中检测到的圆过多,请增大阈值; #相反,如果视野中检测到的圆过少,请减少阈值。 is_circle = True if c.r() > max_radius: max_radius = c.r() max_circle = c if is_circle: img.draw_cross(max_circle.x(), max_circle.y(),color = color_thresholds[i-1]) #画十字 img.draw_circle(max_circle.x(), max_circle.y(), max_circle.r() + 1, color = color_thresholds[i-1])#画圆 output_str=Uart_str(max_circle,i-1) print(output_str) uart.write(output_str) red_led.off() green_led.on() blue_led.off() #print(clock.fps())