標題: MMBEBHE代碼及逐行講解 [打印本頁]

作者: aceamber    時間: 2024-6-24 01:16
標題: MMBEBHE代碼及逐行講解
input_image = imread('圖片地址.tif');
% Step 1 計算每個閾值級別的 AMBE
    hist = imhist(input_image);  % 計算直方圖
    total_pixels = numel(input_image);% 獲取圖像中的總像素數(shù)n
    ambe_values = zeros(256, 1); % 初始化數(shù)組以便儲存AMBE的值

    % 根據(jù)閾值將輸入的直方圖一分為二
        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

                % 按像素總數(shù)歸一化Pr(rk)=nk/n
                lower_cdf_normalized = lower_cdf / total_pixels;
                upper_cdf_normalized = upper_cdf / total_pixels;

                % 計算兩個區(qū)域的平均強度
                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 找到產(chǎn)生最小 MBE 的閾值水平 XT
[~, optimal_threshold] = min(ambe_values);
XT = optimal_threshold - 1;

% Step 3 根據(jù)找到的 XT將輸入直方圖一分為二,獨立均衡化
% 根據(jù)最佳閾值分割圖像
lower_region = input_image <= XT;
upper_region = input_image > XT;

% 兩個區(qū)域單獨直方圖均衡化
lower_eq = histeq(input_image(lower_region), 256);
upper_eq = histeq(input_image(upper_region), 256);

% 合并兩個均衡化區(qū)域得出最終結果
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.torrancerestoration.com/bbs/) Powered by Discuz! X3.1