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中文入门教程

猜你喜欢

转载自blog.csdn.net/m0_71827453/article/details/133096666
今日推荐