OpenMV recognition to find color patches

The main function of OpenMv to find color blocks is to find the corresponding color in the camera screen and select it.




# 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())


  • The code shows how to use OpenMV Cam for monochrome RGB565 color tracking.
  • The threshold index threshold_indexis used to select the color to track (0 is red, 1 is green, 2 is blue).
  • thresholdsList defines the threshold range for color tracking.
  • sensor.reset()Used to reset camera settings.
  • sensor.set_pixformat(sensor.RGB565)Set the image pixel format to RGB565.
  • sensor.set_framesize(sensor.QVGA)Set the frame size to QVGA.
  • sensor.skip_frames(time = 2000)Skip some frames to stabilize the camera.
  • sensor.set_auto_gain(False)Turn off autogain, as color tracking requires it.
  • sensor.set_auto_whitebal(False)Turn off automatic white balance, as color tracking requires it.
  • clock = time.clock()Create a timer object.
  • In the main loop, sensor.snapshot()a frame of image from the camera is obtained.
  • Use img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True)to find the color area that satisfies the threshold condition.
  • For each found region, it is drawn based on its shape features such as edges, major axis, minor axis, rectangular bounding box, and center point intersection.
  • Finally, the frame rate is output.

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)

The main function for finding color blocks. The first parameter thresholds is the color threshold. The second roi is the size of the display screen. If there is no setting, the entire screen is displayed. The third parameter x_stride is the smallest x-direction of the color block being searched. The width in pixels, the default is 2. If you only want to find color blocks with a width of more than 10 pixels, then set this parameter to 10. Color blocks smaller than 10 pixels cannot be found. The fourth parameter y_stride and the third parameter The parameters are the same. When the value of the fifth parameter invert is True, it is the threshold color. For example, when the threshold color is red, it is the color. The sixth parameter area_threshold is the area threshold. If the area of ​​the framed color block is less than this value , containing colors that are not what you want, will be filtered out. The eighth parameter pixels_threshold is the threshold of the number of pixels. If the number of pixels in the color block is less than this value, it will be filtered out. The ninth parameter merge merges. If set to True, the color block of the color you are looking for will be framed in a large frame.

The parameters of threshold thresholds can be used in openmv-ID tools--machine vision--threshold editor--frame buffer area

Adjust the area of ​​the item you want to find the color to white, and the others to black. Copy the parameters of the LAB threshold. This is the color you want to find the color block.

In addition, returning a blob color block object will return the arc of the color block, whether the color block is found and other parameters.

 For some detailed function descriptions, please refer to Finding Color Blocks · OpenMV Chinese Introductory Tutorial

おすすめ

転載: blog.csdn.net/m0_71827453/article/details/133096666