Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python&;Opencv:单击鼠标时实时获取RGB值_Python_Python 3.x_Opencv_Rgb - Fatal编程技术网

Python&;Opencv:单击鼠标时实时获取RGB值

Python&;Opencv:单击鼠标时实时获取RGB值,python,python-3.x,opencv,rgb,Python,Python 3.x,Opencv,Rgb,我写了一个代码,当你们用鼠标点击屏幕的时候,读取一个图像并获得带有点击像素坐标的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 =

我写了一个代码,当你们用鼠标点击屏幕的时候,读取一个图像并获得带有点击像素坐标的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值和坐标


如何编辑代码?

要捕获视频,请添加捕获对象

capture = cv2.VideoCapture(0)
0
是我的网络摄像头的摄像头编号,但如果您有第二个usb摄像头,则可能是
1

然后在
while
循环中,通过添加

ret, frame = capture.read()
您可以使用与处理任何图像完全相同的方式来处理帧

最后,完成后不要忘记释放捕获对象

capture.release()
cv2.destroyAllWindows()
完整的代码列表

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 = frame[y,x,0]
        colorsG = frame[y,x,1]
        colorsR = frame[y,x,2]
        colors = frame[y,x]
        print("Red: ",colorsR)
        print("Green: ",colorsG)
        print("Blue: ",colorsB)
        print("BRG Format: ",colors)
        print("Coordinates of pixel: X: ",x,"Y: ",y)


cv2.namedWindow('mouseRGB')
cv2.setMouseCallback('mouseRGB',mouseRGB)

capture = cv2.VideoCapture(0)

while(True):

    ret, frame = capture.read()

    cv2.imshow('mouseRGB', frame)

    if cv2.waitKey(1) == 27:
        break

capture.release()
cv2.destroyAllWindows()

cv2具有从相机读取的功能-您应该在cv2的任何基础教程中看到它。cv2还具有在图像上绘制图形和文本的功能。我知道这些线条,但我没有按照我的要求进行编辑