Python 官方文档:入门教程 => 点击学习
这里以python版本OpenCV演示如何查找颜色 import numpy as npimport cv2font = cv2.FONT_HERSHEY_SIMPLEXlower_red = np.array([0, 127, 128]
import numpy as npimport cv2font = cv2.FONT_HERSHEY_SIMPLEXlower_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()
效果
用鼠标确定确定待检测目标的HSV值
import cv2img = 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()
文章来源:https://www.jb51.net/article/206173.htm
来源地址:https://blog.csdn.net/FL1623863129/article/details/128189352
--结束END--
本文标题: [opencv]HSV常见颜色上下限值
本文链接: https://lsjlt.com/news/556133.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0