实验三
完整代码:https://github.com/Arancew/digtal-image-processing
- 水平锐化
- 垂直锐化
- Roberts锐化
- Sobel锐化
- Laplacian锐化
- Wallis锐化
import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv
def convolution(img_old, kernel):
img_new = np.zeros(img.shape, dtype=int)
for i in range(1, img_new.shape[0] - 1):
for j in range(1, img_new.shape[1] - 1):
tmp = 0
for k in range(-1, 2):
for l in range(-1, 2):
tmp += img_old[i + k][j + l] * kernel[k + 1][l + 1]
img_new[i][j] = abs(tmp)
return img_new
plt.rc("font", family='Microsoft YaHei')
img = plt.imread('../img/3.1.jpg')
img = img[:, :, 0]
plt.subplot(241), plt.title("1.原图"), plt.axis('off')
plt.imshow(img, cmap="gray")
kernel_horizontal = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
img_horizontal = convolution(img, kernel_horizontal)
plt.subplot(242), plt.title("2.水平锐化后"), plt.axis('off')
plt.imshow(img_horizontal, cmap="gray")
kernel_vertical = np.array([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
img_vertical = convolution(img, kernel_vertical)
plt.subplot(243), plt.title("3.垂直锐化后"), plt.axis('off')
plt.imshow(img_vertical, cmap="gray")
img_Roberts = np.zeros(img.shape, dtype=int)
for i in range(1, img.shape[0] - 1):
for j in range(1, img.shape[1] - 1):
img_Roberts[i][j] = abs((int)(img[i + 1][j + 1]) - (int)(img[i][j])) + abs((int)(img[i + 1][j]) - (int)(img[i][j + 1]))
plt.subplot(244), plt.title("4.Roberts锐化后"), plt.axis('off')
plt.imshow(img_Roberts, cmap="gray")
img_Sobel = np.sqrt(img_vertical*img_vertical + img_horizontal*img_horizontal)
img_Sobel = np.minimum(img_Sobel, 255)
plt.subplot(245), plt.title("5.Sobel锐化后"), plt.axis('off')
plt.imshow(img_Sobel, cmap="gray")
kernel_Laplacian = np.array([[0, -1, 0],
[-1, 4, -1],
[0, -1, 0]])
img_Laplacian = convolution(img, kernel_Laplacian)
img_Laplacian=abs(img_Laplacian)
plt.subplot(246), plt.title("7.Laplacian锐化后"), plt.axis('off')
plt.imshow(img_Laplacian, cmap="gray")
kernel_Laplacian1 = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])
img_Laplacian1 = convolution(img, kernel_Laplacian1)
img_Laplacian1=abs(img_Laplacian1)
plt.subplot(247), plt.title("7.LaplacianH4锐化后"), plt.axis('off')
plt.imshow(img_Laplacian1, cmap="gray")
img_Wallis = np.zeros(img.shape, dtype=int)
for i in range(1, img.shape[0] - 1):
for j in range(1, img.shape[1] - 1):
s=np.log(img[i-1][j]+1)+np.log(img[i+1][j]+1)+np.log(img[i][j-1]+1)+np.log(img[i][j+1]+1)
s*=46
img_Wallis[i][j]=46*np.log(img[i][j])-s/4
plt.subplot(248), plt.title("8.Wallis锐化后"), plt.axis('off')
plt.imshow(img_Wallis, cmap="gray")
plt.show()