python利用opencv实现颜色检测

作者:pengyuan101 时间:2022-05-08 14:20:58 

本文实例为大家分享了python利用opencv实现颜色检测的具体代码,供大家参考,具体内容如下

需要实现倒车辅助标记检测的功能,倒车辅助标记颜色已经确定了,所以不需要使用深度学习的方法,那样成本太高了,直接可以使用颜色检测的方法。

1.首先需要确定待检测目标的HSV值


import cv2

img = cv2.imread('l3.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

def mouse_click(event, x, y, flags, para):
if event == cv2.EVENT_LBUTTONDOWN: # 左边鼠标点击
 print('PIX:', x, y)
 print("BGR:", img[y, x])
 print("GRAY:", gray[y, x])
 print("HSV:", hsv[y, x])

if __name__ == '__main__':
cv2.namedWindow("img")
cv2.setMouseCallback("img", mouse_click)
while True:
 cv2.imshow('img', img)
 if cv2.waitKey() == ord('q'):
  break
cv2.destroyAllWindows()

2.然后利用颜色检测,检测出指定目标


import numpy as np
import cv2

font = cv2.FONT_HERSHEY_SIMPLEX
lower_red = np.array([0, 127, 128]) # 红色阈值下界
higher_red = np.array([10, 255, 255]) # 红色阈值上界
lower_yellow = np.array([15, 230, 230]) # 黄色阈值下界
higher_yellow = np.array([35, 255, 255]) # 黄色阈值上界
lower_blue = np.array([85,240,140])
higher_blue = np.array([100,255,165])
frame=cv2.imread("l3.png")
img_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask_red = cv2.inRange(img_hsv, lower_red, higher_red) # 可以认为是过滤出红色部分,获得红色的掩膜
mask_yellow = cv2.inRange(img_hsv, lower_yellow, higher_yellow) # 获得绿色部分掩膜
mask_yellow = cv2.medianBlur(mask_yellow, 7) # 中值滤波
mask_red = cv2.medianBlur(mask_red, 7) # 中值滤波
mask_blue = cv2.inRange(img_hsv, lower_blue, higher_blue) # 获得绿色部分掩膜
mask_blue = cv2.medianBlur(mask_blue, 7) # 中值滤波
#mask = cv2.bitwise_or(mask_green, mask_red) # 三部分掩膜进行按位或运算
print(mask_red)
cnts1, hierarchy1 = cv2.findContours(mask_red, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # 轮廓检测 #红色
cnts2, hierarchy2 = cv2.findContours(mask_blue, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # 轮廓检测 #红色
cnts3, hierarchy3 = cv2.findContours(mask_yellow, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for cnt in cnts1:
(x, y, w, h) = cv2.boundingRect(cnt) # 该函数返回矩阵四个点
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) # 将检测到的颜色框起来
cv2.putText(frame, 'red', (x, y - 5), font, 0.7, (0, 0, 255), 2)
for cnt in cnts2:
(x, y, w, h) = cv2.boundingRect(cnt) # 该函数返回矩阵四个点
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) # 将检测到的颜色框起来
cv2.putText(frame, 'blue', (x, y - 5), font, 0.7, (0, 0, 255), 2)

for cnt in cnts3:
(x, y, w, h) = cv2.boundingRect(cnt) # 该函数返回矩阵四个点
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # 将检测到的颜色框起来
cv2.putText(frame, 'yellow', (x, y - 5), font, 0.7, (0, 255, 0), 2)
cv2.imshow('frame', frame)

cv2.waitKey(0)
cv2.destroyAllWindows()

3.效果

python利用opencv实现颜色检测

来源:https://blog.csdn.net/sinat_38387807/article/details/107512125

标签:python,颜色检测
0
投稿

猜你喜欢

  • python实现Virginia无密钥解密

    2023-07-25 10:56:49
  • python删除字符串中指定字符的方法

    2022-12-02 18:32:44
  • python密码学RSA密码加密教程

    2023-08-03 17:09:10
  • webpack学习教程之publicPath路径问题详解

    2023-07-16 06:09:52
  • Pytho常见的数据可视化库,小白必备

    2022-08-04 15:21:13
  • Python list与NumPy array 区分详解

    2021-08-25 04:12:37
  • Python如何合并多个字典或映射

    2023-11-07 17:20:51
  • Python调用实现最小二乘法的方法详解

    2022-07-04 16:31:00
  • python遍历文件目录、批量处理同类文件

    2021-10-19 14:58:12
  • Pytorch中的gather使用方法

    2021-11-22 06:11:49
  • asp上传文件自动重命名方法

    2007-08-24 09:46:00
  • asp如何取回已忘记的密码?

    2010-05-13 16:33:00
  • python标准库学习之sys模块详解

    2021-08-02 02:25:19
  • 深入了解Python 中线程和进程区别

    2021-09-01 20:51:38
  • Python入门Anaconda和Pycharm的安装和配置详解

    2022-12-21 20:25:07
  • python实现简单五子棋游戏

    2021-04-04 16:15:57
  • JavaScript中的64位加密及解密

    2009-12-23 19:10:00
  • 好习惯和坏习惯

    2009-01-20 12:51:00
  • IE在DOM操作有表单控件时的bug

    2008-08-21 13:00:00
  • Firebox 3 后退后按钮 diasabled 状态不恢复的一个解决方案

    2008-11-06 12:28:00
  • asp之家 网络编程 m.aspxhome.com