1. 統計直方圖,對於RGB圖像,三通道聯合統計
2. 採用累積直方圖映射
function outmat = ToneEqualization(inmat, bits)
% PhotoShop色調均化
% bits = 16;
% inmat = imgdata;
% imshow(uint8(inmat./65535.*255));
[Height, Width, plane] = size(inmat);
nbins = uint16(2^bits);
in1 = inmat(:,:,1);
in2 = inmat(:,:,2);
in3 = inmat(:,:,3);
all_in = [in1 in2 in3];
all_hist = zeros(nbins,1);
for i=1:Height
for j=1:Width*3
all_hist(uint16(all_in(i,j))) = all_hist(uint16(all_in(i,j))) + 1;
end
end
% figure;plot(all_hist);
%all_hist = imhist(uint16(all_in(:)),nbins);
% 計算映射表,累積直方圖
Num = 0;
for y=1:1:nbins
Num = Num + all_hist(y);
Lut(y) = uint32(Num / (Width * Height * 3) * nbins);
end
outmat = zeros(Height, Width, plane);
for x=1:Height
for y=1:Width
outmat(x,y,1) = Lut(uint16(in1(x,y)));
outmat(x,y,2) = Lut(uint16(in2(x,y)));
outmat(x,y,3) = Lut(uint16(in3(x,y)));
end
end
% imshow(uint8(outmat./65535.*255));
end
原始圖 色調均化後