2010年10月13日星期三

Retinex Source Code

以下为Styx的Retinex图像增强的源代码,写得粗糙了些,并且没有注释,不过看过Retinex论文的人应该可以看懂。包括两个,一个是single scale的,一个是multi-scales的。如果单独使用single scale的,就把其中的注释去掉,可以显示结果了。如要使用multi-scales的,因为它要调用single scale的函数,所以就single scale函数中的一部分就要加上注释,以供其用。

Retinex Single Scale Enhancing : Source Code(by Matlab)
function R = Retinex(I, scale, weight)
%RETINEX enhance the image
%I = double(imread(filename)) + 1;

[width, height, depth] = size(I);

FInverse = 0;
for i = 1 : width
for j = 1 : height
%Fk(i, j) = exp(-((i-width/2).^2 + (j-height/2).^2)/scale.^2);
Fk(i,j) = exp((-i.^2 - j.^2)/scale.^2);
FInverse = FInverse + Fk(i,j);
end
end

coff = 1 / FInverse;
Fk = Fk * coff;

Ift = zeros(size(I));

Ift = fft2(I);
Fkft = fft2(Fk);

tmpSumft = zeros([width, height, depth]);
for k = 1 : depth
tmpSumft(:, :, k) = Ift(:, :, k) .* Fkft;
end
tmpSum = ifft2(abs(tmpSumft));

R = weight * (log(I) - log(abs(tmpSum)));

%figure(2);
%subplot(2, 1, 1);
%imshow(uint8(I));
%subplot(2, 1, 2);
%maxp = max(R(:));
%minp = min(R(:));
%step = 255/(maxp - minp);
%imshow(uint8((R-minp)*step-1));

Retinex Multi-Scales Enhancing : Source Code(by Matlab)
function Output = RetinexMS(filename, scale, weight)
%RETINEXMS multiscale retinex method to enhance the image
OriginI = double(imread(filename)) + 1;
Output = zeros(size(OriginI));
for i = 1 : size(scale, 2)
Output = Output + Retinex(OriginI, scale(i), weight(i));
end

figure(2)
subplot(2,1,1);
imshow(uint8(OriginI - 1));
subplot(2,1,2);
maxp = max(Output(:));
minp = min(Output(:));
step = 255/(maxp - minp);
Output = uint8((Output-minp)*step - 1);
imshow(Output);


这里还有个问题,就是显示的问题,我直接将图像处理后的结果(都是些浮点数,大概在正负个位数之间)线性扩展到了0~255之
间,显示出来的图像虽然亮度增大,阴影部分清楚,但是过于偏白偏亮,实现过的人觉得这有什么错误吗?欢迎任何建议!

没有评论: