【发布时间】:2019-06-27 09:27:35
【问题描述】:
我编写了一个代码,当您用鼠标点击屏幕时,它会读取图像并获取带有点击像素坐标的 rgb 值。工作代码如下;
import cv2
import numpy as np
def mouseRGB(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN: #checks mouse left button down condition
colorsB = image[y,x,0]
colorsG = image[y,x,1]
colorsR = image[y,x,2]
colors = image[y,x]
print("Red: ",colorsR)
print("Green: ",colorsG)
print("Blue: ",colorsB)
print("BRG Format: ",colors)
print("Coordinates of pixel: X: ",x,"Y: ",y)
# Read an image, a window and bind the function to window
image = cv2.imread("image.jpg")
cv2.namedWindow('mouseRGB')
cv2.setMouseCallback('mouseRGB',mouseRGB)
#Do until esc pressed
while(1):
cv2.imshow('mouseRGB',image)
if cv2.waitKey(20) & 0xFF == 27:
break
#if esc pressed, finish.
cv2.destroyAllWindows()
但我想要的是;我不想读取图像,我想在屏幕上看到实时摄像头流;当我点击某个地方时,我想随时查看点击像素的 rgb 值和坐标。
如何编辑我的代码?
【问题讨论】:
-
cv2 具有从相机读取的功能 - 您应该在 cv2 的任何基础教程中看到它。 opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/…
-
cv2 还具有在图像opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/… 上绘制图形和文本的功能
标签: python python-3.x opencv rgb