赞
踩
目录
备战2023电赛~openmv自适应颜色阈值(附源代码网盘链接)
1. 参考openmv官方例程讲解10-Color-Tracking->image_statistics_info图像统计信息https://book.openmv.cc/example/10-Color-Tracking/image-statistics-info.html
用img.get_statistics函数获取图像统计信息(给get_statistics()函数传递一个“roi =”来获得该区域的统计信息)
2.本函数返回一个一个数据对象,请参见 statistics 对象以获取更多信息(在中文文档里找到statistics ),分别获取RGB565中L A B的众数(这里参考其他up主的思路)
3.根据获取到的3个众数,分别+-20当做自适应的颜色阈值thresholds
4.根据这个颜色阈值进行二值化或者画框验证
最简单解释:取roi的阈值,然后全图就用roi里的阈值
- import sensor, image, time, pyb
-
- yellow_thresholds = (47, 0, 78, 11, 65, -31) #定义变量
- sensor.reset() # Reset and initialize the sensor.
- sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
- sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240)
- sensor.skip_frames(time = 2000) # Wait for settings take effect.
- clock = time.clock() # Create a clock object to track the FPS.
-
- while(True):
- clock.tick() # Update the FPS clock.
- img = sensor.snapshot() # Take a picture and return the image.
- img.draw_rectangle((131, 122, 20, 20), color = (255,255,255)) #画roi框
- statistics_Data = img.get_statistics(roi = (131, 122, 20, 20) )
- # print(statistics_Data)
- # print(statistics_Data.l_mode()) #LAB众数,打印出来看看效果稳定不稳定
- # print(statistics_Data.a_mode())
- # print(statistics_Data.b_mode())
- color_L_Mode = statistics_Data.l_mode() #分别赋值LAB的众数
- color_A_Mode = statistics_Data.a_mode()
- color_B_Mode = statistics_Data.b_mode()
- #计算颜色阈值,这样写的话,颜色阈值是实时变化的,后续想要什么效果可以自己修改
- yellow_thresholds = (color_L_Mode-20, color_L_Mode+20, color_A_Mode-20, \
- color_A_Mode+20, color_B_Mode-20, color_B_Mode+20)
- img.binary([yellow_thresholds]) #二值化看图像效果
- print(yellow_thresholds) #打印输出颜色阈值
- print(clock.fps())
用途:上电采集场地上roi区域内的颜色阈值,当做阈值,可以提取一次或者多次,这样的话,就可以不适用固定的颜色阈值,是动态变化的~~~
其他自行体会~~~
电赛结束终于有空发博客了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。