基于v831的颜色识别和色环识别

基于v831的颜色识别和色环识别

——————基于maxipy3开发

1.功能概述:

①基础颜色识别(红\绿\蓝)
②色环识别(识别区域内的圆形并加上颜色进行霍夫圆拟合)
③利用串口通讯将颜色和坐标数据以数据包形式发送

2.功能代码:

①基础颜色识别(红\绿\蓝):

# 定义一个函数,用于检测摄像头图像上的颜色区域
def color_area(img):
    detected_color_areas = []  # 创建一个空列表来存储检测到的颜色区域信息

    x_center = 0
    y_center = 0
    color_name = 0

    set_LAB = [
        [(0, 58, 34, 82, -45, 70)],  # red
        [(0, 69, -4, 28, -61, -14)],  # blue
        [(7, 66, -98, -13, -35, 121)]  # green
    ]
    
    for j in range(3):
        blobs = img.find_blobs(set_LAB[j])
        if blobs:
            for i in blobs:
                size = i["w"] * i["h"]
                if size > 3000:
                    x_start = i["x"]
                    x_end = i["x"] + i["w"]
                    x_center = int((x_start + x_end) / 2)
                    y_start = i["y"]
                    y_end = i["y"] + i["h"]
                    y_center = int((y_start + y_end) / 2)
                    
                    # 计算中心点的坐标
                    m = max((x_center - i["w"] * 0.3), 0)
                    n = max((y_center - i["h"] * 0.3), 0)
                    m = min((x_center - i["w"] * 0.3), 240)
                    n = min((y_center - i["h"] * 0.3), 240)
                    mk = [int(m), int(n), 20, 20]

                    if (mk[0] + 20) < 220 and (mk[1] + 20) < 220:
                        git_color = img.get_blob_color(mk, 0, 0)
                        #color = (int(git_color[0]), int(git_color[1]), int(git_color[2]))

                        if j == 0:
                            color_name = 1
                        elif j == 1:
                            color_name = 3
                        elif j == 2:
                            color_name = 2

                        # 将检测到的颜色区域信息添加到列表中
                        detected_color_areas.append((x_center, y_center, color_name))

    return detected_color_areas
②色环识别(识别区域内的圆形并加上颜色进行霍夫圆拟合):
# 定义一个函数,用于检测摄像头图像上的圆和颜色
def detect_circles_and_colors(img):
    
    detected_circles = []
    
    # 定义不同颜色的阈值范围
    red_thresh = [(10, 59, 9, 43, -21, 34)]
    green_thresh = [(5, 53, -37, 0, -51, 4)]
    blue_thresh = [(0, 56, -1, 24, -42, -14)]
    
    # 使用阈值检测红、绿和蓝色块
    red_blobs = img.find_blobs(red_thresh, merge=True)
    green_blobs = img.find_blobs(green_thresh, merge=True)
    blue_blobs = img.find_blobs(blue_thresh, merge=True)
​
    # 在图像中查找圆形
    circles = img.find_circles(
        threshold=3500,    # 用于检测圆形的阈值
        x_margin=2,        # x轴误差允许值
        y_margin=2,        # y轴误差允许值
        r_margin=2,        # 半径误差允许值
        r_min=35,          # 最小半径
        r_max=37,          # 最大半径
        r_step=1           # 半径步进值
    )
​
    if circles:
        for c in circles:
            center_x, center_y = c[0], c[1]
            closest_color = None
            closest_distance = float('inf')  # 初始化最小距离为正无穷大
​
            # 寻找最接近的颜色块
            for color_blobs in [red_blobs, green_blobs, blue_blobs]:
                if color_blobs:
                    for blob in color_blobs:
                        blob_center_x = blob['x'] + blob['w'] // 2
                        blob_center_y = blob['y'] + blob['h'] // 2
                        distance = (center_x - blob_center_x)**2 + (center_y - blob_center_y)**2
​
                        if distance < closest_distance:
                            closest_color = color_blobs
                            closest_distance = distance
​
            if closest_color:
                # 设置绘制颜色
                if closest_color == red_blobs:
                    color = 1  # 红色
                elif closest_color == green_blobs:
                    color = 2  # 绿色
                elif closest_color == blue_blobs:
                    color = 3  # 蓝色
​
                detected_circles.append((center_x, center_y, color))
​
    return detected_circles
​
​
③串口通信:

# 初始化串口通信,用于发送数据
ser = serial.Serial("/dev/ttyS1", 57600, timeout=0.03)
​
# 创建一个空列表,用于存储16进制字符
hex_chars = []
code = '00'
​
# 定义一个函数,用于发送数据包
def send_data_packet(data):
    for x, y, color in data:
        data_packet = bytes([0x4A, 0x4B, x // 100, (x // 10) % 10, x % 10, y // 100, (y // 10) % 10, y % 10, color, 0x4C])
        ser.write(data_packet)

3.完整代码:

# 导入所需的模块
from pickle import NONE
from maix import image, camera, display
import serial, struct, time, struct
​
# 初始化串口通信,用于发送数据
ser = serial.Serial("/dev/ttyS1", 57600, timeout=0.03)
​
# 创建一个空列表,用于存储16进制字符
hex_chars = []
code = '00'
​
# 定义一个函数,用于检测摄像头图像上的圆和颜色
def detect_circles_and_colors(img):
    
    detected_circles = []
    
    # 定义不同颜色的阈值范围
    red_thresh = [(10, 59, 9, 43, -21, 34)]
    green_thresh = [(5, 53, -37, 0, -51, 4)]
    blue_thresh = [(0, 56, -1, 24, -42, -14)]
    
    # 使用阈值检测红、绿和蓝色块
    red_blobs = img.find_blobs(red_thresh, merge=True)
    green_blobs = img.find_blobs(green_thresh, merge=True)
    blue_blobs = img.find_blobs(blue_thresh, merge=True)
​
    # 在图像中查找圆形
    circles = img.find_circles(
        threshold=3500,    # 用于检测圆形的阈值
        x_margin=2,        # x轴误差允许值
        y_margin=2,        # y轴误差允许值
        r_margin=2,        # 半径误差允许值
        r_min=35,          # 最小半径
        r_max=37,          # 最大半径
        r_step=1           # 半径步进值
    )
​
    if circles:
        for c in circles:
            center_x, center_y = c[0], c[1]
            closest_color = None
            closest_distance = float('inf')  # 初始化最小距离为正无穷大
​
            # 寻找最接近的颜色块
            for color_blobs in [red_blobs, green_blobs, blue_blobs]:
                if color_blobs:
                    for blob in color_blobs:
                        blob_center_x = blob['x'] + blob['w'] // 2
                        blob_center_y = blob['y'] + blob['h'] // 2
                        distance = (center_x - blob_center_x)**2 + (center_y - blob_center_y)**2
​
                        if distance < closest_distance:
                            closest_color = color_blobs
                            closest_distance = distance
​
            if closest_color:
                # 设置绘制颜色
                if closest_color == red_blobs:
                    color = 1  # 红色
                elif closest_color == green_blobs:
                    color = 2  # 绿色
                elif closest_color == blue_blobs:
                    color = 3  # 蓝色
​
                detected_circles.append((center_x, center_y, color))
​
    return detected_circles
​
# 定义一个函数,用于检测摄像头图像上的颜色区域
def color_area(img):
    detected_color_areas = []  # 创建一个空列表来存储检测到的颜色区域信息
​
    x_center = 0
    y_center = 0
    color_name = 0
​
    set_LAB = [
        [(0, 58, 34, 82, -45, 70)],  # red
        [(0, 69, -4, 28, -61, -14)],  # blue
        [(7, 66, -98, -13, -35, 121)]  # green
    ]
    
    for j in range(3):
        blobs = img.find_blobs(set_LAB[j])
        if blobs:
            for i in blobs:
                size = i["w"] * i["h"]
                if size > 3000:
                    x_start = i["x"]
                    x_end = i["x"] + i["w"]
                    x_center = int((x_start + x_end) / 2)
                    y_start = i["y"]
                    y_end = i["y"] + i["h"]
                    y_center = int((y_start + y_end) / 2)
                    
                    # 计算中心点的坐标
                    m = max((x_center - i["w"] * 0.3), 0)
                    n = max((y_center - i["h"] * 0.3), 0)
                    m = min((x_center - i["w"] * 0.3), 240)
                    n = min((y_center - i["h"] * 0.3), 240)
                    mk = [int(m), int(n), 20, 20]
​
                    if (mk[0] + 20) < 220 and (mk[1] + 20) < 220:
                        git_color = img.get_blob_color(mk, 0, 0)
                        #color = (int(git_color[0]), int(git_color[1]), int(git_color[2]))
​
                        if j == 0:
                            color_name = 1
                        elif j == 1:
                            color_name = 3
                        elif j == 2:
                            color_name = 2
​
                        # 将检测到的颜色区域信息添加到列表中
                        detected_color_areas.append((x_center, y_center, color_name))
​
    return detected_color_areas
​
# 定义一个函数,用于发送数据包
def send_data_packet(data):
    for x, y, color in data:
        data_packet = bytes([0x4A, 0x4B, x // 100, (x // 10) % 10, x % 10, y // 100, (y // 10) % 10, y % 10, color, 0x4C])
        ser.write(data_packet)
​
# 主循环
while True:
    img = camera.capture()  # 拍摄图像
​
    if ser.in_waiting > 0:
        data = ser.read(4)
        hex_chars = []
        for byte in data:
            hex_chars.append('%02x' % byte)
        # 如果接收到特定的字符序列('3a3b' + code + '3c'),则解析code值
​

以上代码仅仅作为参考,在实际使用中还需您结合实际情况进行修改,如有错误望指正!!!

  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值