有木有什么好方法分辨出红绿蓝白黑五种颜色
-
黑白色块分辨时,如果只有黑或只有白时色块识别并没有工作是为何呢
我红绿蓝用的是彩图色块识别,黑白用的是灰度
黑白时总是只能识别其中一种颜色import sensor,time import image,math from pyb import LED kernel_size = 1 # kernel width = (size*2)+1, kernel height = (size*2)+1 kernel = [-1, -1, -1,\ -1, +8, -1,\ -1, -1, -1] redthresholds = (29, 99, 19, 127, -128, 127)# grayscale thresholds设置阈值 greenthresholds = (30, 100, -64, -8, -32, 32) bluethresholds = (19, 72, -128, 127, -128, -8) graythresholds = [(65, 128)] whitethresholds = (185, 255) blackthresholds = (0,20) roi=(60,40,40,40) red_color_code = 1 blue_color_code = 2 green_color_code = 3 sensor.reset() # 初始化 sensor. sensor.set_pixformat(sensor.RGB565) # or sensor.RGB565 sensor.set_framesize(sensor.QQVGA) # or sensor.QVGA (or others) sensor.skip_frames(10) # 让新的设置生效 sensor.set_windowing(roi) clock = time.clock() # 追踪FPS while(True): clock.tick() # Track elapsed milliseconds between snapshots(). image = sensor.snapshot() # Take a picture and return the image. blobs = image.find_blobs([redthresholds, greenthresholds, bluethresholds], area_threshold=100) if blobs: print(blobs) for blob in blobs: x = blob[0] y = blob[1] # width = blob[2] # 色块矩形的宽度 height = blob[3] # 色块矩形的高度 center_x = blob[5] # 色块中心点x值 center_y = blob[6] # 色块中心点y值 color_code = blob[8] if color_code == red_color_code: image.draw_rectangle(blob.rect()) #image.draw_rectangle(0, 0, 20, 20,color=greenthresholds,thickness=1,fill=False) image.draw_cross(center_x, center_y) output_str="[%d,%d]" % (blob.cx(),blob.cy()) #方式1 print(output_str) elif color_code == green_color_code: image.draw_rectangle(blob.rect()) image.draw_cross(center_x, center_y) output_str="[%d,%d]" % (blob.cx(),blob.cy()) #方式1 print(output_str) elif color_code == blue_color_code: image.draw_rectangle(blob.rect()) image.draw_cross(center_x, center_y) output_str="[%d,%d]" % (blob.cx(),blob.cy()) #方式1 print(output_str) else : img = sensor.snapshot() img.to_grayscale() img.binary(graythresholds) blob = img.find_blobs([whitethresholds], pixels_threshold=10, area_threshold=10, merge=True) if blob: print('4') else : print('5')
-