基于 区域的空间域图像融合MATLAB实现

空间域图像融合是一种直接将图像像素进行处理的融合方法,基于区域的融合技术通过分析图像的不同区域特性来选择最合适的融合策略。

核心融合算法

function fused_image = region_based_fusion(image1, image2, method, varargin)
    % 基于区域的空间域图像融合
    % 输入参数:
    %   image1, image2 - 待融合的源图像(大小相同)
    %   method - 融合方法: 'pca', 'wavelet', 'gradient', 'svd'
    %   varargin - 方法特定参数
    
    % 检查输入图像
    if size(image1) ~= size(image2)
        error('输入图像大小必须相同');
    end
    
    % 转换为双精度浮点数以便计算
    if ~isfloat(image1)
        image1 = im2double(image1);
    end
    if ~isfloat(image2)
        image2 = im2double(image2);
    end
    
    % 根据选择的方法调用相应的融合函数
    switch lower(method)
        case 'pca'
            fused_image = pca_fusion(image1, image2, varargin{:});
        case 'wavelet'
            fused_image = wavelet_fusion(image1, image2, varargin{:});
        case 'gradient'
            fused_image = gradient_fusion(image1, image2, varargin{:});
        case 'svd'
            fused_image = svd_fusion(image1, image2, varargin{:});
        otherwise
            error('不支持的融合方法。可选择: pca, wavelet, gradient, svd');
    end
end

PCA(主成分分析)融合方法

function fused_image = pca_fusion(image1, image2, block_size)
    % 基于PCA的区域图像融合
    % 输入参数:
    %   image1, image2 - 输入图像
    %   block_size - 分块大小(默认8x8)
    
    if nargin < 3
        block_size = 8;
    end
    
    [rows, cols, channels] = size(image1);
    fused_image = zeros(rows, cols, channels);
    
    for ch = 1:channels
        % 对每个颜色通道分别处理
        img1_ch = image1(:, :, ch);
        img2_ch = image2(:, :, ch);
        
        % 图像分块
        blocks1 = im2col(img1_ch, [block_size block_size], 'distinct');
        blocks2 = im2col(img2_ch, [block_size block_size], 'distinct');
        
        % 初始化融合后的块
        fused_blocks = zeros(size(blocks1));
        
        % 对每个块进行PCA融合
        for i = 1:size(blocks1, 2)
            block1 = reshape(blocks1(:, i), block_size, block_size);
            block2 = reshape(blocks2(:, i), block_size, block_size);
            
            % 计算PCA系数
            [coeff1, score1, latent1] = pca(block1);
            [coeff2, score2, latent2] = pca(block2);
            
            % 根据方差大小选择主成分
            if sum(latent1) > sum(latent2)
                fused_block = score1 * coeff1';
            else
                fused_block = score2 * coeff2';
            end
            
            fused_blocks(:, i) = fused_block(:);
        end
        
        % 重组图像
        fused_image(:, :, ch) = col2im(fused_blocks, [block_size block_size], [rows cols], 'distinct');
    end
end

基于小波变换的融合方法

function fused_image = wavelet_fusion(image1, image2, wavelet_type, levels)
    % 基于小波变换的区域图像融合
    % 输入参数:
    %   image1, image2 - 输入图像
    %   wavelet_type - 小波类型(默认'db4')
    %   levels - 分解层数(默认3)
    
    if nargin < 3
        wavelet_type = 'db4';
    end
    if nargin < 4
        levels = 3;
    end
    
    [rows, cols, channels] = size(image1);
    fused_image = zeros(rows, cols, channels);
    
    for ch = 1:channels
        % 对每个颜色通道分别处理
        img1_ch = image1(:, :, ch);
        img2_ch = image2(:, :, ch);
        
        % 对图像1进行小波分解
        [cA1, cH1, cV1, cD1] = swt2(img1_ch, levels, wavelet_type);
        
        % 对图像2进行小波分解
        [cA2, cH2, cV2, cD2] = swt2(img2_ch, levels, wavelet_type);
        
        % 融合规则: 低频系数取平均,高频系数取绝对值最大
        fused_cA = (cA1 + cA2) / 2;
        
        % 初始化高频系数
        fused_cH = zeros(size(cH1));
        fused_cV = zeros(size(cV1));
        fused_cD = zeros(size(cD1));
        
        % 对每个分解层处理
        for l = 1:levels
            % 水平细节系数融合
            maskH = abs(cH1(:, :, l)) > abs(cH2(:, :, l));
            fused_cH(:, :, l) = maskH .* cH1(:, :, l) + ~maskH .* cH2(:, :, l);
            
            % 垂直细节系数融合
            maskV = abs(cV1(:, :, l)) > abs(cV2(:, :, l));
            fused_cV(:, :, l) = maskV .* cV1(:, :, l) + ~maskV .* cV2(:, :, l);
            
            % 对角细节系数融合
            maskD = abs(cD1(:, :, l)) > abs(cD2(:, :, l));
            fused_cD(:, :, l) = maskD .* cD1(:, :, l) + ~maskD .* cD2(:, :, l);
        end
        
        % 小波重构
        fused_image(:, :, ch) = iswt2(fused_cA, fused_cH, fused_cV, fused_cD, wavelet_type);
    end
end

基于梯度的融合方法

function fused_image = gradient_fusion(image1, image2, window_size, threshold)
    % 基于梯度的区域图像融合
    % 输入参数:
    %   image1, image2 - 输入图像
    %   window_size - 窗口大小(默认3x3)
    %   threshold - 梯度阈值(默认0.1)
    
    if nargin < 3
        window_size = 3;
    end
    if nargin < 4
        threshold = 0.1;
    end
    
    [rows, cols, channels] = size(image1);
    fused_image = zeros(rows, cols, channels);
    
    % 创建高斯权重窗口
    window = fspecial('gaussian', window_size, window_size/6);
    
    for ch = 1:channels
        % 对每个颜色通道分别处理
        img1_ch = image1(:, :, ch);
        img2_ch = image2(:, :, ch);
        
        % 计算梯度幅值
        [Gx1, Gy1] = gradient(img1_ch);
        gradient1 = sqrt(Gx1.^2 + Gy1.^2);
        
        [Gx2, Gy2] = gradient(img2_ch);
        gradient2 = sqrt(Gx2.^2 + Gy2.^2);
        
        % 使用窗口平滑梯度
        smooth_grad1 = conv2(gradient1, window, 'same');
        smooth_grad2 = conv2(gradient2, window, 'same');
        
        % 创建融合掩模
        mask = smooth_grad1 > smooth_grad2;
        
        % 应用阈值
        diff = abs(smooth_grad1 - smooth_grad2);
        uncertain_region = diff < threshold;
        
        % 不确定区域取平均
        fused_ch = zeros(rows, cols);
        fused_ch(mask & ~uncertain_region) = img1_ch(mask & ~uncertain_region);
        fused_ch(~mask & ~uncertain_region) = img2_ch(~mask & ~uncertain_region);
        fused_ch(uncertain_region) = (img1_ch(uncertain_region) + img2_ch(uncertain_region)) / 2;
        
        fused_image(:, :, ch) = fused_ch;
    end
end

基于SVD(奇异值分解)的融合方法

function fused_image = svd_fusion(image1, image2, block_size)
    % 基于SVD的区域图像融合
    % 输入参数:
    %   image1, image2 - 输入图像
    %   block_size - 分块大小(默认8x8)
    
    if nargin < 3
        block_size = 8;
    end
    
    [rows, cols, channels] = size(image1);
    fused_image = zeros(rows, cols, channels);
    
    for ch = 1:channels
        % 对每个颜色通道分别处理
        img1_ch = image1(:, :, ch);
        img2_ch = image2(:, :, ch);
        
        % 图像分块
        blocks1 = im2col(img1_ch, [block_size block_size], 'distinct');
        blocks2 = im2col(img2_ch, [block_size block_size], 'distinct');
        
        % 初始化融合后的块
        fused_blocks = zeros(size(blocks1));
        
        % 对每个块进行SVD融合
        for i = 1:size(blocks1, 2)
            block1 = reshape(blocks1(:, i), block_size, block_size);
            block2 = reshape(blocks2(:, i), block_size, block_size);
            
            % 计算SVD
            [U1, S1, V1] = svd(block1);
            [U2, S2, V2] = svd(block2);
            
            % 融合策略: 选择奇异值较大的矩阵
            if sum(diag(S1)) > sum(diag(S2))
                fused_block = U1 * S1 * V1';
            else
                fused_block = U2 * S2 * V2';
            end
            
            fused_blocks(:, i) = fused_block(:);
        end
        
        % 重组图像
        fused_image(:, :, ch) = col2im(fused_blocks, [block_size block_size], [rows cols], 'distinct');
    end
end

完整的图像融合演示脚本

% 基于区域的空间域图像融合演示
clear all;
close all;
clc;

% 读取源图像
img1 = imread('source1.jpg');
img2 = imread('source2.jpg');

% 转换为灰度图像(如果必要)
if size(img1, 3) == 3
    img1_gray = rgb2gray(img1);
else
    img1_gray = img1;
end

if size(img2, 3) == 3
    img2_gray = rgb2gray(img2);
else
    img2_gray = img2;
end

% 调整图像大小使其相同
if size(img1_gray) ~= size(img2_gray)
    img2_gray = imresize(img2_gray, size(img1_gray));
end

% 转换为双精度
img1_gray = im2double(img1_gray);
img2_gray = im2double(img2_gray);

% 应用不同的融合方法
fused_pca = region_based_fusion(img1_gray, img2_gray, 'pca', 8);
fused_wavelet = region_based_fusion(img1_gray, img2_gray, 'wavelet', 'db4', 3);
fused_gradient = region_based_fusion(img1_gray, img2_gray, 'gradient', 5, 0.1);
fused_svd = region_based_fusion(img1_gray, img2_gray, 'svd', 8);

% 显示结果
figure('Position', [100, 100, 1200, 800]);

subplot(2, 3, 1);
imshow(img1_gray);
title('源图像 1');

subplot(2, 3, 2);
imshow(img2_gray);
title('源图像 2');

subplot(2, 3, 3);
imshow(fused_pca);
title('PCA融合');

subplot(2, 3, 4);
imshow(fused_wavelet);
title('小波融合');

subplot(2, 3, 5);
imshow(fused_gradient);
title('梯度融合');

subplot(2, 3, 6);
imshow(fused_svd);
title('SVD融合');

% 计算融合质量指标
fprintf('=== 融合质量评估 ===\n');
fprintf('PCA融合 - 信息熵: %.4f, 平均梯度: %.4f, 空间频率: %.4f\n', ...
    image_entropy(fused_pca), avg_gradient(fused_pca), spatial_frequency(fused_pca));
fprintf('小波融合 - 信息熵: %.4f, 平均梯度: %.4f, 空间频率: %.4f\n', ...
    image_entropy(fused_wavelet), avg_gradient(fused_wavelet), spatial_frequency(fused_wavelet));
fprintf('梯度融合 - 信息熵: %.4f, 平均梯度: %.4f, 空间频率: %.4f\n', ...
    image_entropy(fused_gradient), avg_gradient(fused_gradient), spatial_frequency(fused_gradient));
fprintf('SVD融合 - 信息熵: %.4f, 平均梯度: %.4f, 空间频率: %.4f\n', ...
    image_entropy(fused_svd), avg_gradient(fused_svd), spatial_frequency(fused_svd));

% 信息熵计算函数
function entropy = image_entropy(img)
    [counts, ~] = imhist(img);
    probabilities = counts / sum(counts);
    probabilities = probabilities(probabilities > 0);
    entropy = -sum(probabilities .* log2(probabilities));
end

% 平均梯度计算函数
function avg_grad = avg_gradient(img)
    [Gx, Gy] = gradient(img);
    grad_magnitude = sqrt(Gx.^2 + Gy.^2);
    avg_grad = mean(grad_magnitude(:));
end

% 空间频率计算函数
function sf = spatial_frequency(img)
    [rows, cols] = size(img);
    
    % 行频率
    rf = sqrt(sum(diff(img, 1, 1).^2, 'all') / (rows * cols));
    
    % 列频率
    cf = sqrt(sum(diff(img, 1, 2).^2, 'all') / (rows * cols));
    
    sf = sqrt(rf^2 + cf^2);
end

高级功能:自适应区域分割融合

function fused_image = adaptive_region_fusion(image1, image2, segmentation_method)
    % 自适应区域分割融合
    % 输入参数:
    %   image1, image2 - 输入图像
    %   segmentation_method - 分割方法: 'kmeans', 'watershed', 'superpixel'
    
    if nargin < 3
        segmentation_method = 'superpixel';
    end
    
    [rows, cols, channels] = size(image1);
    fused_image = zeros(rows, cols, channels);
    
    % 对每个颜色通道分别处理
    for ch = 1:channels
        img1_ch = image1(:, :, ch);
        img2_ch = image2(:, :, ch);
        
        % 图像分割
        switch lower(segmentation_method)
            case 'kmeans'
                % K-means分割
                num_clusters = 5;
                features = [img1_ch(:), img2_ch(:), gradient(img1_ch)(:), gradient(img2_ch)(:)];
                [idx, ~] = kmeans(features, num_clusters);
                segmentation_mask = reshape(idx, rows, cols);
                
            case 'watershed'
                % 分水岭分割
                gradient_magnitude = imgradient(img1_ch);
                segmentation_mask = watershed(gradient_magnitude);
                
            case 'superpixel'
                % 超像素分割
                num_superpixels = 100;
                [L, ~] = superpixels(img1_ch, num_superpixels);
                segmentation_mask = L;
                
            otherwise
                error('不支持的分割方法');
        end
        
        % 对每个区域应用不同的融合策略
        unique_regions = unique(segmentation_mask);
        fused_ch = zeros(rows, cols);
        
        for i = 1:length(unique_regions)
            region_mask = segmentation_mask == unique_regions(i);
            
            % 计算区域特性
            region_pixels1 = img1_ch(region_mask);
            region_pixels2 = img2_ch(region_mask);
            
            region_var1 = var(region_pixels1);
            region_var2 = var(region_pixels2);
            
            region_energy1 = sum(region_pixels1.^2);
            region_energy2 = sum(region_pixels2.^2);
            
            % 根据区域特性选择融合策略
            if region_var1 > region_var2 && region_energy1 > region_energy2
                % 区域1更活跃,使用图像1
                fused_ch(region_mask) = region_pixels1;
            elseif region_var2 > region_var1 && region_energy2 > region_energy1
                % 区域2更活跃,使用图像2
                fused_ch(region_mask) = region_pixels2;
            else
                % 活跃度相似,取平均
                fused_ch(region_mask) = (region_pixels1 + region_pixels2) / 2;
            end
        end
        
        fused_image(:, :, ch) = fused_ch;
    end
end

使用示例

% 使用自适应区域分割融合
fused_adaptive = adaptive_region_fusion(img1_gray, img2_gray, 'superpixel');

% 显示结果
figure;
subplot(2, 2, 1);
imshow(img1_gray);
title('源图像 1');

subplot(2, 2, 2);
imshow(img2_gray);
title('源图像 2');

subplot(2, 2, 3);
imshow(fused_adaptive);
title('自适应区域融合');

% 计算分割区域的可视化
[L, N] = superpixels(img1_gray, 100);
boundary_mask = boundarymask(L);
subplot(2, 2, 4);
imshow(imoverlay(img1_gray, boundary_mask, 'cyan'));
title('超像素分割边界');

% 评估融合质量
fprintf('自适应区域融合 - 信息熵: %.4f, 平均梯度: %.4f, 空间频率: %.4f\n', ...
    image_entropy(fused_adaptive), avg_gradient(fused_adaptive), spatial_frequency(fused_adaptive));

多模态图像融合示例

% 多模态图像融合示例
% 假设我们有一个红外图像和一个可见光图像

% 读取多模态图像
ir_image = imread('infrared.jpg');
visible_image = imread('visible.jpg');

% 转换为灰度图像并调整大小
ir_gray = im2double(rgb2gray(ir_image));
visible_gray = im2double(rgb2gray(visible_image));

if size(ir_gray) ~= size(visible_gray)
    visible_gray = imresize(visible_gray, size(ir_gray));
end

% 应用不同的融合方法
fused_pca = region_based_fusion(ir_gray, visible_gray, 'pca', 8);
fused_wavelet = region_based_fusion(ir_gray, visible_gray, 'wavelet', 'db4', 3);
fused_adaptive = adaptive_region_fusion(ir_gray, visible_gray, 'superpixel');

% 显示结果
figure('Position', [100, 100, 1200, 600]);

subplot(2, 3, 1);
imshow(ir_gray);
title('红外图像');

subplot(2, 3, 2);
imshow(visible_gray);
title('可见光图像');

subplot(2, 3, 3);
imshow(fused_pca);
title('PCA融合');

subplot(2, 3, 4);
imshow(fused_wavelet);
title('小波融合');

subplot(2, 3, 5);
imshow(fused_adaptive);
title('自适应区域融合');

% 添加颜色映射以增强可视化
subplot(2, 3, 6);
imshow(fused_adaptive, 'Colormap', jet(256));
title('融合结果(彩色映射)');

% 计算多模态融合的质量指标
fprintf('=== 多模态融合质量评估 ===\n');
fprintf('PCA融合 - 信息熵: %.4f, 平均梯度: %.4f, 空间频率: %.4f\n', ...
    image_entropy(fused_pca), avg_gradient(fused_pca), spatial_frequency(fused_pca));
fprintf('小波融合 - 信息熵: %.4f, 平均梯度: %.4f, 空间频率: %.4f\n', ...
    image_entropy(fused_wavelet), avg_gradient(fused_wavelet), spatial_frequency(fused_wavelet));
fprintf('自适应区域融合 - 信息熵: %.4f, 平均梯度: %.4f, 空间频率: %.4f\n', ...
    image_entropy(fused_adaptive), avg_gradient(fused_adaptive), spatial_frequency(fused_adaptive));

参考代码 基于区域的空间域图像融合 www.3dddown.com/csa/64336.html

注意

  1. 输入图像应该已经配准,即对应像素表示同一场景位置
  2. 对于彩色图像,可以分别对每个颜色通道进行融合,或者先转换为其他颜色空间(如YUV)再融合
  3. 不同的融合方法适用于不同类型的图像和应用场景
  4. 参数(如分块大小、小波类型、分割数量等)可以根据具体图像进行调整优化
  5. 融合质量评估指标可以帮助选择最适合特定应用的融合方法
posted @ 2025-12-16 11:58  荒川之主  阅读(3)  评论(0)    收藏  举报