matlab基于亮度分区的自适应对数色调映射代码
时间: 2024-05-26 07:05:17 浏览: 60
由于没有具体的图片或数据,以下代码仅提供参考,可能需要根据实际需求进行修改。
function [result] = adaptivelogarithmicmapping(image)
%image: 输入的图片
%result: 输出的映射后的图片
%将图片转化为灰度图
if size(image,3) == 3
image = rgb2gray(image);
end
%将灰度图归一化到0~1之间
image = double(image) ./ 255;
%将图像分成4个亮度区间
[height, width] = size(image);
blockSizeX = floor(width / 2);
blockSizeY = floor(height / 2);
%计算每个区间的亮度值
block1 = image(1:blockSizeY, 1:blockSizeX);
block2 = image(1:blockSizeY, blockSizeX+1:end);
block3 = image(blockSizeY+1:end, 1:blockSizeX);
block4 = image(blockSizeY+1:end, blockSizeX+1:end);
mean1 = mean(block1(:));
mean2 = mean(block2(:));
mean3 = mean(block3(:));
mean4 = mean(block4(:));
%根据亮度值分别进行映射
%映射公式:s = c * log(r + 1)
if mean1 < 0.5
c1 = 1 / log(1 + mean1);
else
c1 = 1 / log(1 + 1 - mean1);
end
block1 = c1 * log(block1 + 1);
if mean2 < 0.5
c2 = 1 / log(1 + mean2);
else
c2 = 1 / log(1 + 1 - mean2);
end
block2 = c2 * log(block2 + 1);
if mean3 < 0.5
c3 = 1 / log(1 + mean3);
else
c3 = 1 / log(1 + 1 - mean3);
end
block3 = c3 * log(block3 + 1);
if mean4 < 0.5
c4 = 1 / log(1 + mean4);
else
c4 = 1 / log(1 + 1 - mean4);
end
block4 = c4 * log(block4 + 1);
%将4个区域拼接成一张图片
result = [block1 block2; block3 block4];
%将像素值限制在0~1之间
result(result < 0) = 0;
result(result > 1) = 1;
%将图片转化为8位图像
result = uint8(result * 255);
end
阅读全文