matlab MSRCR算法代码
时间: 2023-08-06 15:24:41 浏览: 120
MSRCR.zip_MSRCR_msr_msrcr代码
以下是 Matlab 中 MSRCR 算法的代码:
```matlab
function [outImg, Illum] = MSRCR(inImg, scales, weights, gamma)
% MSRCR: Multi-Scale Retinex with Color Restoration
% Usage: [outImg, Illum] = MSRCR(inImg, scales, weights, gamma)
% Inputs:
% inImg: input color image (RGB)
% scales: scales of retinex processing
% weights: weights of each scale
% gamma: gamma correction
% Outputs:
% outImg: output color image (RGB)
% Illum: illumination map
if (nargin < 4)
gamma = 2.4;
end
if (nargin < 3)
weights = [0.5, 0.5, 0.5];
end
if (nargin < 2)
scales = [15, 80, 250];
end
inImg = double(inImg) + 1;
[nr, nc, cc] = size(inImg);
% log_luminance: log-average luminance
log_luminance = log(mean(mean(inImg)));
% initialization
Illum = zeros(nr, nc, cc);
outImg = zeros(nr, nc, cc);
% multi-scale retinex processing
for i = 1:length(scales)
sigma = sqrt(scales(i)^2 / log(2));
% gaussian filtering
blur = imgaussfilt(inImg, sigma);
% illumination map
Illum = Illum + weights(i) * log(inImg) - weights(i) * log(blur);
end
% normalization
Illum = exp(Illum / sum(weights));
Illum = Illum / max(Illum(:));
% gamma correction
Illum = Illum .^ gamma;
% color restoration
for i = 1:cc
outImg(:,:,i) = inImg(:,:,i) ./ Illum;
end
outImg = outImg - 1;
outImg = outImg / max(outImg(:));
outImg = outImg .^ (1/gamma);
end
```
其中,`inImg` 是输入的彩色图像(RGB),`scales` 是用于 Retinex 处理的尺度,`weights` 是每个尺度的权重,`gamma` 是用于 gamma 校正的参数。函数的输出包括输出的彩色图像 `outImg` 和光照图 `Illum`。
阅读全文