-2

I would like to know how to convert this code to show gray intensity instead of 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()
mkrieger1
  • 10,793
  • 4
  • 39
  • 47
  • Hey madziooro, welcome to stock overflow. Have you tried coming up with a solution yourself? I'm sure somebody would like to help if you made some effort yourself first. – Jonas Feb 22 '21 at 16:59

1 Answers1

1

You can use cv2.cvtColor to convert from BGR to Grayscale. In your case you'd have to add

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

For more information see opencv tutorial.

Galunid
  • 592
  • 5
  • 14