% dip06_6.m           gr18mar15  25oct17
% alpha-rooting (w. power scaling)
clear all;

x = 0:1:255;      % alpha rooting: plot of the mapping
for  alpha=.5:.1:1; 
    y = x .^ alpha;
    plot(x,y); hold on; grid on; axis tight;
end;
hold off;
 return;

%%  alpha rooting
 a = imread('.\img\pentagon.tif');    a = a(1:2:end,1:2:end);
% a = imread('.\img\lena.tif');
% a = imread('.\img\pepper.tif');

[nr,nc] = size(a);
a = double(a);
A = fft2(a);
Am = abs(A);  Ap = angle(A);
% imshow(log(fftshift(Am)),[]); return;

mna = mean(a(:));
pwra = mean(a(:).^2);
L = 5;
alpha = .99:-.02:.85;  % -.02
q = zeros(size(alpha));
for k = 1:length(alpha);
    Bm = Am .^ alpha(k);
    B = Bm .* exp(j*Ap);
    b = real(ifft2(B));
    pwrb = mean(b(:).^2);
    b = round(sqrt(pwra/pwrb)*b);   % note rounding
%    mnb = mean(b(:));    b = (b-mnb+mna);   % if desired
    figure(1);  imshow([a b],[0 255]); % note we clip b<0, b>255
    N = max(b(:))-min(b(:))+1;  % to compute the exact histogram (N bins)
    figure(2); [h,x]=hist(b(:),N); plot(x,h); axis([-100 350 0 4000]);
    pause()
end

