2

K210 调节颜色阈值识别红绿黄三色 - Sound_Sleep

 9 months ago
source link: https://www.cnblogs.com/Sound-Sleep/p/k210ColorRecognize.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

K210 调节颜色阈值识别红绿黄三色

官方在机器视觉的API中提供了寻找绿色色块的例程

https://wiki.sipeed.com/soft/maixpy/zh/api_reference/machine_vision/image/image.html#例程

import sensor
import image
import lcd
import time
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
green_threshold   = (0,   80,  -70,   -10,   -0,   30) # 颜色阈值元组
while True:
    img=sensor.snapshot() # 捕获一张图片,返回一个图片对象
    # 调用寻找色块方法,传入颜色阈值列表,返回满足要求的色块列表
    blobs = img.find_blobs([green_threshold])
    if blobs: # 当色块列表不为空时对色块列表进行遍历
        for b in blobs:
	        # start 1
            tmp=img.draw_rectangle(b[0:4]) # 传入xy和宽度、高度将色块用矩形框选
            tmp=img.draw_cross(b[5], b[6]) # 传入中心处的xy坐标在中心处画一个十字
            # end 1
            c=img.get_pixel(b[5], b[6]) # 返回中心处的灰度像素值
    lcd.display(img) # lcd 显示照片

同样在官方 API 中提到色块列表中的每一个色块成员可以用索引的方式获取相应数值:

3060531-20230810215913081-1981063700.png

不仅可以用索引号来获取相应数值,还调用方法,start 1 到 end 1 的代码也可以被替换为:

tmp = img.draw_rectangle(b.x(), b,y(), b.w(), b.h())
tmp = img.draw_cross(b.cx(), b.cy())

这样转换后代码可读性可以更高

读懂例程后不难发现整个代码最核心的地方就是 green_threshold 这个元组的值的确定,也就是颜色阈值的确定,颜色阈值的确定可以借助 Maixpy IDE 中的 阈值编辑器

3060531-20230810215443446-1827746211.png
3060531-20230810215447464-1560109140.png

右边未被筛选掉的色块是白色,我们要做的就是只让需要被识别的颜色呈现白色,所以选取的图片最好包含多种颜色,以精确调整阈值防止误识别

比方说我要识别红色可以将阈值调整成如下模样:

3060531-20230810215455746-2101976908.png

将下面的 LAB 阈值元组复制粘贴赋值给 red_threshold 变量,同理生成 yellow_threshold 和 green_threshold 的颜色阈值

下面是自己根据例程和 API 修改延伸的一个识别红绿黄三色的程序:

import sensor
import image
import lcd
import time

'''
依次寻找所有红色色块、绿色色块和黄色色块,将所有色块进行框选并在中心标记十字
将同一种颜色色块的像素数量进行求和,将像素数量最多的颜色的名称存入 result 数组
result 数组中连续 30 次为同一个颜色则打印该颜色作为最终识别结果 
'''

lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 拍摄彩色图片
sensor.set_framesize(sensor.QVGA) # 设置图片大小为 320x240
  
sensor.set_auto_exposure(1) # 开启自动曝光
sensor.set_auto_gain(1) # 开启自动增益
sensor.set_auto_whitebal(1)  # 开启白平衡

sensor.set_saturation(2)  # 拉满饱和度

sensor.run(1)

# 设置三个颜色的阈值
green_threshold  = (31, 64, -18, 5, -20, 5)
yellow_threshold = (38, 62, -24, 1, 6, 35)
red_threshold = (32, 66, 9, 54, -9, 37)

results = []
isResultAccurate = False

while True:
    img=sensor.snapshot() # 捕获一张照片

	# 寻找三种颜色的所有色块
    red_blobs = img.find_blobs([red_threshold])
    yellow_blobs = img.find_blobs([yellow_threshold])
    green_blobs = img.find_blobs([green_threshold])

	# 用于计算单个颜色的像素和
    red_blobs_pixels_sum = 0
    green_blobs_pixels_sum = 0
    yellow_blobs_pixels_sum = 0

    if red_blobs:
        for red_blob in red_blobs:
            img.draw_rectangle(red_blob[0:4]) # 画出色块矩形
            img.draw_cross(red_blob[5], red_blob[6]) # 在中心坐标上画十字
            red_blobs_pixels_sum += red_blob.pixels() # 计算红色色块像素和
  
    if green_blobs:
        for green_blob in green_blobs:
            img.draw_rectangle(green_blob[0:4])
            img.draw_cross(green_blob[5], green_blob[6])
            green_blobs_pixels_sum += green_blob.pixels()
  
    if yellow_blobs:
        for yellow_blob in yellow_blobs:
            img.draw_rectangle(yellow_blob[0:4])
            img.draw_cross(yellow_blob[5], yellow_blob[6])
            yellow_blobs_pixels_sum += yellow_blob.pixels()

	# 创建字典并按照像素和从多到少进行排序
    color_blobs = {"red":red_blobs_pixels_sum, "green":green_blobs_pixels_sum, "yellow":yellow_blobs_pixels_sum}
    color_blobs_ordered = sorted(color_blobs.items(),key=lambda x:x[1],reverse=True)
    
	# 存入像素和最多的颜色名称
    result = color_blobs_ordered[0][0]
    results.append(result)

	# 连续 30 张图片识别结果都为该颜色则打印该颜色 
    if len(results) >= 30:
        if len(set(results)) == 1:
            isResultAccurate = True
        results.pop(0)

    if isResultAccurate:
        print(result)

    lcd.display(img) #  LCD 显示图片

最后效果:

3060531-20230810215513408-1397132582.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK