openmv4颜色识别
find_blobs函数:
通过find_blobs函数可以找到色块.我们来讨论一下,find_blobs的细节。
1 2 3 | image.find_blobs(thresholds, roi = Auto, x_stride = 2 , y_stride = 1 , invert = False , area_threshold = 10 , pixels_threshold = 10 , merge = False , margin = 0 , threshold_cb = None , merge_cb = None ) |
这里的参数比较多。
- thresholds是颜色的阈值,注意:这个参数是一个列表,可以包含多个颜色。如果你只需要一个颜色,那么在这个列表中只需要有一个颜色值,如果你想要多个颜色阈值,那这个列表就需要多个颜色阈值。注意:在返回的色块对象blob可以调用code方法,来判断是什么颜色的色块。
-
12345678
red
=
(xxx,xxx,xxx,xxx,xxx,xxx)
blue
=
(xxx,xxx,xxx,xxx,xxx,xxx)
yellow
=
(xxx,xxx,xxx,xxx,xxx,xxx)
img
=
sensor.snapshot()
red_blobs
=
img.find_blobs([red])
color_blobs
=
img.find_blobs([red,blue, yellow])
-
roi是“感兴趣区”。
left_roi = [0,0,160,240]
blobs = img.find_blobs([red],roi=left_roi) -
x_stride 就是查找的色块的x方向上最小宽度的像素,默认为2,如果你只想查找宽度10个像素以上的色块,那么就设置这个参数为10:
blobs = img.find_blobs([red],x_stride=10)
-
y_stride 就是查找的色块的y方向上最小宽度的像素,默认为1,如果你只想查找宽度5个像素以上的色块,那么就设置这个参数为5:
blobs = img.find_blobs([red],y_stride=5)
- invert 反转阈值,把阈值以外的颜色作为阈值进行查找
- area_threshold 面积阈值,如果色块被框起来的面积小于这个值,会被过滤掉
- pixels_threshold 像素个数阈值,如果色块像素数量小于这个值,会被过滤掉
- merge 合并,如果设置为True,那么合并所有重叠的blob为一个。
注意:这会合并所有的blob,无论是什么颜色的。如果你想混淆多种颜色的blob,只需要分别调用不同颜色阈值的find_blobs。 -
12345
all_blobs
=
img.find_blobs([red,blue,yellow],merge
=
True
)
red_blobs
=
img.find_blobs([red],merge
=
True
)
blue_blobs
=
img.find_blobs([blue],merge
=
True
)
yellow_blobs
=
img.find_blobs([yellow],merge
=
True
)
- margin 边界,如果设置为1,那么两个blobs如果间距1一个像素点,也会被合并。
阈值:
一个颜色阈值的结构是这样的:
1 | red = (minL, maxL, minA, maxA, minB, maxB) |
元组里面的数值分别是L A B 的最大值和最小值。
颜色阈值选择工具:
OpenMV 的IDE里加入了阈值选择工具,极大的方便了对于颜色阈值的调试。
首先运行hello world.py让IDE里的framebuffer显示图案。
然后打开 工具 →机器视觉 → 阈值编译器
点击 Frame Buffer可以获取IDE中的图像,Image File可以自己选择一个图像文件。
拖动六个滑块,可以实时的看到阈值的结果,我们想要的结果就是,将我们的目标颜色变成白色,其他颜色全变为黑色。
单颜色识别之红色:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import sensor, image, time #thresholds = [(66, 0, -51, -8, 6, 127)] #green thresholds = [( 30 , 100 , 15 , 127 , 15 , 127 )] #red #thresholds = [(66, 0, -51, -8, 6, 127),(30, 100, 15, 127, 15, 127)] #元组里面,包含列表,进行多种颜色识别 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. sensor.set_auto_gain( False ) sensor.set_auto_whitebal( False ) #关掉白平衡和自动增益 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. #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200,merge=True): #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200): for blob in img.find_blobs([thresholds[ 0 ]],pixels_threshold = 200 ,area_threshold = 200 ): img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(),blob.cy()) print (clock.fps()) # Note: OpenMV Cam runs about half as fast when connecte |
现象:
单颜色识别之绿色:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import sensor, image, time thresholds = [( 66 , 0 , - 51 , - 8 , 6 , 127 )] #green #thresholds = [(30, 100, 15, 127, 15, 127)] #red #thresholds = [(66, 0, -51, -8, 6, 127),(30, 100, 15, 127, 15, 127)] #元组里面,包含列表,进行多种颜色识别 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. sensor.set_auto_gain( False ) sensor.set_auto_whitebal( False ) #关掉白平衡和自动增益 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. #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200,merge=True): #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200): for blob in img.find_blobs([thresholds[ 0 ]],pixels_threshold = 200 ,area_threshold = 200 ): img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(),blob.cy()) print (clock.fps()) # Note: OpenMV Cam runs about half as fast when connected |
现象:
多颜色识别之合并:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import sensor, image, time #thresholds = [(66, 0, -51, -8, 6, 127)] #green #thresholds = [(30, 100, 15, 127, 15, 127)] #red thresholds = [( 66 , 0 , - 51 , - 8 , 6 , 127 ),( 30 , 100 , 15 , 127 , 15 , 127 )] #元组里面,包含列表,进行多种颜色识别 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. sensor.set_auto_gain( False ) sensor.set_auto_whitebal( False ) #关掉白平衡和自动增益 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. for blob in img.find_blobs(thresholds,pixels_threshold = 200 ,area_threshold = 200 ,merge = True ): #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200): #for blob in img.find_blobs([thresholds[0]],pixels_threshold=200,area_threshold=200): img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(),blob.cy()) print (clock.fps()) # Note: OpenMV Cam runs about half as fast when connected |
现象:
多颜色识别之不合并:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import sensor, image, time #thresholds = [(66, 0, -51, -8, 6, 127)] #green #thresholds = [(30, 100, 15, 127, 15, 127)] #red thresholds = [( 66 , 0 , - 51 , - 8 , 6 , 127 ),( 30 , 100 , 15 , 127 , 15 , 127 )] #元组里面,包含列表,进行多种颜色识别 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. sensor.set_auto_gain( False ) sensor.set_auto_whitebal( False ) #关掉白平衡和自动增益 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. #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200,merge=True): for blob in img.find_blobs(thresholds,pixels_threshold = 200 ,area_threshold = 200 ): #for blob in img.find_blobs([thresholds[0]],pixels_threshold=200,area_threshold=200): img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(),blob.cy()) print (clock.fps()) # Note: OpenMV Cam runs about half as fast when connected |
现象:
正是步行者,一步步登峰!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix