0


OpenMV——色块识别

OpenMV有很多示例代码,下面是我学习过程中有关知识的总结。

前言

函数RGB(255,0,0)表示的是红色。

RGB(255,0,0)含义:红色值 Red=255;绿色值 Green=0;蓝色值 Green=0。

常见颜色:

黑色RGB:红色值 Red=0;绿色值 Green=0;蓝色值 Green=0;

蓝色RGB:红色值 Red=0;绿色值 Green=0;蓝色值 Green=255;

绿色RGB:红色值 Red=0;绿色值 Green=255;蓝色值 Green=0;

青色RGB:红色值 Red=0;绿色值 Green=255;蓝色值 Green=255;

一、阈值选择

1.点击工具

2.点击机器视觉,阈值编辑器。

3.滑动下方滑块,当所要识别的色块如下图为白色时,滑块下方的列表(LAB阈值)所显示的阈值就是要是别的色块的阈值。

二.代码

# Single Color RGB565 Blob Tracking Example
#
# This example shows off single color RGB565 tracking using the OpenMV Cam.

import sensor, image, time, math

threshold_index = 0 # 0 for red, 1 for green, 2 for blue

# 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...
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

sensor.reset()                     #初始化相机传感器
sensor.set_pixformat(sensor.RGB565)#设置相机模块的像素模式
sensor.set_framesize(sensor.QVGA)  #设置相机模块的帧大小
                                   #感光元件sensor.VGA 640*480(只用于OpenMV Cam M7的灰度处理图像,或者彩图图像采集,但彩图不能用作图像处理)

sensor.skip_frames(time = 2000)    #跳过2000ms的帧数
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.

while(True):
    clock.tick() #开始追踪运行时间
    img = sensor.snapshot() #使用相机拍摄一张照片,并返回 image 对象
    #find_blob 返回色块对象

    for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
        # 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.
        img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
    print(clock.fps())#停止追踪运行时间,并返回当前FPS(每秒帧数)。

本文转载自: https://blog.csdn.net/qq_61448516/article/details/128764805
版权归原作者 1%593 所有, 如有侵权,请联系我们删除。

“OpenMV——色块识别”的评论:

还没有评论