python -opencv image sharpening
Image sharpening is actually a technique for enhancing the contrast of an image. We can calculate the derivative of the image and add the absolute value of the derivative greater than zero back to the original image. In this way, the contrast of the image can be enhanced.
The implementation code is as follows:
import copy
import math
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import os
import cv2
plt.rcParams['font.family'] = 'Microsoft YaHei'
def cv_show(name,img):
cv2.imshow(name,img)
#cv2.waitKey(0),接收0,表示窗口暂停
cv2.waitKey(0)
#销毁所有窗口
cv2.destroyAllWindows()
path=r'D:\learn\photo\cv\muxing.jpg'
img=cv2.imread(path,0)
row,col=img.shape
gra=np.zeros((row,col))
img=img.astype('float')
gra=gra.astype('float')
for x in range(row-1):
for y in range(col-1):
gx=abs(img[x+1,y]-img[x,y])
gy=abs(img[x,y+1]-img[x,y])
gra[x,y]=gx+gy
sharp=img+gra
sharp=np.where(sharp>255,255,sharp)
sharp=np.where(sharp<0,0,sharp)
gra=gra.astype('uint8')
sharp=sharp.astype('uint8')
#cv_show('img',img)
#cv_show('sharp',sharp)
plt.subplot(121)
plt.imshow(img,'gray')
plt.title('原图')
plt.subplot(122)
plt.imshow(sharp,'gray')
plt.title('锐化图')
#plt.subplot(223)
#plt.imshow(img_s)
#plt.title('平移')
#plt.subplot(224)
#plt.imshow(img_r)
#plt.title('旋转')
plt.show()
os.system("pause")
The running results are as follows: