""""
如何读取图像
"""
from matplotlib import pyplot as plt
"""
plt.imread
Docstring:
Read an image from a file into an array.
从一个文件中读取一幅图片到数组中
或者通过PIL的方式读取图像
from PIL import Image
img1 = Image.open(fp="./imgs/liebao.jpg")
"""
img = plt.imread(fname="./liebao.jpg")
# 显示图像
plt.imshow(img)
"""
读取R通道图像
"""
img_r = img[:, :, 0]
"""
将R通道图像的伪彩色图像通过灰度图的形式显示出来
"""
plt.imshow(X=img_r, cmap="gray")
"""
取图像下半部分
"""
H, W, C = img.shape
H, W, C = img.shape # 解构赋值
img_low = img[H // 2:, :, :]
import cv2
import numpy as np
cv2.__version__ # 查看版本
# 读取图像
img2 = cv2.imread(filename="./liebao1.jpg")
# cv2多为BGR模式
plt.imshow(img2[:,:,1])
# cv2.imshow(winname = "test", mat = img2)
# 由BGR转换成RGB模式
img3 = cv2.cvtColor(src=img2, code = cv2.COLOR_BGR2RGB)
# numpy互换层数
img4 = img2[:, :, ::-1] # 分量全取,但是反着来
"""
numpy层数互换
分量全取,但是反着来
"""
img4 = img2[:, :, ::-1]
"""
举例
"""
ls = ["1", "hello", "abd", 2.67]
ls[::-1]# -是反着来,1是步长
# output:[2.67, 'abd', 'hello', '1']
为什么要做卷积?
那么卷积核应该是什么样的呢?
什么图像可以卷积?
不可解释性:卷积核每次优化都不可解释
为什么每层都单独卷积核,增加参数量
import cv2
from matplotlib import pyplot as plt
from PIL import Image
img = plt.imread(fname="./liebao1.jpg")
# 定义卷积核
kernel = np.array([[-1,0,1],[-2, 0, 2], [-1, 0, 1]])
# ddepth=-1就是三层用同一个卷积核卷积,输出和原来输入一样
img2 = cv2.filter2D(src = img,ddepth=-1,kernel=kernel)
# 卷积核取反
-kernel
# 卷积核取转置
kernel.T
# 定义模糊卷积核
kernel = np.ones(shape=(50, 50)) / 50 / 50
# 将两个卷积结果相加使得特征更加突出
plt.imshow(X=img+img6)
"""
pytoch习惯
In the simplest case, the output value of the layer with input size
:math:`(N, C_{\text{in}}, H, W)` and output :math:`(N, C_{\text{out}}, H_{\text{out}}, W_{\text{out}})`
can be precisely described as:
这里N(批量个数,总共有多少图像)在最前面,C(通道数(层))在第二,H, W高宽,而不是习惯中的最后面
注意不管是处理图像还是合成图像都要按照维度计算和卷积
"""
X = torch.randn(32, 3, 512, 512)
conv2d = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=1)
conv2d(X).shape # 只有层数改变
# output:torch.Size([32, 32, 512, 512])
conv2 = nn.Conv2d(in_channels=3,out_channels=2,kernel_size=3,padding=1,stride=2)
imgs= torch.randn(32,3,5,5)
conv2(imgs).shape
output:torch.Size([32, 2, 3, 3])
# 查看 w,b
w = conv2.state_dict()["weight"]
b = conv2.state_dict()["bias"]
w.shape # 2组,每组3个,大小3x3
# torch.Size([2, 3, 3, 3])