import cv2 import numpy as np from imutils import contours import serial from luma.core.interface.serial import i2c, spi from luma.core.render import canvas from luma.oled.device import ssd1306, ssd1325, ssd1331, sh1106 from time import sleep from PIL import ImageFont

颜色阈值

lower_red = np.array([0, 178, 71]) # 红色阈值下界 higher_red = np.array([56, 255, 207]) # 红色阈值上界 lower_green = np.array([86,122,37]) # 绿色阈值下界 higher_green = np.array([96,255,191]) # 绿色阈值上界 lower_white = np.array([0,0,221]) # 白色阈值下界 higher_white = np.array([180,0,255])# 白色阈值上界

ss = i2c(port=1, address=0x3C) device = ssd1306(ss)

ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=2)

def red(draw_frame): kernel = np.ones((5, 5), np.uint8) frame_hsv = cv2.cvtColor(draw_frame, cv2.COLOR_BGR2HSV)

img = cv2.inRange(frame_hsv, lower_red, higher_red)
dilation = cv2.dilate(img, kernel, iterations=1)
closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
closing = cv2.GaussianBlur(closing, (5, 5), 0)
edges = cv2.Canny(closing, 10, 20)

cnts, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

if len(cnts) > 0:
    s = []
    max_index = 0
    (cnts, boundingRects) = contours.sort_contours(cnts)
    for cnt in cnts:
        s.append(cv2.contourArea(cnt))
    max_index = s.index(max(s))
    (x, y, w, h) = boundingRects[max_index]
    frame_out = cv2.rectangle(draw_frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
    center_x, center_y = (int(x + w / 2), int(y + h / 2))
    box_size = 10
    center_box = draw_frame[center_y - box_size:center_y + box_size, center_x - box_size:center_x + box_size]

    cv2.rectangle(img, (center_x - box_size, center_y - box_size), (center_x + box_size, center_y + box_size),
                  (0, 255, 0), 2)
    red_mask = cv2.inRange(center_box, lower_red, higher_red)
    red_pixels = cv2.countNonZero(red_mask)

    data = 'red'
    ser.write(data.encode())
    with canvas(device) as draw:
        draw.rectangle(device.bounding_box, outline='white', fill='black')
        draw.text((30, 20), 'red', fill='white')
    return True
else:
    return False

def green(draw_frame): kernel = np.ones((5, 5), np.uint8) frame_hsv = cv2.cvtColor(draw_frame, cv2.COLOR_BGR2HSV)

img = cv2.inRange(frame_hsv, lower_green, higher_green)
dilation = cv2.dilate(img, kernel, iterations=1)
closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
closing = cv2.GaussianBlur(closing, (5, 5), 0)
edges = cv2.Canny(closing, 10, 20)

cnts, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

if len(cnts) > 0:
    s = []
    max_index = 0
    (cnts, boundingRects) = contours.sort_contours(cnts)
    for cnt in cnts:
        s.append(cv2.contourArea(cnt))
    max_index = s.index(max(s))
    (x, y, w, h) = boundingRects[max_index]
    frame_out = cv2.rectangle(draw_frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
    center_x, center_y = (int(x + w / 2), int(y + h / 2))
    box_size = 10
    center_box = draw_frame[center_y - box_size:center_y + box_size, center_x - box_size:center_x + box_size]

    cv2.rectangle(img, (center_x - box_size, center_y - box_size), (center_x + box_size, center_y + box_size),
                  (0, 255, 0), 2)
    green_mask = cv2.inRange(center_box, lower_green, higher_green)
    green_pixels = cv2.countNonZero(green_mask)

    data = 'green'
    ser.write(data.encode())
    with canvas(device) as draw:
        draw.rectangle(device.bounding_box, outline='white', fill='black')
        draw.text((30, 20), 'green', fill='white')
    return True
else:
    return False

def white(draw_frame): kernel = np.ones((5, 5), np.uint8) frame_hsv = cv2.cvtColor(draw_frame, cv2.COLOR_BGR2HSV)

img = cv2.inRange(frame_hsv, lower_white, higher_white)
dilation = cv2.dilate(img, kernel, iterations=1)
closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
closing = cv2.GaussianBlur(closing, (5, 5), 0)
edges = cv2.Canny(closing, 10, 20)

cnts, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if len(cnts) > 0:
    s = []
    max_index = 0
    (cnts, boundingRects) = contours.sort_contours(cnts)
    for cnt in cnts:
        s.append(cv2.contourArea(cnt))
    max_index = s.index(max(s))
    (x, y, w, h) = boundingRects[max_index]
    frame_out = cv2.rectangle(draw_frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
    center_x, center_y = (int(x + w / 2), int(y + h / 2))
    box_size = 10
    center_box = draw_frame[center_y - box_size:center_y + box_size, center_x - box_size:center_x + box_size]

    cv2.rectangle(img, (center_x - box_size, center_y - box_size), (center_x + box_size, center_y + box_size),
                  (0, 255, 0), 2)
    white_mask = cv2.inRange(center_box, lower_white, higher_white)
    white_pixels = cv2.countNonZero(white_mask)

    data = 'white'
    ser.write(data.encode())
    with canvas(device) as draw:
        draw.rectangle(device.bounding_box, outline='white', fill='black')
        draw.text((30, 20), 'white', fill='white')
    return True
else:
    return False

打开摄像头

vc = cv2.VideoCapture(0) received_signal = False while True: flag, frame = vc.read() draw_frame = frame.copy() data = ser.readline().decode("utf-8").strip() if data == 'g': print('received_signal') received_signal = True color_name = '' if red_pixels > white_pixels and red_pixels > green_pixels: color_name = 'red' elif white_pixels > red_pixels and white_pixels > green_pixels: color_name = 'white' elif green_pixels > red_pixels and green_pixels > white_pixels: color_name = 'green' if received_signal: if color_name: ser.write(color_name.encode("utf-8")) received_signal = False # 处理完信号后将其重置为 False else: print('Color not recognized!') cv2.imshow('frame',frame) cv2.waitKey(0)

vc.release() cv2.destroyAllWindows() ser.close()

Python OpenCV 颜色识别与串口通信:红色、绿色和白色

原文地址: https://www.cveoy.top/t/topic/nXR3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录