简体   繁体   English

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

[英]How fast in Python change 3 channel rgb color image to 1 channel gray?

I have almost 40000 images in a 4D array containing raw pixel data - (number of examples, width, height, channels).我在包含原始像素数据的 4D 数组中拥有近 40000 张图像 - (示例数量、宽度、高度、通道)。 Every image has width of 32 pixels, height of 32 pixels, and 3 channels for RGB colors.每个图像的宽度为 32 像素,高度为 32 像素,以及 3 个 RGB 颜色通道。 I want to change them to grayscale images (from 3 channels with rgb get 1 with intensity).我想将它们更改为灰度图像(从 rgb 的 3 个通道获得 1 的强度)。 How I can do it quite fast?我怎么能做得很快? My code:我的代码:

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 (X_train_gray.shape[2]):
            rgb = X_train_gray[i,j,k]
            gray = rgb2gray(rgb)
            X_train_gray[i,j,k] = gray

print("X_train image data shape =", X_train.shape)
print("X_train_grey image data shape =", X_train_gray.shape)

Result:结果:
X_train_grey image data shape = (40000, 32, 32, 3) X_train_grey 图像数据形状 = (40000, 32, 32, 3)
X_train_grey image data shape = (40000, 32, 32, 1) X_train_grey 图像数据形状 = (40000, 32, 32, 1)
It's good, but it takes a lot of time.这很好,但需要很多时间。

I also tried to use cv2:我也尝试使用 cv2:

X_train_gray = X_train[0].copy()
print("X_train_grey image data shape =", X_train_gray.shape)
X_train_gray = cv2.cvtColor(X_train_gray, cv2.COLOR_BGR2GRAY)
print("X_train_grey image data shape =", X_train_gray.shape)

Result:结果:
X_train_grey image data shape = (32, 32, 3) X_train_grey 图像数据形状 = (32, 32, 3)
X_train_grey image data shape = (32, 32) X_train_grey 图像数据形状 = (32, 32)
But I lose intensity and don't know how to get it.但我失去了强度,不知道如何获得它。
So how in fast way I can change this images from 3 channel rgb to 1 channel gray?那么如何快速将这些图像从 3 通道 rgb 更改为 1 通道灰色?

if you can use PIL.如果你可以使用PIL。 It should be ok.应该没问题。 I had RGB images and convert them:我有 RGB 图像并转换它们:

from PIL import Image
img = Image.open("image_file_path") #for example image size : 28x28x3
img1 = img.convert('L')  #convert a gray scale
print(img1.size)
>> (28,28)

But the image doesn't have a channel但是图片没有频道

y = np.expand_dims(img1, axis=-1)
print(y.shape)
>> (28,28,1)

I had this Problem before.This is the Best way: Your code is correct but needs some more changes to be suitable for a grayscaled image.我之前遇到过这个问题。这是最好的方法:您的代码是正确的,但需要进行更多更改才能适用于灰度图像。 Here is the Code:这是代码:

ii = cv2.imread("0.png")
gray_image = cv2.cvtColor(ii, cv2.COLOR_BGR2GRAY)
print(gray_image)
plt.imshow(gray_image,cmap='Greys')
plt.show()

and this is the result:这是结果:

[[196 196 197 195 195 194 195 197 196 195 194 194 196 194 196 189 188 195 195 196 197 198 195 194 194 195 193 191] . [[196 196 197 195 195 194 195 197 196 195 194 194 196 194 196 189 188 195 195 196 197 198 195 49 19 195 19 19. . . . . [194 194 193 193 191 189 193 193 192 193 191 194 193 192 192 191 192 192 193 196 199 198 200 200 200 201 200 199]] [194 194 193 193 191 189 193 193 192 193 191 194 193 192 192 191 192 192 193 196 199 198 200 200 19] 200 200 19

在此处输入图片说明 . .

尝试使用:

cv::cvtColor(gray_img,color_img,CV_GRAY2BGR)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM