opencv-python 颜色识别

不废话 直接贴程序:

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()

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的颜色识别示例,使用OpenCV-Python库: ``` import cv2 import numpy as np # 定义颜色范围 lower_red = np.array([0, 50, 50]) upper_red = np.array([10, 255, 255]) lower_blue = np.array([110, 50, 50]) upper_blue = np.array([130, 255, 255]) lower_green = np.array([50, 50, 50]) upper_green = np.array([70, 255, 255]) # 读取图像并转换为HSV格式 img = cv2.imread('color_image.jpg') hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 根据颜色范围创建掩膜 mask_red = cv2.inRange(hsv, lower_red, upper_red) mask_blue = cv2.inRange(hsv, lower_blue, upper_blue) mask_green = cv2.inRange(hsv, lower_green, upper_green) # 对原始图像进行掩膜操作,提取出指定颜色的区域 res_red = cv2.bitwise_and(img, img, mask=mask_red) res_blue = cv2.bitwise_and(img, img, mask=mask_blue) res_green = cv2.bitwise_and(img, img, mask=mask_green) # 显示结果 cv2.imshow('Original Image', img) cv2.imshow('Red Region', res_red) cv2.imshow('Blue Region', res_blue) cv2.imshow('Green Region', res_green) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在这个例子中,我们首先定义了三个颜色的范围:红色、蓝色和绿色。然后,我们读取了一个图像,并将其转换为HSV格式。接下来,我们使用`cv2.inRange()`函数创建了三个掩膜,分别用于提取红色、蓝色和绿色区域的像素。最后,我们使用`cv2.bitwise_and()`函数将原始图像与掩膜进行操作,提取出指定颜色的区域,并将其显示出来。 请注意,这个例子只是一个基础示例,您可以根据需要修改颜色范围,并添加更多的颜色
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值