function [alpha, beta] = Cal_alpha_beta(Rec)   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function calculates the appropriate alpha and beta for the proposed JM-CDM
% according to the initial demosaicked image
% 
% Author: Kan Chang, Guangxi University
% Date: 2/2/2015
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[Height,Width,~] = size(Rec); 
[D, ~, ~] = DiffOper_d(Height, Width);
Rec_temp = Rec(:);
n = Height*Width;
R_G = Rec_temp(1:n) - Rec_temp(n+1:2*n);
G_B = Rec_temp(n+1:2*n) - Rec_temp(2*n+1:3*n);
B_R = Rec_temp(2*n+1:3*n) - Rec_temp(1:n);
Ave_TV = mean(abs(D*R_G(:))) + mean(abs(D*G_B(:))) + mean(abs(D*B_R(:))); 
       
       
Tol_s = 0;
for ii = 1:Height
	for jj = 1:Width
        y = (Rec(ii,jj,1) + Rec(ii,jj,2) + Rec(ii,jj,3))/3.0;
        s = sqrt(((Rec(ii,jj,1)-y)^2 + (Rec(ii,jj,2)-y)^2 + (Rec(ii,jj,3)-y)^2)/3.0);
        Tol_s = Tol_s + s;
	end
end
Ave_s = Tol_s/(Height*Width);

if Ave_TV < 6.5
    if Ave_s < 21.0
        alpha = 0.005;
        beta = 0.08;
    elseif 21.0 <= Ave_s && Ave_s < 26.0
        alpha = 0.005;
        beta = 0.15;        
    elseif 26.0 <= Ave_s && Ave_s < 31.0
        alpha = 0.005;   
        beta = 0.22;
    elseif 31.0 <= Ave_s && Ave_s < 36.0
        alpha = 0.005;
        beta = 0.29;
    else
        alpha = 0.005;  
        beta = 0.36;
    end
elseif 6.5 <= Ave_TV && Ave_TV < 8.0
    if Ave_s < 26.0
        alpha = 0.005;
        beta = 0.15;   
    elseif 26.0 <= Ave_s && Ave_s < 31.0
        alpha = 0.005;   
        beta = 0.22;        
    elseif 31.0 <= Ave_s && Ave_s < 36.0
        alpha = 0.005;
        beta = 0.29;        
    else
        alpha = 0.005; 
        beta = 0.36;       
    end    
elseif 8.0 <= Ave_TV && Ave_TV < 10
    if Ave_s < 31.0
        alpha = 0.005;   
        beta = 0.22;         
    elseif 31.0 <= Ave_s && Ave_s < 36.0
        alpha = 0.005; 
        beta = 0.29;          
    else
        alpha = 0.005;  
        beta = 0.36;        
    end     
elseif 10.0 <= Ave_TV && Ave_TV < 12
    if Ave_s < 36.0
        alpha = 0.005;
        beta = 0.29;        
    else
        alpha = 0.005; 
        beta = 0.36;         
    end       
else
        alpha = 0.005;  
        beta = 0.36;   
end
 


       