1.注意矩阵计算时的行列数量要匹配,读取和显示图像引入opencv,要加上等待waitkey不然图像会闪退。
# 浮雕滤波
kernel = np.array([[-2, -1, 0],
[-1, 1, 1],
[0, 1, 2]], dtype="float32")
# 平均模糊
kernel = np.ones([8, 8])/64
# 纵向边缘
kernel = np.ones([8, 8])
kernel[:, :4] = -1
# 横向边缘
kernel = np.ones([8, 8])
kernel[:4, :] = -1
while True:
_, img = cap.read()
img_flt = cv2.filter2D(img, -1, kernel)
cv2.imshow("a", img_flt)
ret = cv2.waitKey(100)
print(ret)
2.图像是有红绿蓝三个通道的,可以看做是三张没有厚度的纸张叠在一起,而做运算时是可以对每张纸分别处理的。奇异值分解就是线代里面的特征值提取。
img = mpimg.imread('img/1.jpg')
img = img[:, :, :]/255
# 灰度化
a1, a2, a3 = 0.2989, 0.5870, 0.1140
img_gray = img[:,:,0]*a1+img[:,:,1]*a2+img[:,:,2]*a3
# 奇异值分解
from numpy import linalg as LA
M, Q, N = np.linalg.svd(img_gray, full_matrices=True)
Q=np.diag(Q)
re_img=M[:, :50] @ Q[:50, :50] @ N[:50, :]
plt.imshow(np.real(re_img), cmap=plt.get_cmap("gray"))
plt.show()
3.旋转中引入三角函数保证了旋转中尺寸不变,拉伸就比较好理解直接乘。多次变换也就是依次做矩阵乘法。
X = np.random.random([1000, 2])
# 空间拉伸
W1 = np.array([[0.5, 0],
[0.0, 2]]) # |W|=1
X1 = X @ W1 # 如果+b就是空间平移
# 空间旋转
o = np.pi/4
W2 = np.array([[np.cos(o), np.sin(o)], [-np.sin(o), np.cos(o)]]) #|w|=1
X2 = X @ W2
# 旋转和拉伸
W3 = W2 @ W1 @ W2.T
X3 = X @ W3