opencv+python实现鼠标点击图像,输出该点的RGB和HSV值
我就废话不多说了,大家还是直接看代码吧!
import cv2 # 读取图片并缩放方便显示 img = cv2.imread('D:/6.jpg') height, width = img.shape[:2] size = (int(width * 0.2), int(height * 0.2)) # 缩放 img = cv2.resize(img, size, interpolation=cv2.INTER_AREA) # BGR转化为HSV HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 鼠标点击响应事件 def getposHsv(event, x, y, flags, param): if event==cv2.EVENT_LBUTTONDOWN: print("HSV is", HSV[y, x]) def getposBgr(event, x, y, flags, param): if event==cv2.EVENT_LBUTTONDOWN: print("Bgr is", img[y, x]) cv2.imshow("imageHSV", HSV) cv2.imshow('image', img) cv2.setMouseCallback("imageHSV", getposHsv) cv2.setMouseCallback("image", getposBgr) cv2.waitKey(0)
补充知识:python opencv查看图片中任一点的像素 BGR值 灰度值 HSV值
如下所示:
# -*- coding:utf-8 -*- import cv2 img = cv2.imread('11_13/120002.jpg') 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()
Windows下有画图软件可以直接查看图片的RGB值,但是没办法查看HSV值,这样一个小程序就可以帮你做到。
附一张opencv下HSV范围图片:
以上这篇opencv+python实现鼠标点击图像,输出该点的RGB和HSV值就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)