文件名:colorList.py 这是颜色库。
# -*- coding:UTF-8 -*-
import numpy as np
import collections
def getColorList():
dict = collections.defaultdict(list)
# black
lower_black = np.array([0, 0, 0])
upper_black = np.array([180, 255, 46])
color_list_black = []
color_list_black.append(lower_black)
color_list_black.append(upper_black)
dict['black'] = color_list_black
# gray
lower_gray = np.array([0, 0, 46])
upper_gray = np.array([180, 43, 220])
color_list_gray= []
color_list_gray.append(lower_gray)
color_list_gray.append(upper_gray)
dict['gray'] = color_list_gray
# white
lower_white = np.array([0, 0, 221])
upper_white = np.array([180, 30, 255])
color_list_white = []
color_list_white.append(lower_white)
color_list_white.append(upper_white)
dict['white'] = color_list_white
# red
lower_red = np.array([156, 43, 46])
upper_red = np.array([180, 255, 255])
color_list_red = []
color_list_red.append(lower_red)
color_list_red.append(upper_red)
dict['red'] = color_list_red
# red2
lower_red = np.array([0, 43, 46])
upper_red = np.array([10, 255, 255])
color_list_red2 = []
color_list_red2.append(lower_red)
color_list_red2.append(upper_red)
dict['red2'] = color_list_red2
# orange
lower_orange = np.array([11, 43, 46])
upper_orange = np.array([25, 255, 255])
color_list_orange = []
color_list_orange.append(lower_orange)
color_list_orange.append(upper_orange)
dict['orange'] = color_list_orange
# yellow
lower_yellow = np.array([26, 43, 46])
upper_yellow = np.array([34, 255, 255])
color_list_yellow = []
color_list_yellow.append(lower_yellow)
color_list_yellow.append(upper_yellow)
dict['yellow'] = color_list_yellow
# green
lower_green = np.array([35, 43, 46])
upper_green = np.array([77, 255, 255])
color_list_green = []
color_list_green.append(lower_green)
color_list_green.append(upper_green)
dict['green'] = color_list_green
# cyan
lower_cyan = np.array([78, 43, 46])
upper_cyan = np.array([99, 255, 255])
color_list_cyan = []
color_list_cyan.append(lower_cyan)
color_list_cyan.append(upper_cyan)
dict['cyan'] = color_list_cyan
# blue
lower_blue = np.array([100, 43, 46])
upper_blue = np.array([124, 255, 255])
color_list_blue = []
color_list_blue.append(lower_blue)
color_list_blue.append(upper_blue)
dict['blue'] = color_list_blue
# purple
lower_purple = np.array([125, 43, 46])
upper_purple = np.array([155, 255, 255])
color_list_purple = []
color_list_purple.append(lower_purple)
color_list_purple.append(upper_purple)
dict['purple'] = color_list_purple
return dict
if __name__ == '__main__':
color_dict = getColorList()
print(color_dict)
num = len(color_dict)
print('num=', num)
for d in color_dict:
print('key=', d)
print('value=', color_dict[d][1])
然后是 xf_color.py 这就能识别颜色了 (这是识别已有图片文件版的) 原理就是用上面的色库 算出图片颜色面积 哪个最大 就算是啥颜色
# -*- coding:UTF-8 -*-
import cv2
import colorList
def get_color(frame):
print('go in get_color')
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
maxsum = 0
color = None
color_dict = colorList.getColorList()
for d in color_dict:
mask = cv2.inRange(hsv, color_dict[d][0], color_dict[d][1])
# cv2.imwrite(d + ".png", mask)
binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
binary = cv2.dilate(binary, None, iterations=2)
# cv2.imwrite(d +"1.png", binary)
cnts = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
sum = 0
for c in cnts:
sum += cv2.contourArea(c)
# print("%s , %d" %(d, sum ))
if sum > maxsum:
maxsum = sum
color = d
return color
if __name__ == '__main__':
filename = "./images/test_yellow.png"
frame = cv2.imread(filename)
print(get_color(frame))
文件名:xf_realize.py 这是用摄像头来识别颜色。加了个拍照功能 ,只返回红绿蓝三种颜色
# -*- coding:UTF-8 -*-
import cv2
import xf_color
def videox():
vix = cv2.VideoCapture(0)
while True:
ret, tu = vix.read()
cv2.imshow("take_photo", tu)
cv2.waitKey(1)
cv2.imwrite("color.png", tu)
filename = cv2.imread("color.png")
color = xf_color.get_color(filename)
if color == "red" or color == "red2":
print("red")
result = "1"
break
elif color == "green":
print("green")
result = "2"
break
elif color == "blue":
print("blue")
result = "3"
break
vix.release()
cv2.destroyAllWindows()
return result
if __name__ == '__main__':
videox()
接下来是识别二维码的(有二维码图片文件) 文件名: xf_scance.py
# -*- coding:UTF-8 -*-
import pyzbar.pyzbar as pyzbar
from PIL import Image, ImageEnhance
def scance():
image = "photo.png"
img = Image.open(image)
# img = ImageEnhance.Brightness(img).enhance(2.0) # 增加亮度
#
# img = ImageEnhance.Sharpness(img).enhance(17.0) # 锐利化
#
img = ImageEnhance.Contrast(img).enhance(4.0) # 增加对比度
#
img = img.convert('L') # 灰度化
barcodes = pyzbar.decode(img)
for barcode in barcodes:
barcodeData = barcode.data.decode("utf-8")
return barcodeData
if __name__ == '__main__':
scance()
摄像头扫二维码的
# -*- coding:UTF-8 -*-
import cv2
import xf_scance
def videox():
vix = cv2.VideoCapture(0)
while True:
ret, tu = vix.read()
cv2.imshow("take_photo", tu)
cv2.waitKey(1)
cv2.imwrite("photo.png", tu)
result = xf_scance.scance()
if result:
# print(result)
break
vix.release()
cv2.destroyAllWindows()
return result
if __name__ == '__main__':
r = videox()
print(r)
注:需要将以上的脚本文件放在同一个文件夹里
本篇文章转自
https://www.cnblogs.com/xf1262048067/p/10640050.html
如有冒犯,请原著联系,即刻删除。
(想着以后查看会方便些,所以把它放到了自己的博客上。)