然而面對一張過暗、模糊或對比過小的圖片,等同於無法正確取得圖片內含的資訊。也因此,我們必須經過特定程序使得圖片內含的資訊得已重新顯現。
直方圖均衡化 (Histogram Equalization) 正是可以用來處理圖片局部過暗的方式之一;其藉由像素個數的累積特性 (單調遞增函數) 計算每一像素值出現的次數並以此將灰階直方圖重新進行調整,在調整過程中,像素個數之間的關係不會改變,只會將該個數重新分配到新的灰階值上,以增強圖片的對比度。
上圖左:原圖,其內容過於模糊,色調過於接近難以辨識。
上圖右:經由 Histogram Equalization 後重新輸出,其辨識度大大提升。
下圖左:原圖,其直方圖。
下圖右:經由 Histogram Equalization 後之直方圖。
這邊分別利用三種方法來實現這個效果:
《方法一》使用內建函式完成。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
close all,clear; | |
im = imread('$path'); | |
hei = zeros(256,1); | |
for level=0:255 | |
hei(level+1) = sum(sum(im==level)); | |
end | |
hist(hei); | |
set(gca,'XLim',[0 255]); | |
image(repmat(im,[1 1 3])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clear | |
close all | |
disp('Getting photo...'); | |
im = imread('$path'); | |
[x,y] = size(im); | |
figure,image(repmat(im,[1 1 3])), axis off | |
disp('Processing...'); | |
change = 0:1:255; | |
im_res = reshape(im, [], 1); | |
hist(im_res, change); | |
[n,bin] = hist(im_res, change); | |
total = cumsum(n); | |
plot(bin, total); | |
disp('Reproducing...'); | |
count_hist = round((total-min(double(im_res)))./sum(n)*255); | |
for level=1:1:256 | |
im_after(find(im_res==bin(level)))=count_hist(level); | |
end | |
im_hist_after = uint8(reshape(im_after, x, y)); | |
hist(im_after, change); | |
disp('Normalizing...'); | |
set(gca,'XLim',[0 255]); | |
%figure,image(repmat(im_hist_after,[1 1 3])), axis off |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clc | |
clear | |
close all | |
im=imread('$path'); | |
figure, colormap(gray(256)),imagesc(im, [0 255]) | |
[m,n]=size(im); | |
h=uint32(zeros(1,256)); | |
for i=1:m | |
for j=1:n | |
index=im(i,j)+1; | |
h(index)=h(index)+1; | |
end | |
end | |
LUT=uint32(zeros(1,256)); | |
amount=0; | |
for i=1:256 | |
amount=amount+h(i); | |
LUT(i)=amount; | |
end | |
LUT=uint8(LUT*255/(m*n)); | |
for i=1:m | |
for j=1:n | |
index=im(i,j)+1; | |
im(i,j)=uint8(LUT(index)); | |
end | |
end | |
figure, colormap(gray(256)), imagesc(im, [0 255]) |
有個陷阱,
你發現了嗎?
No comments:
Post a Comment