【发布时间】:2018-12-19 12:02:12
【问题描述】:
我有一个由三元组(r,g,b) 的numpy.array 矩阵nxm 表示的图像,我想使用我自己的函数将其转换为灰度。
我尝试将矩阵 nxmx3 转换为单个值矩阵 nxm 失败,这意味着从数组开始 [r,g,b] 我得到 [gray, gray, gray] 但我需要 @ 987654327@.
即初始颜色通道:[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
【问题讨论】:
-
请使用矢量化而不是嵌套的 for 循环。
-
您能详细说明一下吗? @Quickbeam2k1
-
我不确定我是否理解您的问题。 opencv转换到底有什么问题?你想自己实现opencv转换吗?
-
opencv转换没有问题。是的,我想要一个二维图像数组来实现我自己的代码。
-
thesamiroli 如果我的回答不正确,您能否详细说明我错过了什么?谢谢!
标签: python arrays numpy opencv