如何在识别颜色时过滤掉后方小物块,就是同样的颜色只识别大的,如图只识别前面的红绿蓝,不识别后面的两个色块
-
-
# Untitled - By: 86183 - 周五 9月 11 2020 import sensor, image, time, math from pyb import UART threshold_index = 0 #thresholds = thresholds = [(11, 15, 19, 35, -27, 35), #red # (7, 17, -73, 14, -46, -16), #blue # (11, 68, -87, -22, -27, 58)] #green thresholds = [(29, 88, 94, 36, 86, -54), (7, 63, -3, 77, -31, -69), (38, 81, -128, -35, -128, 127) ] sensor.reset() sensor.set_vflip(True) #图像垂直翻转 #截取图像 sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) sensor.set_windowing((0,83,320,97)) sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking clock = time.clock() DetectFlag = False def draw_keypoints(img, kpts): if kpts: print(kpts) img.draw_keypoints(kpts) img = sensor.snapshot() time.sleep(1000) #初始化串口 uart = UART(1, 9600) uart.init(9600, bits=8, parity=None, stop=1) def find_max(blobs): max_size=0 for blob in blobs: if blob.pixels() > max_size: max_blob=blob max_size = blob.pixels() return max_blob #初始化变量 x = 0 green_position = 0 send_1 = 0 #颜色识别模块发送的内容 x_red = 0 x_blue = 0 x_green = 0 Num_Blob = 0 while(True): clock.tick() img = sensor.snapshot() blobs = img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200) if blobs: max_blob = find_max(blobs) img.draw_rectangle(max_blob.rect()) # rect img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy for blob in img.find_blobs([thresholds[0]], pixels_threshold=200, area_threshold=200): x_red = blob.cx() img.draw_rectangle(blob.rect()) for blob in img.find_blobs([thresholds[1]], pixels_threshold=200, area_threshold=200): x_blue = blob.cx() img.draw_rectangle(blob.rect()) for blob in img.find_blobs([thresholds[2]], pixels_threshold=200, area_threshold=200): x_green = blob.cx() img.draw_rectangle(blob.rect()) if(x_red < x_green and x_green < x_blue): #send_1 = 'RGB' send_1 = '321' elif(x_red < x_blue and x_blue < x_green): #send_1 = 'RBG' send_1 = '231' elif(x_green < x_red and x_red < x_blue): #send_1 = 'GRB' send_1 = '312' elif(x_green < x_blue and x_blue < x_red): #send_1 = 'GBR' send_1 = '132' elif(x_blue < x_red and x_red < x_green): #send_1 = 'BRG' send_1 = '213' elif(x_blue < x_green and x_green < x_red): #send_1 = 'BGR' send_1 = '123' #发串口 print('%s' %(send_1)) uart.write('%s' %(send_1)) uart.write('\r\n') #img.draw_rectangle((0,75,321,103))
-
https://book.openmv.cc/project/zhui-xiao-qiu-de-xiao-8f665d28-project-pan-tilt-md.html
参考里面的find_max函数。