[英]Converting multiple numpy images to gray scale
我目前有一个包含 2000 张照片的 numpy 数组“图像”。 我正在寻找一种将“图像”中的所有照片转换为灰度的改进方法。 图像的形状是 (2000, 100, 100, 3)。 这是我到目前为止:
# Function takes index value and convert images to gray scale
def convert_gray(idx):
gray_img = np.uint8(np.mean(images[idx], axis=-1))
return gray_img
#create list
g = []
#loop though images
for i in range(0, 2000):
#call convert to gray function using index of image
gray_img = convert_gray(i)
#add grey image to list
g.append(gray_img)
#transform list of grey images back to array
gray_arr = np.array(g)
我想知道是否有人可以提出一种更有效的方法来做到这一点? 我需要数组格式的输出
用你现在做的最后一个轴的平均值:
Gray = 1/3 * Red + 1/3 * Green + 1/3 * Blue
但实际上另一个转换公式更常见(请参阅此答案):
Gray = 299/1000 * Red + 587/1000 * Green + 114/1000 * Blue
@unutbu 提供的代码也适用于图像数组:
import numpy as np
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
rgb = np.random.random((100, 512, 512, 3))
gray = rgb2gray(rgb)
# shape: (100, 512, 512)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.