5-1感觉这些方法主体都差不多,就是微分算子不同,懒得一个个写了
from cv2 import cv2
import numpy as np
import random
def singleDirectionsharpen(img, N=3):
p = N // 2
img_shape = np.shape(img)
out = np.zeros(img_shape)
for i in range(img_shape[0]):
for j in range(img_shape[1]):
if i >= p and i < img_shape[0] - p and j >= p and j < img_shape[1] - p:
out[i][j] = img[i - 1][j - 1] - img[i + 1][j - 1] + 2 * (img[i - 1][j] - img[i + 1][j]) + img[i - 1][j + 1] - img[i + 1][j + 1]
else:
out[i][j] = 0
return out
img = cv2.imread("C:\\test\\1.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
single_irection_sharpen = singleDirectionsharpen(gray)
cv2.imwrite("C:\\test\\single_irection_sharpen.jpg", single_irection_sharpen)
single_irection_sharpen = cv2.imread("C:\\test\\single_irection_sharpen.jpg")
cv2.imshow('gray', gray)
cv2.imshow('single_irection_sharpen', single_irection_sharpen)
cv2.waitKey()
cv2.destroyAllWindows