不废话 直接贴程序:
import numpy as np
import os,sys,time,cv2
from PIL import Image, ImageDraw, ImageFont
from datetime import datetime
date_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
if (isinstance(img, np.ndarray)): # 判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(img)
# 字体的格式
fontStyle = ImageFont.truetype(
"font/simsun.ttc", textSize, encoding="utf-8")
# 绘制文本
draw.text((left, top), text, textColor, font=fontStyle)
# 转换回OpenCV格式
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
font = cv2.FONT_HERSHEY_SIMPLEX
#0,0,0 180,255,46 #黑
#0,0,46 180,43,220#灰
#0,0,221 180,30,255#白
#0,43,46 10,255,255#红:0-156,10-180:
#11,43,46 25,255,255#橙
lower_Black = np.array([0,0,0]) # 范围低阈值
upper_Black = np.array([180,255,46]) # 范围高阈值
lower_gray = np.array([0,0,46]) # 范围低阈值
upper_gray = np.array([180,43,220]) # 范围高阈值
lower_white = np.array([0,0,60]) # 范围低阈值
upper_white = np.array([160,230,230]) # 范围高阈值
lower_red = np.array([0,210,210]) # ===红色范围低阈值
upper_red = np.array([10,255,255]) # ==红色范围高阈值
lower_orange = np.array([11,150,150]) # 范围低阈值
upper_orange = np.array([25,230,230]) # 范围高阈值
#26,43,46 34,255,255#黄
#35,43,46 77,255,255#绿
#78,43,46 99,255,255#青
#100,43,46 124,255,255#蓝
#125,43,46 155,255,255#紫
lower_yellow = np.array([20,40,40]) # 范围低阈值
upper_yellow = np.array([40,255,255]) # 范围高阈值
lower_green = np.array([35, 110, 106]) # ====绿色范围低阈值
upper_green = np.array([77, 255, 255]) # ====绿色范围高阈值
lower_Cyan = np.array([78,43,46]) # 范围低阈值
upper_Cyan = np.array([180,255,46]) # 范围高阈值
lower_gray = np.array([0, 43, 46]) # 范围低阈值
upper_gray = np.array([99,255,255]) # 范围高阈值
lower_blue = np.array([100,43,46]) # 范围低阈值
upper_blue = np.array([124,255,255]) # 范围高阈值
lower_purple = np.array([125,43,46]) # 范围低阈值
upper_purple = np.array([155,255,255]) # 范围高阈值
'''
#黑 灰 白 红 橙 黄 绿 青 蓝 紫
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
#HSV>>hmin hmax smin smax vmin vmax
#0,0,0 180,255,46 #黑
#0,0,46 180,43,220#灰
#0,0,221 180,30,255#白
#0,43,46 10,255,255#红:0-156,10-180:
#11,43,46 25,255,255#橙
#26,43,46 34,255,255#黄
#35,43,46 77,255,255#绿
#78,43,46 99,255,255#青
#100,43,46 124,255,255#蓝
#125,43,46 155,255,255#紫
'''
#RGB,红绿蓝..
#需要更多颜色,可以去百度一下HSV阈值!
camre="QQ_fe805b75534ce4acd99aa42718f767921573675239.mp4"
camre=0
cap = cv2.VideoCapture(camre)#打开USB摄像头
cap.set(3, 1024)
cap.set(4, 768)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
for i in range(100000):
before = time.time()
ret, frame = cap.read() # 读取一帧
hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask_green = cv2.inRange(hsv_img, lower_green, upper_green) # 根据颜色范围删选
mask_Black = cv2.inRange(hsv_img, lower_Black, upper_Black)
mask_gray = cv2.inRange(hsv_img, lower_gray, upper_gray)
mask_white = cv2.inRange(hsv_img, lower_white, upper_white)
mask_red = cv2.inRange(hsv_img, lower_red, upper_red)#hong
mask_orange = cv2.inRange(hsv_img, lower_orange, upper_orange)
mask_yellow = cv2.inRange(hsv_img, lower_yellow, upper_yellow)
mask_green = cv2.inRange(hsv_img, lower_green, upper_green) # 根据颜色范围删选
mask_Cyan = cv2.inRange(hsv_img, lower_Cyan, upper_Cyan)
mask_blue = cv2.inRange(hsv_img, lower_blue, upper_blue)
mask_purple = cv2.inRange(hsv_img, lower_purple, upper_purple)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
lblblb=9
# 根据颜色范围删选
mask_Black = cv2.medianBlur(mask_Black, lblblb) # 中值滤波
mask_gray = cv2.medianBlur(mask_gray, lblblb) # 中值滤波
mask_white = cv2.medianBlur(mask_white, lblblb) # 中值滤波
mask_red = cv2.medianBlur(mask_red, lblblb) # 中值滤波
mask_orange = cv2.medianBlur(mask_orange, lblblb) # 中值滤波
mask_yellow = cv2.medianBlur(mask_yellow, lblblb) # 中值滤波
mask_green = cv2.medianBlur(mask_green, lblblb) # 中值滤波
mask_Cyan = cv2.medianBlur(mask_Cyan, lblblb) # 中值滤波
mask_blue = cv2.medianBlur(mask_blue, lblblb) # 中值滤波
mask_purple = cv2.medianBlur(mask_purple, lblblb) # 中值滤波
mask = cv2.bitwise_or(mask_orange, mask_red)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
contours, hierarchy = cv2.findContours(mask_Black, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours2, hierarchy2 = cv2.findContours(mask_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours3, hierarchy3 = cv2.findContours(mask_white, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours4, hierarchy4 = cv2.findContours(mask_red, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours5, hierarchy5 = cv2.findContours(mask_orange, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours6, hierarchy6 = cv2.findContours(mask_yellow, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours7, hierarchy7 = cv2.findContours(mask_green, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours8, hierarchy8 = cv2.findContours(mask_Cyan, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours9, hierarchy9 = cv2.findContours(mask_blue, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours10,hierarchy10 = cv2.findContours(mask_purple, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 0), 2)
#cv2.putText(frame, "Black", (x, y - 5), font, 0.7, (0, 0, 0), 2)
frame = cv2ImgAddText(frame, "黑", x,y , (0, 255 , 0), 25)#
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
for cnt2 in contours2:
(x2, y2, w2, h2) = cv2.boundingRect(cnt2)
cv2.rectangle(frame, (x2, y2), (x2 + w2, y2 + h2), (128,128,128), 2)
cv2.putText(frame,"gray", (x2, y2 - 5), font, 0.7, (128,128,128), 2)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
for cnt3 in contours3:
(x2, y2, w2, h2) = cv2.boundingRect(cnt3)
cv2.rectangle(frame, (x2, y2), (x2 + w2, y2 + h2), (255, 255, 255), 2)
cv2.putText(frame,"white", (x2, y2 - 5), font, 0.7, (255, 255, 255), 2)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
for cnt4 in contours4:
(x2, y2, w2, h2) = cv2.boundingRect(cnt4)
cv2.rectangle(frame, (x2, y2), (x2 + w2, y2 + h2), (0,0, 255), 2)
cv2.putText(frame,"Red", (x2, y2 - 5), font, 0.7, (0, 0, 255), 2)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
for cnt5 in contours5:
(x2, y2, w2, h2) = cv2.boundingRect(cnt5)
cv2.rectangle(frame, (x2, y2), (x2 + w2, y2 + h2), (5,140,255), 2)
cv2.putText(frame,"orange", (x2, y2 - 5), font, 0.7, (5,140,255), 2)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
for cnt6 in contours6:
(x2, y2, w2, h2) = cv2.boundingRect(cnt6)
cv2.rectangle(frame, (x2, y2), (x2 + w2, y2 + h2), (0,255,255), 2)
cv2.putText(frame,"yellow", (x2, y2 - 5), font, 0.7, (0,255,255), 2)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
for cnt7 in contours7:
(x2, y2, w2, h2) = cv2.boundingRect(cnt7)
cv2.rectangle(frame, (x2, y2), (x2 + w2, y2 + h2), (0, 255, 0), 2)
cv2.putText(frame,"green", (x2, y2 - 5), font, 0.7, (0,255, 0), 2)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
for cnt8 in contours8:
(x2, y2, w2, h2) = cv2.boundingRect(cnt8)
cv2.rectangle(frame, (x2, y2), (x2 + w2, y2 + h2), (255, 255,0), 2)
cv2.putText(frame,"Cyan", (x2, y2 - 5), font, 0.7, (255, 255,0), 2)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
for cnt9 in contours9:
(x2, y2, w2, h2) = cv2.boundingRect(cnt9)
cv2.rectangle(frame, (x2, y2), (x2 + w2, y2 + h2), (255, 0, 0), 2)
cv2.putText(frame,"blue", (x2, y2 - 5), font, 0.7, (255, 0, 0), 2)
#Black, gray, white, red, orange, yellow, green, Cyan, blue, purple
for cnt10 in contours10:
(x2, y2, w2, h2) = cv2.boundingRect(cnt10)
cv2.rectangle(frame, (x2, y2), (x2 + w2, y2 + h2), (255,0,255), 2)
cv2.putText(frame,"purple", (x2, y2 - 5), font, 0.7, (255,0,255), 2)
##(255,255,255)蓝绿红BGR.
frame = cv2ImgAddText(frame, "OpenCV+Python:颜色识别", 5, 5, (255, 0 , 0), 25)#
cv2.putText(frame,datetime.now().strftime('%Y-%m-%d %H:%M:%S'), (2,50), font, 0.7, (0,0,255), 2)
after = time.time()
cv2.putText(frame,str(after-before)+"/F:"+str(1//(after-before))+"frame", (2,75), font, 0.7, (0,0,255), 2)
cv2.imshow('frame_',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print("done")
cap.release()
cv2.destroyAllWindows()