An improved homomorphic filtering enhancement algorithm for image enhancement-221211 work summary

1. Reference

[1] https://blog.csdn.net/qq_32808045/article/details/108855380
[2] https://blog.csdn.net/star_sky_sc/article/details/122371392
[3] Gong Yun, Jie Xinyu. An improved homomorphic filtering underground image enhancement algorithm [J/OL]. Coal Science and Technology: 1-8[2022-12-09].DOI:10.13199/j.cnki.cst.2021-0774.
[4] https://blog.csdn.net/LaoYuanPython/article/details/120850922

2. Background

Homomorphic filtering is a global enhancement algorithm. For images with a large difference between light and dark, when enhancing certain pixels, some pixels will be over-enhanced, and the enhancement effect will not be good for the highlight and shadow areas.

3. Steps

  1. Adaptive gamma correction of weighted distribution (AGCWD)
    The purpose is to smooth the image probability distribution; enhance the applicability of homomorphic filtering to highlight and shadow areas.
  2. Single parameter homomorphic filtering algorithm
    alleviates the problem of difficult selection of multiple parameters, and a single parameter can also achieve similar enhancement effects.
  3. Contrast limited adaptive histogram equalization (CLAHE)
    further improves contrast and enhances image detail information

4. Adaptive gamma correction with weighted distribution (AGCWD)

1.Background

Gamma correction only uses one parameter γ to adjust, which lacks adaptability and can easily cause over-enhancement in images with uneven light and dark distribution.

2.Principle

Replace the γ coefficient with a compensated cumulative distribution function (cdf), that is:
Insert image description here
Insert image description here
Insert image description here
Insert image description here
It can be seen that the correction coefficient 1-cdf(l)<1 in the formula is also That is to say, γ<1 in gamma correction. At this time, the dark areas of the image are enhanced and the overall image becomes brighter. In areas with lower gray values ​​(darker areas), cdf(l) is smaller, 1-cdf(l) is larger, and the enhancement effect is obvious; in areas with higher gray values ​​(brighter areas) cdf (l) is larger, 1-cdf(l) is smaller, and the enhancement effect is weakened. AGCWD stretches the grayscale to reduce dark image elements and increase bright image elements, making the grayscale distribution smoother and alleviating the over-enhancement phenomenon caused by uneven light and dark distribution in subsequent homomorphic filtering.

3. Function supplement

1. cv2.calcHist() function

is a histogram used to count grayscale values. The x-axis is the grayscale value 0-255, and the y-axis is the number of pixels corresponding to the grayscale value.
cv2.calcHist([img], [channels], mask, [histSize], range)
[img]: [] enclosed, input image, uint8 or float32;
[channels]: enclosed in [], grayscale image - [0], color image - [0], [1], [2] correspond to BGR respectively;
mask: mask image, statistical histogram for a certain part of the image, None means statistical histogram for the entire image;
histSize: enclosed in [], BIN Number of. [256] means that 0-255 is divided into 256 parts; [16] means that 0-255 is divided into 16 parts;
range: pixel value range, usually [0, 256].

2. img.convert()

Return the transformed image and convert the image to the specified color mode. PIL has nine different modes: 1, L, P, RGB, RGBA, CMYK, YCbCr, I, F
"1"represents a black and white image; Note: the density of black dots can represent different grayscales
"L"represents an 8-bit grayscale image.

5. Single parameter homomorphic filtering algorithm

1.Principle

Traditional homomorphic filtering algorithm filter (Gaussian type):
Insert image description here
Parameters that need to be specified:
rH high frequency gain
rL low frequency gain
c sharpening coefficient
n filter order

For general homomorphic filters:
Insert image description here
There are also three parameters that need to be specified. Among them, rH, rL, and n often need to be selected based on experience.

Single parameter filter:
Insert image description here
Only one parameter k needs to be selected.
Principle: The profile of the S-shaped function has a similar structure to the profile of homomorphic filtering;
Advantages: It can be seen from the three-dimensional structure diagram that single-parameter homomorphic filtering Compared with the traditional homomorphic filter, the transition from the center frequency to the high frequency is smoother, the slope is smaller, and the filtering is more uniform.

2. Function supplement

1.np.zeros_like()

Outputs a tensor (array) with the same shape as () and all zero elements.

2.Fourier transform in numpy

np.fft.fft() One-dimensional Fourier transform
np.fft.fft2() Two-dimensional Fourier transform
np .fft.fftn()n-dimensional Fourier transform
np.fft.shift() moves the DC component in the FFT output to the center of the spectrum
np. fft.fftfreq() returns the Fourier transform sampling frequency

3.math.log()

math.log(x [,base])
base represents the base, the default is e

4.np.real()

np.real(val) returns the real part of the complex parameter

6. Contrast Limited Adaptive Histogram Equalization (CLAHE)

1.Background

Classic histogram equalization is a global algorithm and is no longer applicable when only a certain part of the image needs to be equalized.

2.Advantages

Suppress excessive noise enhancement caused by excessive merging of some gray levels and loss of some gray levels during the histogram equalization process, while improving image contrast and making the image outline clearer.

3. Function supplement

Implementing contrast-limited adaptive histogram through CLAHE class in opencv

1. Object construction createCLAHE()

retval = cv.creatCLAHE(clipLimit = 40, tilesGridSize = (8, 8))
titlesGridSize image is divided into tiles called tiles a> small blocks, the default tilesGridSize in opencv is (8, 8), that is, the entire picture is divided into 64 blocks of 8x8, and histogram equalization is performed on each block;
clipLimit clipping limit, corresponding to the contrast limit, the number of pixels exceeded by all bins is accumulated and evenly distributed to all bins;
retval returns the created CLAHE class object.

2. dst = cv2. CLAHE.apply(src [,dst])

src input image, 8-bit or 16-bit grayscale image
dst output image, 8-bit or 16-bit grayscale image

3.retval = cv2.ClAHE.getClipLimit()

Get the ClipLimit value set by the current CLAHE object

4.None = cv2.CLAHE.setClipLimit(clipLimit)

Set the current CLAHE object and set the ClipLimit value

5.retval = cv2.CLAHE.getTilesGridSize()

Get the tilesGridSize value set by the current CLAHE object

6.None = cv2.CLAHE.setClipLimit(clipLimit)

Set the current CLAHE object to set the tilesGridSize value

7. Realization

Here we still use general homomorphic filtering instead of single-parameter homomorphic filtering.

import os
import cv2
import numpy as np
#import matplotlib.pyplot as plt

def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def agcwd(image, w=1):   #加权分布的自适应伽马校正,w就是公式里的alpha
    is_colorful = len(image.shape) >= 3
    img = extract_value_channel(image) if is_colorful else image
    img_pdf = get_pdf(img)
    max_intensity = np.max(img_pdf)
    min_intensity = np.min(img_pdf)
    w_img_pdf = max_intensity * (((img_pdf - min_intensity) / (max_intensity - min_intensity)) ** w) #加权分布后的概率密度函数
    w_img_cdf = np.cumsum(w_img_pdf) / np.sum(w_img_pdf)
    l_intensity = np.arange(0, 256)
    l_intensity = np.array([255 * (e / 255) ** (1 - w_img_cdf[e]) for e in l_intensity], dtype=np.uint8)  #变换后的灰度值
    enhanced_image = np.copy(img)
    height, width = img.shape
    #对像素点依次进行处理
    for i in range(0, height):
        for j in range(0, width):
            intensity = enhanced_image[i, j]
            enhanced_image[i, j] = l_intensity[intensity]
    enhanced_image = set_value_channel(image, enhanced_image) if is_colorful else enhanced_image
    return enhanced_image


def extract_value_channel(color_image):  #提取v通道
    color_image = color_image.astype(np.float32) / 255.
    hsv = cv2.cvtColor(color_image, cv2.COLOR_BGR2HSV)
    v = hsv[:, :, 2]
    return np.uint8(v * 255)


def get_pdf(gray_image):  #获取概率密度函数
    height, width = gray_image.shape
    pixel_count = height * width
    hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])
    return hist / pixel_count


def set_value_channel(color_image, value_channel): #替换color_image中的v通道为value_channel,再把HSV变成RGB
    value_channel = value_channel.astype(np.float32) / 255
    color_image = color_image.astype(np.float32) / 255.
    color_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2HSV)
    color_image[:, :, 2] = value_channel
    color_image = np.array(cv2.cvtColor(color_image, cv2.COLOR_HSV2BGR) * 255, dtype=np.uint8)
    return color_image

def homomorphic_filter(src,d0=10,rl=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)
    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-rl)*(1-np.exp(-c*(D**2/d0**2)))+rl
    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

def clahe(image):  #对比度受限的自适应直方图均衡化
    clahe = cv2.createCLAHE(clipLimit=200, tileGridSize=(8, 8))
    #cl2 = clahe.getClipLimit()
    #clahe.setClipLimit(500)
    #cl1 = clahe.getClipLimit()
    #clahe.setTilesGridSize((3, 3))
    dst = clahe.apply(image)
    return dst

imageDir = "./test_img/"
saveDir_agcwd = "./AGCWD_results/"
saveDir_hf = "./AGCWD_HF_results/"
saveDir_clahe = "./AGCWD_HF_CLAHE_results/"

for name in os.listdir(imageDir):
    img = cv2.imread(os.path.join(imageDir,name))
    image_agcwd = agcwd(img)
    #plt.hist(img.ravel(),256)
    #plt.show()
    #cv2.waitKey(0)
    cv2.imwrite(os.path.join(saveDir_agcwd, name), image_agcwd)
    image_hf = homomorphic_filter(img)
    cv2.imwrite(os.path.join(saveDir_hf, name), image_hf)
    image_clahe = cv2.equalizeHist(image_hf)
    cv2.imwrite(os.path.join(saveDir_clahe, name), image_clahe)


Insert image description here
From left to right are the original image, homomorphic filtering enhancement, and this experimental method enhancement. It can be seen that the enhancement effect of this experiment is not significantly improved compared with the homomorphic filtering method, and the procedure is more complex and time-consuming.
Although this experiment did not achieve good results for the sample images, the experiment found that this algorithm and the AGCWD, single-parameter homomorphic filtering, and CLAHE methods that make up the algorithm have their own applications in image enhancement. The environment can be used as an accumulation for future related research.

Guess you like

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