led指示灯识别可以实现led的八种灯光颜色识别以及灯光闪烁识别。可用于大型设备的led灯光自动化监控。暂未实现led定位识别。
本代码使用python编写,依赖opencv来进行颜色识别。 依赖open cv 3.4.1,太高可能会报错
pip3 install opencv_python==3.4.2.16
# -*- coding: utf-8 -*-
"""
图片测试
"""
import cv2
import numpy as np
import collections
import OperationMysql as mySql
# 处理图片
def get_color(frame):
print('go in get_color')
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
maxsum = -100
color = None
color_dict = getColorList()
for d in color_dict:
mask = cv2.inRange(hsv, color_dict[d][0], color_dict[d][1])
cv2.imwrite(d + '.jpg', mask)
binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
binary = cv2.dilate(binary, None, iterations=2)
img, cnts, hiera = cv2.findContours(binary.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
sum = 0
for c in cnts:
sum += cv2.contourArea(c)
if sum > maxsum:
maxsum = sum
color = d
return color
def getColorList():
dict = collections.defaultdict(list)
# 黑色
lower_black = np.array([0, 0, 0])
upper_black = np.array([180, 255, 46])
color_list = []
color_list.append(lower_black)
color_list.append(upper_black)
dict['black'] = color_list
# 灰色
lower_gray = np.array([0, 0, 46])
upper_gray = np.array([180, 43, 220])
color_list = []
color_list.append(lower_gray)
color_list.append(upper_gray)
dict['gray'] = color_list
# 白色
lower_white = np.array([0, 0, 221])
upper_white = np.array([180, 30, 255])
color_list = []
color_list.append(lower_white)
color_list.append(upper_white)
dict['white'] = color_list
# 红色
lower_red = np.array([156, 43, 46])
upper_red = np.array([180, 255, 255])
color_list = []
color_list.append(lower_red)
color_list.append(upper_red)
dict['red'] = color_list
# 红色2
lower_red = np.array([0, 43, 46])
upper_red = np.array([10, 255, 255])
color_list = []
color_list.append(lower_red)
color_list.append(upper_red)
dict['red2'] = color_list
# 橙色
lower_orange = np.array([11, 43, 46])
upper_orange = np.array([25, 255, 255])
color_list = []
color_list.append(lower_orange)
color_list.append(upper_orange)
dict['orange'] = color_list
# 黄色
lower_yellow = np.array([26, 43, 46])
upper_yellow = np.array([34, 255, 255])
color_list = []
color_list.append(lower_yellow)
color_list.append(upper_yellow)
dict['yellow'] = color_list
# 绿色
lower_green = np.array([35, 43, 46])
upper_green = np.array([77, 255, 255])
color_list = []
color_list.append(lower_green)
color_list.append(upper_green)
dict['green'] = color_list
# 青色
lower_cyan = np.array([78, 43, 46])
upper_cyan = np.array([99, 255, 255])
color_list = []
color_list.append(lower_cyan)
color_list.append(upper_cyan)
dict['cyan'] = color_list
# 蓝色
lower_blue = np.array([100, 43, 46])
upper_blue = np.array([124, 255, 255])
color_list = []
color_list.append(lower_blue)
color_list.append(upper_blue)
dict['blue'] = color_list
# 紫色
lower_purple = np.array([125, 43, 46])
upper_purple = np.array([155, 255, 255])
color_list = []
color_list.append(lower_purple)
color_list.append(upper_purple)
dict['purple'] = color_list
return dict
# 待识别图片
img = cv2.imread('images/test.jpg')
# 区域,可改为for循环,同时识别多个区域
x2, y2, w2, h2 = (426, 909, 37, 37)
# print(x2, y2, w2, h2)
img_roi2 = img[int(y2):int(y2 + h2), int(x2):int(x2 + w2)]
cv2.rectangle(img=img, pt1=(x2, y2), pt2=(x2 + w2, y2 + h2), color=(0, 0, 255), thickness=1)
color = get_color(img_roi2)
cv2.putText(img, '{0}'.format(color),
(x2 + 50, y2),
cv2.FONT_HERSHEY_SIMPLEX,
1.2, (0, 0, 255), 4,
cv2.LINE_AA)
cv2.imshow('roi', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
效果图:
后期有需要可扩展led自动区域识别与区域文字识别,现在暂时手动定位roi区域。
定位工具参看下一篇博客roi定位工具