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")
実行結果は次のとおりです。