赞
踩
import os import cv2 from PIL import Image import numpy as np def cv_show(name,img): cv2.imshow(name,img) cv2.waitKey(0) cv2.destroyAllWindows() def homomorphic_filter(src,d0=10,r1=0.5,rh=2,c=4,h=2.0,l=0.5): gray = src if len(src.shape)>2: gray = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY) gray = np.float64(gray) rows,cols = gray.shape gray_fft = np.fft.fft2(gray) gray_fftshift = np.fft.fftshift(gray_fft) dst_fftshift = np.zeros_like(gray_fftshift) M,N = np.meshgrid(np.arange(-cols//2,cols//2),np.arange(-rows//2,rows//2)) D = np.sqrt(M**2+N**2) Z = (rh-r1)*(1-np.exp(-c*(D**2/d0**2)))+r1 dst_fftshift = Z*gray_fftshift dst_fftshift = (h-l)*dst_fftshift+l dst_ifftshift = np.fft.ifftshift(dst_fftshift) dst_ifft = np.fft.ifft2(dst_ifftshift) dst = np.real(dst_ifft) dst = np.uint8(np.clip(dst,0,255)) return dst imageDir = "./img/" saveDir = "./HomoFilter_results/" for name in os.listdir(imageDir): img = Image.open(os.path.join(imageDir, name)) img = img.convert('L') img = np.array(img) #print(img,img.shape) img_new = homomorphic_filter(img) #print('new img shape is {}',format(img_new.shape)) #cv_show('1',img_new) cv2.imwrite(os.path.join(saveDir, name),img_new)
上原图,下增强
针对微小的砂眼缺陷,有一定的增强效果
左原图,右增强
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。