小编kam*_*mil的帖子

Python 将 3 通道 rgb 彩色图像更改为 1 通道灰色的速度有多快?

我在包含原始像素数据的 4D 数组中拥有近 40000 张图像 - (示例数量、宽度、高度、通道)。每个图像的宽度为 32 像素,高度为 32 像素,以及 3 个 RGB 颜色通道。我想将它们更改为灰度图像(从 rgb 的 3 个通道获得 1 的强度)。我怎么能做得很快?我的代码:

import pickle
import cv2
training_file = "/train.p"

with open(training_file, mode='rb') as f:
train = pickle.load(f)
X_train = train['features']

def rgb2gray(rgb):
    r, g, b = rgb[0], rgb[1], rgb[2]
    gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray

X_train_gray = X_train.copy()

for i in range (X_train_gray.shape[0]):
    for j in range (X_train_gray.shape[1]):
        for k in range …
Run Code Online (Sandbox Code Playgroud)

python image-processing python-3.x

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

image-processing ×1

python ×1

python-3.x ×1