标题: MMBEBHE代码及逐行讲解 [打印本页]

作者: aceamber    时间: 2024-6-24 01:16
标题: MMBEBHE代码及逐行讲解
input_image = imread('图片地址.tif');
% Step 1 计算每个阈值级别的 AMBE
    hist = imhist(input_image);  % 计算直方图
    total_pixels = numel(input_image);% 获取图像中的总像素数n
    ambe_values = zeros(256, 1); % 初始化数组以便储存AMBE的值

    % 根据阈值将输入的直方图一分为二
        for threshold =[]
            lower_hist = hist(1:threshold + 1);
            upper_hist = hist(threshold + 2:end);

     % 检查是否有任何直方图为空      
            if isempty(lower_hist) || isempty(upper_hist)
                ambe_values(threshold + 1) = inf; %将AMBE设置为无限大
            else
                lower_cdf = cumsum(lower_hist);
                upper_cdf = cumsum(upper_hist);% 计算nk

                % 按像素总数归一化Pr(rk)=nk/n
                lower_cdf_normalized = lower_cdf / total_pixels;
                upper_cdf_normalized = upper_cdf / total_pixels;

                % 计算两个区域的平均强度
                mean_lower = sum((0:threshold) .* lower_hist) / sum(lower_hist);
                mean_upper = sum((threshold + 1:255) .* upper_hist) / sum(upper_hist);

                % 计算绝对平均亮度误差 (AMBE)
                ambe_values(threshold + 1) = abs(mean_upper - mean_lower);
            end
        end

% STEP 2 找到产生最小 MBE 的阈值水平 XT
[~, optimal_threshold] = min(ambe_values);
XT = optimal_threshold - 1;

% Step 3 根据找到的 XT将输入直方图一分为二,独立均衡化
% 根据最佳阈值分割图像
lower_region = input_image <= XT;
upper_region = input_image > XT;

% 两个区域单独直方图均衡化
lower_eq = histeq(input_image(lower_region), 256);
upper_eq = histeq(input_image(upper_region), 256);

% 合并两个均衡化区域得出最终结果
result_image = input_image;
result_image(lower_region) = lower_eq;
result_image(upper_region) = upper_eq;

% 显示最终图像&直方图
figure
imshow(input_image)
title('原图像');
figure
imshow(result_image)
title('图像MMBEBHE的结果');
figure
imhist(input_image);
title('原图像的直方图');
figure
imhist(result_image);
title('图像MMBEBHE的直方图')








欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1