% MSRCR Method For Image Processing % Test for Digital Image Processing % By Gu Jinjin 2012/10/16-17 % Refer to Retinex processing for automatic image enhancemen % And thank videoandimage08's help for sharing me some Info % ------ clc;clear all; % 读取图像灰度值 I = imread('test.jpg'); IR = I(:,:,1); IG = I(:,:,2); IB = I(:,:,3); % 转换为 double 类型 IR_double = double(IR); IG_double = double(IG); IB_double = double(IB); % Set Gaussian Parameters sigma1 = 5; sigma2 = 20; sigma3 = 240; % 矩阵化网格向量 [x,y] = meshgrid((-(size(IR,2)-1)/2):(size(IR,2)/2),(-(size(IR,1)-1)/2):(size(IR,1)/2)); % 计算高斯环境函数 Fk gauss1 = exp(-(x.^2+y.^2)/(sigma1*sigma1)); % 归格化 Fk = k*Fk Gauss1 = gauss1/sum(gauss1(:)); gauss2 = exp(-(x.^2+y.^2)/(sigma2*sigma2)); Gauss2 = gauss2/sum(gauss2(:)); gauss3 = exp(-(x.^2+y.^2)/(sigma3*sigma3)); Gauss3 = gauss3/sum(gauss3(:)); % 计算MSR,MSRCR for i = 1:3 % MSR Part if i == 1 Ii = IR; Ii_double = IR_double; elseif i == 2 Ii = IG; Ii_double = IG_double; else Ii = IB; Ii_double = IB_double; end % 将图像转换到对数域 Ii_log = log(Ii_double+1); % 对图像傅里叶变换,转换到频域 f_Ii = fft2(Ii_double); fgauss = fft2(Gauss1,size(Ii,1),size(Ii,2)); fgauss = fftshift(fgauss); % 卷积 FI1 = ifft2(fgauss.*f_Ii); minv = min(min(FI1)); FI1_log = log(FI1-minv+1); % 计算R, MSR公式 R1 = Ii_log - FI1_log; fgauss = fft2(Gauss2,size(Ii,1),size(Ii,2)); fgauss = fftshift(fgauss); % 卷积 FI2 = ifft2(fgauss.*f_Ii); minv = min(min(FI2)); FI2_log = log(FI2-minv+1); R2 = Ii_log - FI2_log; fgauss = fft2(Gauss3,size(Ii,1),size(Ii,2)); fgauss = fftshift(fgauss); % 卷积 FI3 = ifft2(fgauss.*f_Ii); minv = min(min(FI3)); FI3_log = log(FI3-minv+1); R3 = Ii_log - FI3_log; % 加权求和得到 MSR R(x1,x2) Wk = 1/3; R = Wk*R1 + Wk*R2 + Wk*R3; % 计算 alpha, 取f() = log %N = 3; %alpha = log(N*Ii_double)-log(IR_double+IG_double+IB_double); %CR = alpha.*R; % MSRCR % 设定参数 G = 192; b = -30; alpha = 125; beta = 46; CR = beta*(log(alpha*Ii_double+1))-log(IR_double+IG_double+IB_double+1); CR=G*CR.*R+b; % 转化为灰度值 R = mat2gray(abs(R)); CR = mat2gray(abs(CR)); % figure,imshow(Rr); if i == 1 MSRr = R; MSRCRr = CR; elseif i == 2 MSRg = R; MSRCRg = CR; else MSRb = R; MSRCRb = CR; end end % imshow % 将三通道图像拼接 IMSR = cat(3,MSRr,MSRg,MSRb); IMSRCR = cat(3,MSRCRr,MSRCRg,MSRCRb); % 显示原始图像 figure,imshow(I); % MSR处理后图像 figure,imshow(IMSR); % MSRCR处理后后图像 figure,imshow(IMSRCR);