I have a homework assignment I'm desperate to figure out. We've only spent two lecture days on opencv so I'm going in blind with this.
The assignment is to convert an RGB img to grayscale using the luminance formula 0.02126*R+0.7152*G+0.0722*B
So the type of each pixel should be the same as the original image. We're not allowed to use matplot or PIL which is what I've seen a lot of trying to figure this thing out.
The code I have now outputs just a gray image. So instead of graySCALE, it's only gray. I'm so lost, please help.
import cv2
import numpy as np
def togray():
img = cv2.imread("fruits.jpg")
cv2.imshow('Original',img)
height, width, channels = img.shape
img2 = np.ndarray (shape=(height,width,))
for i in range(height):
for j in range(width):
img2[i,j]=(0*0.2126 + 0.7152*1 + 0.0722*2)
cv2.imshow('Grayscale',img2)
cv2.waitKey(0)
togray()
i,j
coordinates to the exact same value:(0*0.2126 + 0.7152*1 + 0.0722*2)
. I haven't used this package, but I imagine you need r, g, and b values in place of 0, 1, and 2. – cpander