重生学图像之图像增强

1、图像增强

主要是针对在经过JPEG压缩后的图像进行增强,以增强视觉效果,便于人们观察。下面两篇讲了什么是图像增强以及特别细致的空域和频域的图像增强算法原理。

【图像增强】常见的图像增强算法-优快云博客 

【笔记】数字图像处理--图像增强(空间域+频率域)_空间域和频率域-优快云博客

2、传统图像增强算法

1)空域图像增强处理方法

  • 线性变换:通过调整图像的亮度范围,使图像的对比度更明显。

  • 直方图均衡化:调整图像的灰度分布,使图像的直方图更加均匀。自适应直方图均衡化(CLAHE):直方图均衡化是对全局进行的,就会导致部分细节丢失、导致某些区域过亮或过暗,CLAHE局部直方图均衡化,避免过度增强。

  • matlab代码如下:

  • img = imread('example.jpg');
    gray_img = rgb2gray(img);
    
    % 直方图均衡化
    equalized_img = histeq(gray_img);
    
    % 显示结果
    subplot(1, 3, 1), imshow(gray_img), title('原始图像');
    subplot(1, 3, 2), imshow(equalized_img), title('直方图均衡化');
    subplot(1, 3, 3), imhist(equalized_img), title('均衡化后的直方图');
    
    
    % CLAHE
    clahe = adapthisteq(gray_img, 'ClipLimit', 0.02);
    
    % 显示结果
    subplot(1, 3, 1), imshow(gray_img), title('原始图像');
    subplot(1, 3, 2), imshow(clahe), title('CLAHE');
    subplot(1, 3, 3), imhist(clahe), title('CLAHE后的直方图');
  • python代码如下:

  • import cv2
    import matplotlib.pyplot as plt
    
    # 读取图像
    image = cv2.imread('example.jpg', cv2.IMREAD_GRAYSCALE)
    
    # 直方图均衡化
    equalized_image = cv2.equalizeHist(image)
    
    # CLAHE
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    clahe_image = clahe.apply(image)
    
    
    
  • 高斯滤波:平滑图像,去除高斯噪声。

  • 中值滤波:去除椒盐噪声。

  • 双边滤波:在平滑图像的同时保留边缘信息。

  • 非局部均值滤波:图像中的每个像素值可以通过其邻域内的相似像素加权平均来估计

  • matlab代码如下:

  • img = imread('example.jpg');
    gray_img = rgb2gray(img);
    
    % 高斯滤波
    sigma = 1.5;  % 标准差
    gaussian_filtered = imgaussfilt(gray_img, sigma);
    
    
    %%中值滤波
    median_filtered = medfilt2(gray_img, [5 5]);  % 5x5
    
    
    %%双边滤波
    d = 9  # 滤波时周围每个像素邻域的直径
    sigma_color = 75  # 颜色空间过滤器的sigma值
    sigma_space = 75  # 坐标空间中滤波器的sigma值
    bilateral_filtered = cv2.bilateralFilter(image, d, sigma_color, sigma_space)
    
    
    %%%非局部均值滤波
    denoised_img = imnlmfilt(noisy_img);
  • python代码如下

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    
    # 读取图像
    image = cv2.imread('example.jpg', cv2.IMREAD_GRAYSCALE)
    
    # 高斯滤波
    sigma = 1.5  # 标准差
    gaussian_filtered = cv2.GaussianBlur(image, (5, 5), sigma)  # 5x5 的高斯核
    
    # 中值滤波
    median_filtered = cv2.medianBlur(image, 5)  # 5x5 的中值滤波
    
    
    # 双边滤波
    d = 9  # 滤波时周围每个像素邻域的直径
    sigma_color = 75  # 颜色空间过滤器的sigma值
    sigma_space = 75  # 坐标空间中滤波器的sigma值
    bilateral_filtered = cv2.bilateralFilter(image, d, sigma_color, sigma_space)
    
    
  • 饱和度增强:提高图像的色彩鲜艳度。

  • 色调调整:调整图像的整体色调。

  • matlab代码如下:

  • img = imread('path_to_image.jpg');
    if size(img, 3) == 3
        hsv_img = rgb2hsv(img); % 转换到 HSV 色彩空间
    else
        hsv_img = img; % 如果已经是灰度图像,直接使用
    end
    
    % 分离通道
    h = hsv_img(:, :, 1);
    s = hsv_img(:, :, 2);
    v = hsv_img(:, :, 3);
    
    % 调整饱和度
    saturation_factor = 1.5; % 调整因子,大于1增加饱和度,小于1降低饱和度
    s = min(s * saturation_factor, 1); % 限制饱和度不超过1
    
    % 合并通道并转换回 RGB
    adjusted_img = hsv2rgb(cat(3, h, s, v));
    
    % 显示结果
    figure;
    subplot(1, 2, 1), imshow(img), title('Original Image');
    subplot(1, 2, 2), imshow(adjusted_img), title('Adjusted Saturation');

    python代码如下:

  • import cv2
    import numpy as np
    
    # 读取图像
    image = cv2.imread('path_to_image.jpg')
    
    # 转换到 HSV 色彩空间
    hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    
    # 分离通道
    h, s, v = cv2.split(hsv_image)
    
    # 调整饱和度
    saturation_factor = 1.5  # 调整因子,大于1增加饱和度,小于1降低饱和度
    s = np.clip(s * saturation_factor, 0, 255).astype(np.uint8)
    
    # 合并通道并转换回 BGR
    adjusted_image = cv2.cvtColor(cv2.merge([h, s, v]), cv2.COLOR_HSV2BGR)
    
    # 显示结果
    cv2.imshow('Original Image', image)
    cv2.imshow('Adjusted Saturation', adjusted_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    
    
    
    ###色调调整
    # 调整色调
    hue_shift = 30  # 色调偏移量,正数向红色偏移,负数向蓝色偏移
    h = (h + hue_shift) % 180
    
    # 合并通道并转换回 BGR
    adjusted_image = cv2.cvtColor(cv2.merge([h, s, v]), cv2.COLOR_HSV2BGR)
    # 显示结果
    cv2.imshow('Original Image', image)
    cv2.imshow('Adjusted Hue', adjusted_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

2)白平衡算法

通过分析图像中的颜色偏差来进行白平衡调整。白平衡的调整目的是使图像中白色物体看起来不受颜色偏差的影响,从而提高图像的视觉效果。

这个博主列出来了五种白平衡的算法。

基于图像分析的偏色检测及颜色校正方法:

def white_balance(img):
    '''基于图像分析的偏色检测及颜色校正方法'''
    def detection(img):
        '''计算偏色值'''
        img_lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
        l, a, b = cv2.split(img_lab)
        d_a, d_b, M_a, M_b = 0, 0, 0, 0
        for i in range(m):
            for j in range(n):
                d_a = d_a + a[i][j]
                d_b = d_b + b[i][j]
        d_a, d_b = (d_a / (m * n)) - 128, (d_b / (n * m)) - 128
        D = np.sqrt((np.square(d_a) + np.square(d_b)))

        for i in range(m):
            for j in range(n):
                M_a = np.abs(a[i][j] - d_a - 128) + M_a
                M_b = np.abs(b[i][j] - d_b - 128) + M_b

        M_a, M_b = M_a / (m * n), M_b / (m * n)
        M = np.sqrt((np.square(M_a) + np.square(M_b)))
        k = D / M
        print('偏色值:%f' % k)
        return

    b, g, r = cv2.split(img)
    # print(img.shape)
    m, n = b.shape
    # detection(img)

    I_r_2 = np.zeros(r.shape)
    I_b_2 = np.zeros(b.shape)
    sum_I_r_2, sum_I_r, sum_I_b_2, sum_I_b, sum_I_g = 0, 0, 0, 0, 0
    max_I_r_2, max_I_r, max_I_b_2, max_I_b, max_I_g = int(r[0][0] ** 2), int(r[0][0]), int(b[0][0] ** 2), int(
        b[0][0]), int(g[0][0])
    for i in range(m):
        for j in range(n):
            I_r_2[i][j] = int(r[i][j] ** 2)
            I_b_2[i][j] = int(b[i][j] ** 2)
            sum_I_r_2 = I_r_2[i][j] + sum_I_r_2
            sum_I_b_2 = I_b_2[i][j] + sum_I_b_2
            sum_I_g = g[i][j] + sum_I_g
            sum_I_r = r[i][j] + sum_I_r
            sum_I_b = b[i][j] + sum_I_b
            if max_I_r < r[i][j]:
                max_I_r = r[i][j]
            if max_I_r_2 < I_r_2[i][j]:
                max_I_r_2 = I_r_2[i][j]
            if max_I_g < g[i][j]:
                max_I_g = g[i][j]
            if max_I_b_2 < I_b_2[i][j]:
                max_I_b_2 = I_b_2[i][j]
            if max_I_b < b[i][j]:
                max_I_b = b[i][j]
    # 线性变换
    [u_b, v_b] = np.matmul(np.linalg.inv([[sum_I_b_2, sum_I_b], [max_I_b_2, max_I_b]]), [sum_I_g, max_I_g])
    [u_r, v_r] = np.matmul(np.linalg.inv([[sum_I_r_2, sum_I_r], [max_I_r_2, max_I_r]]), [sum_I_g, max_I_g])
    # print(u_b, v_b, u_r, v_r)
    b0, g0, r0 = np.zeros(b.shape, np.uint8), np.zeros(g.shape, np.uint8), np.zeros(r.shape, np.uint8)
    # 颜色调整
    for i in range(m):
        for j in range(n):
            b_point = u_b * (b[i][j] ** 2) + v_b * b[i][j]
            g0[i][j] = g[i][j]
            # r0[i][j] = r[i][j]
            r_point = u_r * (r[i][j] ** 2) + v_r * r[i][j]
            # 限制颜色范围
            if r_point > 255:
                r0[i][j] = 255
            else:
                if r_point < 0:
                    r0[i][j] = 0
                else:
                    r0[i][j] = r_point
            if b_point > 255:
                b0[i][j] = 255
            else:
                if b_point < 0:
                    b0[i][j] = 0
                else:
                    b0[i][j] = b_point
    return cv2.merge([b0, g0, r0])

灰度世界法+完美反射法:

def combined_white_balance(img, bright_threshold=0.95, weight_gray=0.5, blue_suppress_factor=0.8):
    """
    增强版白平衡算法,针对蓝色偏色进一步优化
    :param img: 输入图像 (BGR格式)
    :param bright_threshold: 高光阈值 (0-1)
    :param weight_gray: 灰度世界法权重 (0-1)
    :param blue_suppress_factor: 初始蓝色压制因子 (0-1)
    :return: 白平衡后的图像
    """
    # 分离B、G、R通道
    b, g, r = cv2.split(img)

    # 计算各通道均值
    b_avg = np.mean(b)
    g_avg = np.mean(g)
    r_avg = np.mean(r)

    # 自适应蓝色压制因子:如果蓝色均值远高于其他通道,加强压制
    avg_non_blue = (g_avg + r_avg) / 2
    if b_avg > avg_non_blue:
        dynamic_blue_factor = min(blue_suppress_factor * (avg_non_blue / (b_avg + 1e-6)), 1.0)
    else:
        dynamic_blue_factor = blue_suppress_factor

    # --- 灰度世界法 ---
    gray_value = max(g_avg, r_avg)  # 以绿色或红色最大值为参考

    k_b_gray = gray_value / (b_avg + 1e-6)
    k_g_gray = gray_value / (g_avg + 1e-6)
    k_r_gray = gray_value / (r_avg + 1e-6)

    # --- 完美反射法 ---
    brightness = 0.11 * b.astype(float) + 0.59 * g.astype(float) + 0.3 * r.astype(float)
    threshold_value = np.percentile(brightness, bright_threshold * 100)

    mask = brightness >= threshold_value
    b_bright = b[mask]
    g_bright = g[mask]
    r_bright = r[mask]

    if len(b_bright) == 0:
        b_bright, g_bright, r_bright = b, g, r

    b_avg_bright = np.mean(b_bright)
    g_avg_bright = np.mean(g_bright)
    r_avg_bright = np.mean(r_bright)
    max_value = 255  # 直接以最大值255作为参考,强制校正到中性

    k_b_bright = max_value / (b_avg_bright + 1e-6)
    k_g_bright = max_value / (g_avg_bright + 1e-6)
    k_r_bright = max_value / (r_avg_bright + 1e-6)

    # --- 融合增益系数 ---
    weight_bright = 1.0 - weight_gray
    k_b = (k_b_gray * weight_gray + k_b_bright * weight_bright) * dynamic_blue_factor
    k_g = k_g_gray * weight_gray + k_g_bright * weight_bright
    k_r = k_r_gray * weight_gray + k_r_bright * weight_bright

    # 应用增益调整
    b_balanced = np.clip(b * k_b, 0, 255).astype(np.uint8)
    g_balanced = np.clip(g * k_g, 0, 255).astype(np.uint8)
    r_balanced = np.clip(r * k_r, 0, 255).astype(np.uint8)

    # 后处理:轻微降低蓝色通道(可选)
    b_balanced = np.clip(b_balanced * 0.95, 0, 255).astype(np.uint8)

    # 合并通道
    img_balanced = cv2.merge([b_balanced, g_balanced, r_balanced])
    return img_balanced

2)频域图像增强处理方法

  • 低通滤波:去除高频噪声,平滑图像。

  • 高通滤波:增强图像的边缘和细节。

  • 带通滤波:仅保留特定频率范围内的信号,适用于特定的图像处理任务。

  • 锐化:通过增强高频成分来突出图像的细节。

  • 频域锐化
    
    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    
    # 读取图像
    image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)
    
    # 傅里叶变换
    f_transform = np.fft.fft2(image)
    f_transform_shifted = np.fft.fftshift(f_transform)
    
    # 创建锐化核
    rows, cols = image.shape
    crow, ccol = rows // 2, cols // 2
    kernel = np.zeros((rows, cols), np.float32)
    kernel[crow-1:crow+2, ccol-1:ccol+2] = [[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]
    
    # 应用锐化核
    f_transform_shifted_filtered = f_transform_shifted * kernel
    
    # 逆傅里叶变换
    f_ishift = np.fft.ifftshift(f_transform_shifted_filtered)
    img_back = np.fft.ifft2(f_ishift)
    img_back = np.abs(img_back)
    

3、深度学习的图像增强算法:

怎么说,这有是网络环节了,蜘蛛网!!!目前接触到的:压缩图像的超分辨率、清晰度优化、低光照增强、去噪

超分辨率:第一篇有哦。

低光照:

低光照图像增强论文算法合集:前人的肩膀太好站了!谢谢他们!https://github.com/zhihongz/awesome-low-light-image-enhancement

去模糊:

GitHub - KupynOrest/DeblurGAN: Image Deblurring using Generative Adversarial Networks需要自己训练,无预训练模型。

去模糊-有预训练模型有预训练模型。

去噪:

下面这个链接完美总结各种去噪算法,包括传统深度都有。GitHub - wenbihan/reproducible-image-denoising-state-of-the-art: Collection of popular and reproducible image denoising works.


还有一个去雾的:

https://github.com/zhilin007/FFA-Net

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值