如何转换此代码以显示灰色强度而不是 RGB

How to convert this code to show gray intensity instead of RGB

我想知道如何将此代码转换为显示灰色强度而不是 RGB。

import cv2
import numpy as np


def mouseRGB(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDOWN:
        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.cvtColor 从 BGR 转换为灰度。在你的情况下你必须添加

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('mouseRGB', gray)

有关详细信息,请参阅 opencv tutorial