OpenMV识别寻找色块

OpenMv寻找色块的主要功能是在摄像头画面内可以寻找出相应的颜色,并对其框选出来。




# Single Color RGB565 Blob Tracking Example
# 单色RGB565颜色追踪示例

# This example shows off single color RGB565 tracking using the OpenMV Cam.
# 该示例展示了如何使用OpenMV Cam进行单色RGB565颜色追踪。

import sensor, image, time, math

threshold_index = 0 # 0 for red, 1 for green, 2 for blue
# 阈值索引(0表示红色,1表示绿色,2表示蓝色)

# 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...
# 颜色追踪的阈值范围(L最小值,L最大值,A最小值,A最大值,B最小值,B最大值)
# 下面的阈值通常用于追踪红色/绿色/蓝色物体。可以根据需要进行调整...

thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
              (30, 100, -64, -8, -32, 32), # generic_green_thresholds
              (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds
# 这些阈值用于设置对应颜色的最小和最大像素亮度(L通道)、颜色饱和度(A通道)和颜色亮度(B通道)范围。

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.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.
# 只有像素数量大于"pixel_threshold"和区域面积大于"area_threshold"的斑点才会在下面的"find_blobs"函数中返回。
# 如果你改变了相机的分辨率,请相应地调整"pixels_threshold"和"area_threshold"。
# "merge=True"会合并图像中所有重叠的斑点。

while(True):
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):#其中roi没有设置就是整个画面
        # 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_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.
        # 注意:斑点的旋转角度仅在0-180之间唯一。
        img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
    print(clock.fps())


  • 代码展示了如何使用OpenMV Cam进行单色RGB565颜色追踪。
  • 阈值索引threshold_index用于选择要追踪的颜色(0为红色、1为绿色、2为蓝色)。
  • thresholds列表定义了颜色追踪的阈值范围。
  • sensor.reset()用于重置摄像头设置。
  • sensor.set_pixformat(sensor.RGB565)设置图像像素格式为RGB565。
  • sensor.set_framesize(sensor.QVGA)设置帧大小为QVGA。
  • sensor.skip_frames(time = 2000)跳过一些帧以让摄像头稳定。
  • sensor.set_auto_gain(False)关闭自动增益功能,因为颜色追踪需要关闭该功能。
  • sensor.set_auto_whitebal(False)关闭自动白平衡功能,因为颜色追踪需要关闭该功能。
  • clock = time.clock()创建一个计时器对象。
  • 主循环中,通过sensor.snapshot()获取摄像头的一帧图像。
  • 使用img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True)找到满足阈值条件的颜色区域。
  • 对于每个找到的区域,根据其形状特征进行绘制,如边缘、主轴线、次轴线、矩形边界框和中心点交叉线。
  • 最后,输出帧率。

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就是颜色阈值,第二个 roi就是显示画面的大小,无设置的就是显示整个画面,第三个参数x_stride 就是查找的色块的x方向上最小宽度的像素,默认为2,如果你只想查找宽度10个像素以上的色块,那么就设置这个参数为10,小于10像素的色块的无法被找到,第四个参数y_stride和第三个参数同理,第五个 参数invert当值为True的时候就是阈值颜色以为的,例如当阈值颜色为红色的以为颜色,第六个参数area_threshold 面积阈值,如果色块被框起来的面积小于这个值,包含不是想要的颜色,会被过滤掉,第八个参数pixels_threshold 像素个数阈值,如果色块像素数量小于这个值,会被过滤掉。第九个参数merge 合并,如果设置为True,就是所要寻找的颜色的色块都会框出一个大框

阈值thresholds的参数可用openmv-ID的工具--机器视觉--阈值编辑器--帧缓存区

把想要寻找颜色的物品区域调整为白色,其他的为黑色,复制LAB阈值的参数,这就是想要寻找色块的颜色了。

还有返回blob色块对象会返回色块的弧度,是否找到色块等参数

 一些详细函数说明参考寻找色块 · OpenMV中文入门教程

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
OpenMV寻找最大色块的方法是通过调用OpenMV自带的函数img.find_blobs来实现的。一般的思路是将图像视野分为上、中、下三部分,然后寻找最大的黑色色块(或其他指定颜色的色块)。通过调用find_blobs函数并筛选出最大的色块,可以实现寻找最大色块的功能。\[1\] 具体的代码示例如下: blobs = img.find_blobs(\[(30,60,-30,-10,-25,-12)\], pixels_threshold=300, area_threshold=300, merge=False) max_size = 0 if blobs: for blob in blobs: if blob.cy() + 0.5 * blob.h() > max_size: img.draw_rectangle(blob.rect(), (255,0,0)) max_size = blob.cy() + 0.5 * blob.h() row_data\[2\] = int(k * (120 - max_size)) 在这段代码中,首先使用find_blobs函数找到指定颜色的色块,并设置了像素阈值和面积阈值来筛选出符合条件的色块。然后通过遍历找到的色块,找到最大的色块,并在图像上绘制矩形框标记出来。最后根据实际测量设置参数k,根据色块的下边沿y值计算出距离值,并将其存储在row_data\[2\]中。\[3\] 总结起来,OpenMV寻找最大色块的方法是通过调用find_blobs函数并筛选出最大的色块来实现的。这个功能可以用于循迹、识别停止线等应用场景。\[2\] #### 引用[.reference_title] - *1* *2* *3* [2022年电赛C题小车之OpenMV篇](https://blog.csdn.net/weixin_52385589/article/details/126329933)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值