RGB转化为Gray图像 用于深度学习训练 PIL

实验需要,将RGB转化为gray,可以单独处理RGB数据,或者在训练时,数据准备阶段,利用transforms直接转化。

RGB转gray可以通过PIL或者CV2相应算法库进行转化。另一种选择是通过公式Y = 0.299 R + 0.587 G + 0.114 B.从RGB计算灰度(Y)通道 切片数组并转换为一个通道。

class Gray(object)

    def __call__(self, tensor):
        # TODO: make efficient
        R = tensor[0]
        G = tensor[1]
        B = tensor[2]
        tensor[0]=0.299*R+0.587*G+0.114*B
        tensor = tensor[0]
        tensor = tensor.view(1,w,h)
        return tensor

注意PIL读取图片为RGB格式,cv2.imread读取图片为BGR格式。

数据准备阶段 transform转化

import torch
import torchvision
import torchvision.transforms as transforms
from PIL import Image


image_path = "/home/WuHF/whf/pytorch_code/dttNet/dataset/fusion_datasets/lytro-01-A.jpg"
image = Image.open(image_path)
 
input_transform = transforms.Compose([
   transforms.Grayscale(num_output_channels=1), #这一句就是转为单通道灰度图像
   transforms.ToTensor(),
])
image_tensor = input_transform(image)

 

数据单独转化

采用convert函数将RGB转化为Gray,采用expand_dims和repeat函数扩充到三通道。

import numpy as np
from PIL import Image

img_path =  'test.jpg'

img = Image.open(img_path)
# print(np.array(img).shape)
# print(np.array(img)[50:52,50:60,:])

# 转化为灰度图,这里建议先转化为灰度图,再进行resize。
img = img.convert('L')
img = img.resize((144, 288), Image.ANTIALIAS)
# print(np.array(img).shape)
# print(np.array(img)[50:52,50:60])

#灰度图单通道复制扩充到三通道
img = np.expand_dims(img, axis=0)
img = np.array(img).repeat(3, axis=0)
# print(np.array(img).shape)
# print(np.array(img)[...,50:52,50:60])

repeat函数使用

numpy.repeat(a,repeats,axis=None)或者是object(ndarray).repeat(repeats,axis=None)

axis=None,时候就会flatten当前矩阵,实际上就是变成了一个行向量

                      axis对应维度扩充repeats倍,

repeats可为数,也可为矩阵(针对axis维度的元素),复制repeats次。

a=numpy.array(([[1,2], [3,4]]))

#[8]
numpy.repeat(a,2)
# array([[1,1,2,2,3,3,4,4]])

#0维度2倍 [4,2]
numpy.repeat(a,2,axis=0)
# array([[1,2], 
         [1,2], 
         [3,4], 
         [3,4]])

#1维度2倍 [2,4]
numpy.repeat(a,2,axis=1)
# array([[1,1,2,2], 
         [3,3,4,4]])


# [5,2] 矩阵对应元素复制n倍
numpy.repeat(a,[2,3],axis=0)
# array([[1,2], 
         [1,2], 
         [3,4], 
         [3,4], 
         [3,4]])

Numpy扩充矩阵维度(np.expand_dims, np.newaxis)和删除维度(np.squeeze)

np.newaxis和np.expand_dims扩充矩阵维度

import numpy as np
 
x = np.arange(8).reshape(2, 4)
 
# 添加第0维,输出shape -> (1, 2, 4),这里np.newaxis所在的位置就是新添加的维度位置
x1 = x[np.newaxis, :]
print(x1.shape)
 
# 添加第1维, 输出shape -> (2, 1, 4),这里直接指定axis即可指定维度
x2 = np.expand_dims(x, axis=1)
print(x2.shape)

np.squeeze删除矩阵中维度大小为1的维度

"""
    squeeze 函数:从数组的形状中删除单维度条目,即把shape中为1的维度去掉
    用法:numpy.squeeze(a,axis = None)
     1)a表示输入的数组;
     2)axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
     3)axis的取值可为None 或 int 或 tuple of ints, 可选。若axis为空,则删除所有单维度的条目;
     4)返回值:数组
     5) 不会修改原数组;
"""
 
 
import numpy as np

x = np.arange(10).reshape(1, 1, 10, 1)
print(x.shape,x)

(1, 1, 10, 1)
[[[[0]
   [1]
   [2]
   [3]
   [4]
   [5]
   [6]
   [7]
   [8]
   [9]]]]


#去掉axis=0这个维度
x_squeeze_0 = np.squeeze(x, axis=0)
print(x_squeeze_0.shape, x_squeeze_0)
 
(1, 10, 1) [[[0]
  [1]
  [2]
  [3]
  [4]
  [5]
  [6]
  [7]
  [8]
  [9]]]

#去掉axis=3这个维度
x_squeeze_3 = np.squeeze(x, axis=3)
print(x_squeeze_3.shape, x_squeeze_3)
 

(1, 1, 10) [[[0 1 2 3 4 5 6 7 8 9]]]

#去掉axis=0, axis=1这两个维度
x_squeeze_0_1 = np.squeeze(x, axis=(0, 1))
print(x_squeeze_0_1.shape, x_squeeze_0_1)
 

(10, 1) [[0]
 [1]
 [2]
 [3]
 [4]
 [5]
 [6]
 [7]
 [8]
 [9]]

#去掉所有1维的维度
x_squeeze = np.squeeze(x)
print(x_squeeze.shape, x_squeeze)
 
(10,) [0 1 2 3 4 5 6 7 8 9]

#去掉不是1维的维度,抛异常
try:
    x_squeeze = np.squeeze(x, axis=2)
    print(x_squeeze.shape, x_squeeze)
except Exception as e:
    print(e)

#cannot select an axis to squeeze out which has size not equal to one


 

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值