Convert an RGB image to grayscale using the luminosity method, which weights the color channels according to human perception.
def rgb_to_grayscale(image: list[list[list[int]]]) -> list[list[int]]:
"""
image: [H, W, 3] with R, G, B channels (0-255)
Returns: [H, W] grayscale image
"""
H = len(image)
W = len(image[0])
gray = []
for h in range(H):
row = []
for w in range(W):
r, g, b = image[h][w][0], image[h][w][1], image[h][w][2]
# Luminosity method (ITU-R BT.601)
val = 0.299 * r + 0.587 * g + 0.114 * b
row.append(round(val))
gray.append(row)
return gray