Python implementation of homomorphic filtering for image enhancement - 20221204 work summary

reference

[1]https://wenku.baidu.com/view/4eb598180a12a21614791711cc7931b765ce7ba9.html?wkts=1670054222364&bdQuery=%E5%90%8C%E6%80%81%E6%BB%A4%E6%B3%A2python%E5%AE%9E%E7%8E%B0

Homomorphic filtering python implementation

code

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)

Effect

Original image on the left, enhancement on the bottom
Insert image description here
Aiming at the tiny blisters, it has a certain enhancement effect
Original image on the left, enhancement on the right
Insert image description here
Insert image description here
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_45246566/article/details/128164659