第一题:Take several pictures of red, blue, and green items with your phone or other digital camera (or download some from the internet, if a camera isn’t available). 下载一些红蓝绿的照片,然后进行一些操作
由于是用jupyter写的,所以格式不大好看,输入输出会有提示符。
a. Load each image, and convert it to a tensor
利用imageio库,来读取图片。读取的图片格式为numpy的数组,利用torch.from_numpy变成tensor。
import torch
import imageio
red_arr = imageio.imread(r'F:\Users\asus\JupyterProjects\data\red.jpg')
blue_arr = imageio.imread(r'F:\Users\asus\JupyterProjects\data\blue.jpg')
green_arr = imageio.imread(r'F:\Users\asus\JupyterProjects\data\green.jpg')
red = torch.from_numpy(red_arr)
blue = torch.from_numpy(blue_arr)
green = torch.from_numpy(green_arr)
red_arr.shape, blue_arr.shape, green_arr.shape
Out[76]:
((424, 336, 3), (667, 500, 3), (500, 500, 3))
b. For each image tensor, use the .mean() method to get a sense of how bright the image is. 利用mean方法查看每张图的亮度。
对一个tensor直接使用mean方法,是指对所有元素求平均值。但是先要把整型变成浮点型才能用mean方法。
red = red.float()
red.mean()
Out[84]:
tensor(68.0852)
blue = blue.float()
green = green.float()
blue.mean()
Out[86]:
tensor(134.8569)
green.mean()
Out[87]:
tensor(88.2007)
c.Take the mean of each channel of your images. Can you identify the red, green, and blue items from only the channel averages?对三个通道分别求mean,你能通过tensor的平均值的值直接看出图片的颜色吗
这里稍微有一点难,涉及到mean()这个函数的具体参数。这一片文章说的很详细:mean()。首先对red的第一维r_a = torch.mean(red,0)
求平均值,得到的结果如out89所示,可以看出来此时r_a的维度已经变成二维了。然后再对r_a求meanr_b = torch.mean(r_a,0)
,其他蓝、绿色的处理也相同,只是我连写了两次求均值。
如何通过tensor直接看出颜色呢?
python中使用的是RGB三通道,所以一个1*3的tensor的三个元素就分别代表了红绿蓝三种颜色。从out90,97,98可以看出,当图片为某一颜色时,相应地,该通道的数值就要大一些,如:[200.8518, 1.3317, 2.0721],对应我下载的图片就为红色图片:
red.shape
Out[88]:
torch.Size([424, 336, 3])
r_a = torch.mean(red,0)
r_a
Out[89]:
tensor([[153.9741, 1.0094, 0.9269],
[154.3160, 0.9741, 0.8608],
[155.1722, 1.0425, 1.1108],
...,
[248.8891, 1.3255, 6.5991],
[249.2618, 1.5590, 6.9316],
[249.4576, 1.8019, 7.1981]])
r_b = torch.mean(r_a,0)
r_b
Out[90]:
tensor([200.8518, 1.3317, 2.0721])
torch.mean(torch.mean(blue,0),0)
Out[97]:
tensor([ 53.7911, 138.5508, 212.2290])
torch.mean(torch.mean(green,0),0)
Out[98]:
tensor([ 80.7196, 144.1003, 39.7822])
这里我通过一个小例子来说明两次对第一维就均值就是对三个通道求均值。
x = [[[1,2,3],
[4,5,6]],
[[1,2,3],
[4,5,6]]]
x_t = torch.tensor(x)
x_t.shape
Out[92]:
torch.Size([2, 2, 3])
x_t = x_t.float()
x_t1 = torch.mean(x_t,0)
x_t1
Out[94]:
tensor([[1., 2., 3.],
[4., 5., 6.]])
x_t2 = torch.mean(x_t1,0)
x_t2
Out[96]:
tensor([2.5000, 3.5000, 4.5000])