我有一个由
numpy.array
矩阵表示的图像,该矩阵是nxm三元组(r,g,b)
,我想使用自己的函数将其转换为灰度图像。
我的尝试失败了,无法将nxmx3矩阵转换为单值nxm矩阵,这意味着从数组[r,g,b]
开始,我得到了[gray, gray, gray]
,但我需要gray
。
例如,初始颜色通道:[150 246 98]
。
转换为灰色后:[134 134 134]
。
我需要的是:134
我该如何做到这一点?
我的代码:
def grayConversion(image):
height, width, channel = image.shape
for i in range(0, height):
for j in range(0, width):
blueComponent = image[i][j][0]
greenComponent = image[i][j][1]
redComponent = image[i][j][2]
grayValue = 0.07 * blueComponent + 0.72 * greenComponent + 0.21 * redComponent
image[i][j] = grayValue
cv2.imshow("GrayScale",image)
return image