5
5ian
@5ian
0
声望
5
楼层
290
资料浏览
0
粉丝
0
关注
5ian 发布的帖子
-
IDE升级到固件4.5.1后,无法在LCD显示屏上面绘制文字,硬件是官方店铺买的OpenMv 4 Puls和LCD
# Blob Detection Example # # This example shows off how to use the find_blobs function to find color # blobs in the image. This example in particular looks for dark green objects. import sensor, image, time #导入模块 from pid import PID #导入PID控制模块 from pyb import Pin, Timer #导入引脚和定时器 from pyb import UART import display uart = UART(3, 115200) lcd = display.SPIDisplay() # Initialize the lcd screen. sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # use RGB565. sensor.set_framesize(sensor.QQVGA2) # use QQVGA for speed. sensor.skip_frames(10) # Let new settings take affect. sensor.set_auto_whitebal(False) # turn this off. sensor.set_auto_gain(False) # turn this off. #################阈值定义################################ fenbian_thresholds = [ (0, 100, 18, 127, 10, 127),#000-红 (0, 46, -128, -13, 6, 127) ,#001-绿 (0, 39, -128, 127, -128, -10) ]#020-蓝 yidong_thresholds = [ (44, 100, 15, 127, -9, 127),#000-红 (64, 100, -16, 3, 0, 127) ,#001-绿 (65, 100, 6, 127, -128, -18) ]#020-紫 ####################################################### #########电机控制函数############################################## def run(left_speed, right_speed): if inverse_left==True: #判断左轮是否需要反转 left_speed=(-left_speed) if inverse_right==True: #判断右轮是否需要反转 right_speed=(-right_speed) #设置通道PWM,应改为串口发送 l_speed="#006P"+str(int(abs(left_speed)))+"T1000!" uart.write(l_speed) r_speed="#007P"+str(int(abs(right_speed)))+"T1000!" uart.write(r_speed) #########寻找最大色块函数########################################### ####返回最大色块max_blob的blob参数###### def find_max(blobs): max_size=0 for blob in blobs: if blob[2]*blob[3] > max_size: max_blob=blob max_size = blob[2]*blob[3] return max_blob ########变量定义################################################### fenbian_mode=0 #方块 分辨模式 yidong_mode=000 #移动 分辨区域模式 inverse_left=False #将其更改为True以反转左轮 inverse_right=False #将其更改为True以反转右轮 size_threshold = 2000 x_pid = PID(p=0.5, i=1, imax=100) h_pid = PID(p=0.05, i=0.1, imax=50) yidong_finish_flag=0 ################################################################ clock = time.clock() # Tracks FPS. #############主循环############################################### while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() #获取一张图片 lcd.write(img) blobs = img.find_blobs([yidong_thresholds[0]]) if blobs and yidong_mode == 000 and yidong_finish_flag==0 : ##########分辨红色########### yidong_blobs = img.find_blobs([yidong_thresholds[0]]) yidong_max_blob = find_max(yidong_blobs) img.draw_rectangle(yidong_max_blob[0:4]) #绘制矩形 img.draw_cross(yidong_max_blob[5], yidong_max_blob[6]) #绘制中心十字 print('test3') img.draw_string(10,10,"test3",scale=1.2,mono_space=False) if fenbian_mode == 0 : max_blob = find_max(blobs) x_error = max_blob[5]-img.width()/2 #计算x轴离中心点位置 h_error = max_blob[2]*max_blob[3]-size_threshold #距离计算,计算色块面积-滤波面积 img.draw_string(10,50,f"x_error:{x_error}",scale=1.2,mono_space=False) img.draw_rectangle(max_blob[0:4]) #绘制矩形 img.draw_cross(max_blob[5], max_blob[6]) #绘制中心十字 x_output=x_pid.get_pid(x_error,1) #速度计算 h_output=h_pid.get_pid(h_error,1) #速度计算 run(-h_output-x_output+1500,-h_output+x_output-1500) print('test1') print(-h_output-x_output+1500,-h_output+x_output-1500) img.draw_string(10,30,"test1",scale=1.2,mono_space=False) if yidong_max_blob[4]>=3000: run(1500,1500) fenbian_mode=1 yidong_finish_flag=1 elif blobs and yidong_finish_flag==1 : ##################进入分辨模式################## 色块像素大于3000进入 for i in range (0,3) : fenbian_blobs = img.find_blobs([fenbian_thresholds[i]]) if fenbian_blobs : fenbian_max_blob = find_max(fenbian_blobs) img.draw_rectangle(fenbian_max_blob[0:4]) #绘制矩形 img.draw_cross(fenbian_max_blob[5], fenbian_max_blob[6]) #绘制中心十字 print('test2') img.draw_string(10,30,"test2",scale=1.2,mono_space=False) # else: # run(18,-18) #寻找不到色块则原地自转 # if blobs and fenbian_mode==0 : # max_blob = find_max(blobs) # x_error = max_blob[5]-img.width()/2 #计算x轴离中心点位置 # h_error = max_blob[2]*max_blob[3]-size_threshold #滤波,计算色块面积-滤波面积 # img.draw_string(10,10,f"x_error:{x_error}",scale=1.2,mono_space=False) # img.draw_rectangle(max_blob[0:4]) #绘制矩形 # img.draw_cross(max_blob[5], max_blob[6]) #绘制中心十字 # x_output=x_pid.get_pid(x_error,1) #速度计算 # h_output=h_pid.get_pid(h_error,1) #速度计算 # run(-h_output-x_output,-h_output+x_output) # else: # run(18,-18) #寻找不到色块则原地自转![0_1699003044429_IMG_20231103_171641.jpg](https://fcdn.singtown.com/73475913-7e75-4b5a-8299-e9410042ab90.jpg)
-
if blob.elongation() > 0.5: 怎么判定为0.5的?
# Single Color RGB565 Blob Tracking Example # # This example shows off single color RGB565 tracking using the OpenMV Cam. import sensor, image, time, math 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 = [(67, 82, -128, 127, 28, 127)] # generic_red_thresholds 阈值设置,代表LAB的最大值和最小值 sensor.reset() #重置感光元件/重置摄像机 sensor.set_pixformat(sensor.RGB565) #设置模式为彩图 sensor.set_framesize(sensor.QVGA) #设置图像大小为QVGA sensor.skip_frames(time = 2000) #使以上操作生效 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() # 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(True): clock.tick() #开始追踪运行时间 img = sensor.snapshot() #在感光元件截取一张图 for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=320, area_threshold=400, merge=True): #img.find_blobs()函数创建一个class image.blob对象,返回一个包含多个元素的字典 # These values depend on the blob not being circular - otherwise they will be shaky. if blob.elongation() > 0.5: #返回一个介于0和1之间的值,该值表示对象的长度(不是圆形)。一条线将是1。 img.draw_edges(blob.min_corners(), color=(255,0,0)) img.draw_line(blob.major_axis_line(), color=(0,255,0)) img.draw_line(blob.minor_axis_line(), color=(0,0,255)) # These values are stable all the time. img.draw_rectangle(blob.rect()) #识别到颜色则画框框起 img.draw_cross(blob.cx (), blob.cy()) #识别到颜色则画十字 # Note - the blob rotation is unique to 0-180 only. img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20) print(blob) # print(clock.fps())#停止追踪运行时间,并返回当前帧数
-
clock = time.clock() 是什么意思?
# Single Color RGB565 Blob Tracking Example # # This example shows off single color RGB565 tracking using the OpenMV Cam. import sensor, image, time, math 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 = [(67, 82, -128, 127, 28, 127)] # generic_red_thresholds 阈值设置,代表LAB的最大值和最小值 sensor.reset() #重置感光元件/重置摄像机 sensor.set_pixformat(sensor.RGB565) #设置模式为彩图 sensor.set_framesize(sensor.QVGA) #设置图像大小为QVGA sensor.skip_frames(time = 2000) #使以上操作生效 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() # 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(True): clock.tick() #开始追踪运行时间 img = sensor.snapshot() #在感光元件截取一张图 for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=320, area_threshold=400, merge=True): #img.find_blobs()函数创建一个class image.blob对象,返回一个包含多个元素的字典 # These values depend on the blob not being circular - otherwise they will be shaky. if blob.elongation() > 0.5: #返回一个介于0和1之间的值,该值表示对象的长度(不是圆形)。一条线将是1。 img.draw_edges(blob.min_corners(), color=(255,0,0)) img.draw_line(blob.major_axis_line(), color=(0,255,0)) img.draw_line(blob.minor_axis_line(), color=(0,0,255)) # These values are stable all the time. img.draw_rectangle(blob.rect()) #识别到颜色则画框框起 img.draw_cross(blob.cx (), blob.cy()) #识别到颜色则画十字 # Note - the blob rotation is unique to 0-180 only. img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20) print(blob) # print(clock.fps())#停止追踪运行时间,并返回当前帧数