Pengolahan Histogram: Equalization, Local Enhancement, dan Histogram Statistics
Dokumentasi tahapan pengolahan histogram dan peningkatan kontras lokal dengan window 3x3 menggunakan MATLAB.
1. Kode MATLAB
% =========================================================================
% SOAL 2: PENGOLAHAN HISTOGRAM & OPERASI ARITMATIKA
% a. Histogram Equalization
% b. Local Enhancement (Window 3x3)
% c. Histogram Statistics (E=2, k0=0.4, k1=0.02, k2=0.4, Window 3x3)
% =========================================================================
img = imread('cameraman.tif');
if size(img, 3) == 3
img = rgb2gray(img);
end
[rows, cols] = size(img);
img_double = double(img);
% -------------------------------------------------------------------------
% A. HISTOGRAM EQUALIZATION (Manual Step-by-Step)
% -------------------------------------------------------------------------
% Hitung frekuensi tiap derajat keabuan (0-255)
hist_counts = zeros(1, 256);
for i = 1:rows
for j = 1:cols
val = img(i,j) + 1;
hist_counts(val) = hist_counts(val) + 1;
end
end
% Hitung PDF dan CDF
pdf = hist_counts / (rows * cols);
cdf = cumsum(pdf);
% Pemetaan s_k = round(255 * CDF)
sk = round(255 * cdf);
% Terapkan fungsi pemetaan ke citra
img_histeq = zeros(rows, cols, 'uint8');
for i = 1:rows
for j = 1:cols
img_histeq(i,j) = uint8(sk(img(i,j) + 1));
end
end
% -------------------------------------------------------------------------
% B. LOCAL ENHANCEMENT (Window 3x3)
% -------------------------------------------------------------------------
img_padded = padarray(img_double, [1 1], 'replicate');
img_local_enh = zeros(rows, cols);
for i = 2:rows+1
for j = 2:cols+1
% Ekstrak sub-citra tetangga 3x3
sub_img = img_padded(i-1:i+1, j-1:j+1);
center_pixel = sub_img(2,2);
% Hitung CDF lokal 3x3
local_pdf = histcounts(sub_img, 0:256) / 9;
local_cdf = cumsum(local_pdf);
% Petakan nilai piksel pusat
img_local_enh(i-1, j-1) = round(255 * local_cdf(uint8(center_pixel) + 1));
end
end
img_local_enh = uint8(img_local_enh);
% -------------------------------------------------------------------------
% C. HISTOGRAM STATISTICS
% Param: E = 2, k0 = 0.4, k1 = 0.02, k2 = 0.4, Window 3x3
% Syarat: Jika m_Sxy <= k0*m_G DAN k1*s_G <= s_Sxy <= k2*s_G, g(x,y) = E*f(x,y)
% -------------------------------------------------------------------------
E = 2; k0 = 0.4; k1 = 0.02; k2 = 0.4;
% Mean & Standar Deviasi Global
m_G = mean(img_double(:));
s_G = std(img_double(:));
img_stat = img_double;
for i = 2:rows+1
for j = 2:cols+1
% Ekstrak window 3x3
sub_img = img_padded(i-1:i+1, j-1:j+1);
% Mean & Standar Deviasi Lokal
m_Sxy = mean(sub_img(:));
s_Sxy = std(sub_img(:));
% Evaluasi kondisi penguatan
if (m_Sxy <= k0 * m_G) && (s_Sxy >= k1 * s_G) && (s_Sxy <= k2 * s_G)
img_stat(i-1, j-1) = E * img_padded(i, j);
end
end
end
% Boundary clipping [0, 255]
img_stat = min(max(img_stat, 0), 255);
img_hist_stat = uint8(img_stat);
% -------------------------------------------------------------------------
% VISUALISASI HASIL
% -------------------------------------------------------------------------
figure;
subplot(2,2,1); imshow(img); title('Citra Asli');
subplot(2,2,2); imshow(img_histeq); title('Histogram Equalization');
subplot(2,2,3); imshow(img_local_enh); title('Local Enhancement (3x3)');
subplot(2,2,4); imshow(img_hist_stat); title('Histogram Statistics');