Python image dehazing algorithm based on OpenCV [full source code & deployment tutorial]

1. Image recognition

2.png

3.png

4.png

2. Video display

[Project Sharing]Python Image Dehazing Algorithm Based on OpenCV [Complete Source Code & Deployment Tutorial]_哔哩哔哩_bilibili

3. Algorithm principle

Image enhancement algorithms are often used to adjust the brightness, contrast, saturation, hue, etc. of the image to increase its clarity and reduce noise. Image enhancement often completes the above functions through the combination of multiple algorithms. For example, image denoising is equivalent to a low-pass filter, and increasing clarity is a high-pass filter. Of course, enhancing an image is mainly for the final acquisition of useful image information. The general algorithm flow can be: de-drying the image, increasing the definition (contrast), graying or obtaining the edge features of the image or convolving and binarizing the image, etc. The above four steps can often be realized through different steps , follow-up will conduct special experiments on this aspect, enumerate its application scenarios and processing characteristics.

Image Enhancement Based on Histogram Equalization

Image contrast enhancement methods can be divided into two types: direct contrast enhancement methods and indirect contrast enhancement methods. Histogram stretching and histogram equalization are common indirect contrast enhancement methods. Histogram stretching is to use contrast stretching to adjust the histogram to expand the difference between the foreground and background gray levels. This method can be realized by linear and nonlinear methods. In ps, this method is used to improve the contrast; histogram Equalization is to use the cumulative function to adjust the gray value to achieve contrast enhancement.
Histogram equalization processing principle: the grayscale image of the original image is evenly distributed in the entire grayscale space from a relatively concentrated grayscale interval, so as to realize the nonlinear stretching of the image and redistribute the image pixel values.

Algorithm application scenarios:
1. The essence of the algorithm is to redistribute the pixel values ​​of the image, adding a lot of local contrast, and the overall contrast has not changed much, so the application image is similar to the contrast of the useful data of the image, for example: X-ray image , which can better display overexposed or underexposed photos, or images with too bright or too dark backgrounds and foregrounds are very useful.
2. Of course, the algorithm also has disadvantages. The specific performance is: the gray level of the transformed image is reduced, and some details are reduced; some images have high peaks, and the contrast after processing is unnaturally over-enhanced.

Algorithm implementation features:
1. Equalization process: Histogram equalization ensures that the original size relationship remains unchanged during the image pixel mapping process, that is, the brighter areas are still brighter, and the darker areas are still darker, but the contrast is increased. Light and dark are reversed; the value range of the pixmap function is guaranteed to be between 0 and 255. The cumulative distribution function is a single growth function and has a range of 0 to 1.
2. The implementation process of the cumulative distribution function:
compare the probability distribution function and the cumulative distribution function. The two-dimensional image of the former is uneven, while the latter is monotonically increasing. During histogram equalization, the mapping method is
5.png
where n is the sum of pixels in the image.

Image Enhancement Based on Laplacian

The essence of image enhancement using Laplace operator is to use the second differential of the image to degrade the image. In the image field, the differential is sharpening, and the integral is blurring. Using the second differential to degrade the image is to use the neighborhood pixels to improve contrast. There is also a Laplacian function in opencv, but at that time grayscale images were generated, and more edges were obtained. The specific source code has not been studied yet. The principle can refer to my previous article. There is a detailed description of Laplacian introduction.
6.png

Image Enhancement Based on Object Log Transform

Logarithmic transformation can expand the low gray value part of the image to show more details of the low gray value part, compress the high gray value part, reduce the details of the high gray value part, so as to emphasize the low gray value of the image part of the purpose. Transformation method:
The function of logarithmic transformation to enhance the details of the low-grayscale part of the image can be intuitively understood from the logarithmic graph: 0.4 on the
7.png
x-axis corresponds to 0.8 on the y-axis, that is, the low-grayscale part of 0 0.4 on the original image passes through After the logarithmic operation, it is extended to the part of 0 0.8, and the entire high gray level part of 0.4 1 is projected into the interval of only 0.8 1, so that the function of expanding and enhancing the low gray level part and compressing the value of the high gray level part is achieved .

It can also be seen from the above figure that for different base numbers, the larger the base number, the stronger the expansion of the low gray level part, and the stronger the compression of the high gray level part.

4. Code implementation

# 图像增强算法,图像锐化算法
# 1)基于直方图均衡化 2)基于拉普拉斯算子 3)基于对数变换 4)基于伽马变换 5)CLAHE 6)retinex-SSR 7)retinex-MSR
# 其中,基于拉普拉斯算子的图像增强为利用空域卷积运算实现滤波
# 基于同一图像对比增强效果
# 直方图均衡化:对比度较低的图像适合使用直方图均衡化方法来增强图像细节
# 拉普拉斯算子可以增强局部的图像对比度
# log对数变换对于整体对比度偏低并且灰度值偏低的图像增强效果较好
# 伽马变换对于图像对比度偏低,并且整体亮度值偏高(对于相机过曝)情况下的图像增强效果明显

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


# 直方图均衡增强
def hist(image):
    r, g, b = cv2.split(image)
    r1 = cv2.equalizeHist(r)
    g1 = cv2.equalizeHist(g)
    b1 = cv2.equalizeHist(b)
    image_equal_clo = cv2.merge([r1, g1, b1])
    return image_equal_clo


# 拉普拉斯算子
def laplacian(image):
    kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
    image_lap = cv2.filter2D(image, cv2.CV_8UC3, kernel)
    return image_lap


# 对数变换
def log(image):
    image_log = np.uint8(np.log(np.array(image) + 1))
    cv2.normalize(image_log, image_log, 0, 255, cv2.NORM_MINMAX)
    # 转换成8bit图像显示
    cv2.convertScaleAbs(image_log, image_log)
    return image_log


# 伽马变换
def gamma(image):
    fgamma = 2
    image_gamma = np.uint8(np.power((np.array(image) / 255.0), fgamma) * 255.0)
    cv2.normalize(image_gamma, image_gamma, 0, 255, cv2.NORM_MINMAX)
    cv2.convertScaleAbs(image_gamma, image_gamma)
    return image_gamma


# 限制对比度自适应直方图均衡化CLAHE
def clahe(image):
    b, g, r = cv2.split(image)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    b = clahe.apply(b)
    g = clahe.apply(g)
    r = clahe.apply(r)
    image_clahe = cv2.merge([b, g, r])
    return image_clahe


def replaceZeroes(data):
    min_nonzero = min(data[np.nonzero(data)])
    data[data == 0] = min_nonzero
    return data


# retinex SSR
def SSR(src_img, size):
    L_blur = cv2.GaussianBlur(src_img, (size, size), 0)
    img = replaceZeroes(src_img)
    L_blur = replaceZeroes(L_blur)

    dst_Img = cv2.log(img/255.0)
    dst_Lblur = cv2.log(L_blur/255.0)
    dst_IxL = cv2.multiply(dst_Img, dst_Lblur)
    log_R = cv2.subtract(dst_Img, dst_IxL)

    dst_R = cv2.normalize(log_R,None, 0, 255, cv2.NORM_MINMAX)
    log_uint8 = cv2.convertScaleAbs(dst_R)
    return log_uint8


def SSR_image(image):
    size = 3
    b_gray, g_gray, r_gray = cv2.split(image)
    b_gray = SSR(b_gray, size)
    g_gray = SSR(g_gray, size)
    r_gray = SSR(r_gray, size)
    result = cv2.merge([b_gray, g_gray, r_gray])
    return result


# retinex MMR
def MSR(img, scales):
    weight = 1 / 3.0
    scales_size = len(scales)
    h, w = img.shape[:2]
    log_R = np.zeros((h, w), dtype=np.float32)

    for i in range(scales_size):
        img = replaceZeroes(img)
        L_blur = cv2.GaussianBlur(img, (scales[i], scales[i]), 0)
        L_blur = replaceZeroes(L_blur)
        dst_Img = cv2.log(img/255.0)
        dst_Lblur = cv2.log(L_blur/255.0)
        dst_Ixl = cv2.multiply(dst_Img, dst_Lblur)
        log_R += weight * cv2.subtract(dst_Img, dst_Ixl)

    dst_R = cv2.normalize(log_R,None, 0, 255, cv2.NORM_MINMAX)
    log_uint8 = cv2.convertScaleAbs(dst_R)
    return log_uint8


def MSR_image(image):
    scales = [15, 101, 301]  # [3,5,9]
    b_gray, g_gray, r_gray = cv2.split(image)
    b_gray = MSR(b_gray, scales)
    g_gray = MSR(g_gray, scales)
    r_gray = MSR(r_gray, scales)
    result = cv2.merge([b_gray, g_gray, r_gray])
    return result


if __name__ == "__main__":
    image = cv2.imread("example.jpg")
    image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    plt.subplot(4, 2, 1)
    plt.imshow(image)
    plt.axis('off')
    plt.title('Offical')

    # 直方图均衡增强
    image_equal_clo = hist(image)

    plt.subplot(4, 2, 2)
    plt.imshow(image_equal_clo)
    plt.axis('off')
    plt.title('equal_enhance')

    # 拉普拉斯算法增强
    image_lap = laplacian(image)

    plt.subplot(4, 2, 3)
    plt.imshow(image_lap)
    plt.axis('off')
    plt.title('laplacian_enhance')

    # LoG对象算法增强
    image_log = log(image)

    plt.subplot(4, 2, 4)
    plt.imshow(image_log)
    plt.axis('off')
    plt.title('log_enhance')

    # 伽马变换
    image_gamma = gamma(image)

    plt.subplot(4, 2, 5)
    plt.imshow(image_gamma)
    plt.axis('off')
    plt.title('gamma_enhance')

    # CLAHE
    image_clahe = clahe(image)

    plt.subplot(4, 2, 6)
    plt.imshow(image_clahe)
    plt.axis('off')
    plt.title('CLAHE')

    # retinex_ssr
    image_ssr = SSR_image(image)

    plt.subplot(4, 2, 7)
    plt.imshow(image_ssr)
    plt.axis('off')
    plt.title('SSR')

    # retinex_msr
    image_msr = MSR_image(image)

    plt.subplot(4, 2, 8)
    plt.imshow(image_msr)
    plt.axis('off')
    plt.title('MSR')

    plt.show()

5. Project display

1.png

6. Complete source code & environment deployment video tutorial & algorithm principle:

Baidu Bread can download the source code by searching more title names

7. References

1、2008,Single image dehazing,R. Fattal,ACM Transactions on Graphics.


2、2014,Efficient Image Dehazing with Boundary Constraint and Contextual Regularization, G. Meng, Y. Wang, J. Duan, S. Xiang and C. Pan,ICCV


3、2016,Non-local image dehazing,D. Berman, T. Treibitz and S. Avidan, CVPR.


4、2009,Single image haze removal using dark channel prior,K. He, J. Sun and X. Tang,CVPR.


5、2017,Single Image Dehazing Based on the Physical Model and MSRCR Algorithm,J. B. Wang, K. Lu, J. Xue, N. He and L. Shao,TCSVT


6、2013,Hardware Implementation of a Fast and Efficient Haze Removal Method,Y. H. Shiau, H. Y. Yang, P. Y. Chen and Y. Z. Chuang,TCSVT


7、2014,Visibility Restoration of Single Hazy Images Captured in Real-World Weather Conditions,S. C. Huang, B. H. Chen and W. J. Wang,TCSVT


8、2015,Single image dehazing with a physical model and dark channel prior,J. B. Wang, N. He, L. L. Zhang and K. Lu,Neurocomputing


9、2017,An Efficient Fusion-Based Defogging,J. M. Guo, J. Y. Syue, V. R. Radzicki and H. Lee,Tip


10、2017,Haze Removal Using the Difference-Structure-Preservation Prior,L. Y He, J. Z. Zhao, N. N. Zheng and D.Y. Bi,TIP

Guess you like

Origin blog.csdn.net/cheng2333333/article/details/126621146