python -opencv 图像锐化

python -opencv 图像锐化

图像锐化其实,是一种增强图片对比度的技术,我们可以通过计算图像的导数,把导数绝对值数值大于零的数值加回原图像,通过这种方法,可以增强图像的对比度。
实现代码如下:

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")

运行结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43327597/article/details/134585938