基于MATLAB的噪声图像处理方案

基于MATLAB的噪声图像处理方案,包含噪声方差估计、Gamma校正和白平衡的实现代码及原理详解:


一、系统架构设计


二、核心算法实现

1. 噪声方差估计(改进中值滤波法)

function sigma2 = estimate_noise_var(img, block_size=8, texture_threshold=0.1)
    % 分块计算局部方差
    [h,w] = size(img);
    variances = zeros(floor(h/block_size), floor(w/block_size));
    
    for i = 1:block_size:h
        for j = 1:block_size:w
            % 提取局部块
            patch = img(max(1,i-block_size/2):min(h,i+block_size/2), ...
                        max(1,j-block_size/2):min(w,j+block_size/2));
            local_var = var(patch(:));
            
            % 纹理强度计算
            grad_x = imfilter(patch, fspecial('sobel')/8, 'replicate');
            grad_y = imfilter(patch, fspecial('sobel')'/8, 'replicate');
            texture = mean(grad_x(:).^2 + grad_y(:).^2);
            
            % 阈值筛选
            if texture < texture_threshold
                variances(ceil(i/block_size), ceil(j/block_size)) = local_var;
            end
        end
    end
    
    % 中值滤波估计全局噪声
    sigma2 = median(variances(:)) * 2; % 考虑泊松噪声成分
end

2. Gamma校正(自适应查找表法)

function img_gamma = adaptive_gamma_correction(img, target_contrast=0.35)
    % 直方图均衡化预处理
    img_eq = imadjust(img);
    
    % Gamma值自适应计算
    [counts, binLocations] = imhist(img_eq);
    cdf = cumsum(counts)/numel(img_eq);
    
    % 寻找目标对比度对应的Gamma值
    [~, idx] = min(abs(cdf - target_contrast));
    optimal_gamma = binLocations(idx)/255;
    
    % 构建Gamma校正LUT
    lut = zeros(256,1);
    for i = 1:256
        lut(i) = 255 * (i/255)^optimal_gamma;
    end
    
    % 应用校正
    img_gamma = imadjust(img, lut, []);
end

3. 白平衡算法(双模式实现)

function balanced_img = white_balance(img, method='grayworld')
    img = im2double(img);
    
    switch method
        case 'grayworld'
            % 灰度世界算法
            avg_r = mean(img(:,:,1)(:));
            avg_g = mean(img(:,:,2)(:));
            avg_b = mean(img(:,:,3)(:));
            scale = avg_g / [avg_r, 1, avg_b];
            balanced_img = bsxfun(@times, img, scale);
            
        case 'whitepatch'
            % 白点估计算法
            [rows, cols, ~] = size(img);
            sum_rgb = sum(img, 3);
            threshold = prctile(sum_rgb(:), 95);
            white_pixels = sum_rgb > threshold;
            
            avg_r = mean(img(:,:,1)(white_pixels));
            avg_g = mean(img(:,:,2)(white_pixels));
            avg_b = mean(img(:,:,3)(white_pixels));
            scale = max([avg_r, avg_g, avg_b]) / [avg_r, avg_g, avg_b];
            balanced_img = bsxfun(@times, img, scale);
        otherwise
            error('Unknown method');
    end
    
    % 色彩范围限制
    balanced_img = max(0, min(1, balanced_img));
end

三、完整处理流程

%% 读取噪声图像
img = imread('noisy_image.jpg');
img = im2double(img);

%% 步骤1:噪声方差估计
sigma2 = estimate_noise_var(img);
fprintf('Estimated noise variance: %.4f\n', sigma2);

%% 步骤2:Gamma校正
gamma_img = adaptive_gamma_correction(img, 0.4);

%% 步骤3:白平衡处理
balanced_img = white_balance(gamma_img, 'whitepatch');

%% 结果可视化
figure;
subplot(2,2,1); imshow(img); title('原始图像');
subplot(2,2,2); imshow(gamma_img); title('Gamma校正');
subplot(2,2,3); imshow(balanced_img); title('白平衡处理');
subplot(2,2,4); imshow(img); title('原图对比');

%% 量化评估
psnr_before = psnr(img, balanced_img);
ssim_before = ssim(balanced_img, img);
disp(['PSNR: ', num2str(psnr_before), ' dB']);
disp(['SSIM: ', num2str(ssim_before)]);

四、参数说明

参数 作用范围 推荐值 调优方法
block_size 噪声估计 8-16 基于图像分辨率动态调整
texture_threshold 纹理筛选 0.05-0.2 直方图分析确定最佳阈值
target_contrast Gamma校正 0.3-0.6 峰值信噪比(PSNR)最大化准则
method 白平衡 grayworld/whitepatch 场景光照判断

参考代码 基于噪声图像的gamma校正,噪声方差估计,白平衡等 www.youwenfan.com/contentcnk/78981.html

该方法通过噪声估计指导Gamma校正参数选择,结合改进的白平衡算法,在保持色彩准确性的同时提升图像质量。实际应用中建议根据具体场景调整参数,并配合深度学习模型进行后处理优化。

posted @ 2025-11-04 09:30  yijg9998  阅读(7)  评论(0)    收藏  举报