现在就是串口调试工具发送1运行1的部分,发送2没有变化怎么修改小白求助
-
# Untitled - By: 15622 - 周四 10月 6 2022 # Single Color RGB565 Blob Tracking Example # This example shows off single color RGB565 tracking using the OpenMV Cam. import sensor, image, time, math import sensor, image, time from pyb import UART #加入 import json #载入库 加入 import pyb import math def modified_data(data): data = int(data) str_data = '' if data < 10: str_data = str_data + '000' + str(data) elif data >= 10 and data < 100: str_data = str_data + '00' + str(data) elif data >=100 and data <1000: str_data = str_data + '0' + str(data) else: str_data = str_data + str(data) return str_data.encode('utf-8') kernel_size = 1 # kernel width= (size*2)+1, kernel height = (size*2)+1,默认不需改 kernel = [-2, -1, 0, \ -1, 1, 1, \ 0, 1, 2] threshold_index = 0 # 0 for red, 1 for green, 2 for blue # Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max) # The below thresholds track in general red/green/blue things. You may wish to tune them... thresholds = [(100, 8, -13, -57, 25, 127), # generic_red_thresholds ] # generic_blue_thresholds sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(time = 300) uart = UART(3, 115200) 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() sj=0 while(True): clock.tick() if uart.any(): sj = uart.read(1) sj=int(sj) # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the # camera resolution. "merge=True" merges all overlapping blobs in the image. while(sj==1): img = sensor.snapshot().lens_corr(1.8 ) #镜头畸变矫正 img.morph(kernel_size, kernel) #核滤波 img.laplacian(1, sharpen=True) #锐化 for c in img.find_circles(threshold = 6001, x_margin = 10, y_margin = 10, r_margin = 10, r_min = 15, r_max = 100, r_step = 2): #只需修改threshold和r_min,增大threshold可以提高准确性,减小可以提高灵敏度(太小会乱框) # 修改r_min值使其只识别大圆,不识别小圆 #if c.r()>15 and c.r()<80: area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r()) #area为识别到的圆的区域,即圆的外接矩形框 img.draw_rectangle(area, color = (255,255,255)) #将圆用白色的矩形框出来 img.draw_cross(c.x(),c.y()) #画十字 mj=4*c.r()*c.r() #矩形框面积 xzb = modified_data(c.x()) yzb = modified_data(c.y()) mjzb = modified_data(mj) uart.write('st') uart.write(xzb) uart.write(yzb) uart.write(mjzb) # 串口发送数据 print(xzb, yzb,mjzb) while(sj==2): img = sensor.snapshot() for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=500, area_threshold=400, merge=True): # These values depend on the blob not being circular - otherwise they will be shaky. if blob.elongation()>0.5: img.draw_edges(blob.min_corners(), color=(255,0,0)) img.draw_cross(blob.cx(), blob.cy()) # Note - the blob rotation is unique to 0-180 only. img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(), blob.cy()) mj=blob.w()*blob.h() xzb = modified_data(blob.cx()) yzb = modified_data(blob.cy()) mjzb = modified_data(mj) uart.write('st') uart.write(xzb) uart.write(yzb) uart.write(mjzb) print(xzb, yzb,mjzb)
-
50行和73行的while改成if