为什么曲面拟合在对数域中不起作用?
Why surface fitting is not working in logarithmic domain?
让我们有一个损坏的图像 C、偏差配置文件 B 和真实图像 A。因此,如果我们可以定义一个模型,
C = A * B;
我们可以得到原始图像,
A=C/B;
在日志域中,
log A = log C - log B。
现在让我们说,我有真实的图像 A,我正在引入偏差 B,我得到损坏的图像 C。现在我可以使用多项式回归校正这个有偏差的图像 C。一旦我在对数域中转换损坏的图像 C,我将拟合表面,并且我可以从中减去偏差配置文件,如上所示。减法后,我不需要明显地应用 exp(log C - log B) 。需要 Onlu 归一化才能获得 [0 255] 范围。
算法:
原始图像没有任何偏置场被引入多项式轮廓,这导致图像具有不均匀的照明。
在对数域中转换有偏差的图像,并使用多项式拟合来近似表面
从偏置图像中减去近似表面,从而得到没有偏置场的原始图像。(理想情况下)。
测量近似曲面与步骤1中引入的多项式场之间的RMSE。测量偏置图像与减法后最终得到的图像之间的RMSE。
代码:
clear;clc;close all;
%read the image, on which profile is to be generated
I = ones(300);
I = padarray(I,[20,20],'both','symmetric'); % padding
%%
%creating a bias profile using polynomial modeling
[x,y] = meshgrid(1:size(I,1),1:size(I,2));
profile = -2.5.*x.^3 - 2.5.* y.^3 + 0.25 .*(x.* y.^2) - 0.3*( x.^2 .* y ) - 0.5.* x .* y - x + y - 2.5*( x.^2) - y.^2 + 0.5 .* x .*y + 1;
% come to scale [0 1]
profile = profile - min(profile(:));
profile = profile / max(profile(:));
figure,imshow(profile,[]); %introduced bias profile
%% corrupt the image
biasedImage = (I .* profile);
figure,imshow(biasedImage,[]); %biased Image
cImage = log(biasedImage+1);% conversion to log domain/ +1 is needed to avoid infinite values in case of 0 intensty values in corrupted image.
%% forming the input for prediction of surface
colorChannel = cImage;
rows = size(colorChannel, 1);
columns = size(colorChannel, 2);
[X,Y] = meshgrid(1:columns, 1:rows);
z = colorChannel;
x1d = reshape(X, numel(X), 1);
y1d = reshape(Y, numel(Y), 1);
z1d = double(reshape(z, numel(z), 1)); %dependent variables
x = [x1d y1d]; % two regressors
%% surface fitting
m = 3; %define the order of polynomial
p = polyfitn(x,z1d,m); %prediction step
zg = polyvaln(p, x);
modeledColorChannel = reshape(zg, [rows columns]); % predicted surface
%modeledColorChannel = exp(modeledColorChannel)-1; if we use this step then the step below will be division instead of subtraction
%f = biasedImage./ modeledColorChannel; Same as the step below but as we are using exponential, it will be devision.
%% correction
f = cImage- modeledColorChannel; %getting the original image back.
%grayImage = f(21:end-20,21:end-20);
%modeledColorChannel = modeledColorChannel(21:end-20,21:end-20); %to remove the padding
figure,imshow(f,[]);
figure,imshow(modeledColorChannel,[]);
%% measure the RMSE for image
y = (I - f);
RMSE = sqrt(mean(y(:).^2));
disp(RMSE);
% RMSE for profile
z = (modeledColorChannel - profile);
RMSE = sqrt(mean(z(:).^2));
disp(RMSE);
结果:
如果是:f = cImage-modeledColorChannel
1.0000
0.2127
更正图像:enter image description here
除法时:f = cImage ./ modeledColorChannel(虽然理论上不正确。)
0.0190
0.2127
更正图像:enter image description here
现在,问题是:如果我在对数域中进行除法而不是像我在这里做的那样进行减法,我最后得到的 RMSE 值较低(参见 %% 更正 部分)。在理论上正确的情况下如何有更高的 RMSE 用于减法?根据我的理解,如果我将所有计算都保留在对数域中,图像除法将变成图像 subtraction.It 如果您 运行 代码并且在除法和减法校正结束时看到图像 f 则很明显在日志域中。
注意:在这两种情况下,引入和感知轮廓之间的 RMSE 与我在 cases.Either 图像分割或图像减法中的对数域中进行的估计相同。
查看 polyfitn 工具箱。
www.mathworks.com/matlabcentral/fileexchange/34765-polyfitn
让我补充一下我的问题的答案,因为我发现了我的错误,以防将来有人遇到同样的问题。
错误 1:减法后,我不需要应用 exp(log C - log B) 很明显。只需要归一化即可获得 [0 255] 范围。
我的直觉是,我不需要应用 exp() 来取回原始值。但实际上我必须应用 exp()。 LHS 和 RHS 上的 Log-log 永远不会相互抵消。
如果 log(A) = log(B),要返回 A 的值,我需要 A = exp(log(B))。
错误2:在对数域中,我将两个图像相减,这样我就不必在对数域中面临无穷大的问题,我们通常在除法的情况下会遇到。
所以简单地在日志域中转换图像时,我可以这样做,
cImage = log(biasedImage);
而不是,
cImage = log(biasedImage+1);
在这里,添加 +1 会在图像中产生不需要的模糊,因为在预测表面的估计中,它会将表面推到黑暗区域的高值。
让我们有一个损坏的图像 C、偏差配置文件 B 和真实图像 A。因此,如果我们可以定义一个模型,
C = A * B;
我们可以得到原始图像,
A=C/B;
在日志域中,
log A = log C - log B。
现在让我们说,我有真实的图像 A,我正在引入偏差 B,我得到损坏的图像 C。现在我可以使用多项式回归校正这个有偏差的图像 C。一旦我在对数域中转换损坏的图像 C,我将拟合表面,并且我可以从中减去偏差配置文件,如上所示。减法后,我不需要明显地应用 exp(log C - log B) 。需要 Onlu 归一化才能获得 [0 255] 范围。
算法:
原始图像没有任何偏置场被引入多项式轮廓,这导致图像具有不均匀的照明。
在对数域中转换有偏差的图像,并使用多项式拟合来近似表面
从偏置图像中减去近似表面,从而得到没有偏置场的原始图像。(理想情况下)。
测量近似曲面与步骤1中引入的多项式场之间的RMSE。测量偏置图像与减法后最终得到的图像之间的RMSE。
代码:
clear;clc;close all;
%read the image, on which profile is to be generated
I = ones(300);
I = padarray(I,[20,20],'both','symmetric'); % padding
%%
%creating a bias profile using polynomial modeling
[x,y] = meshgrid(1:size(I,1),1:size(I,2));
profile = -2.5.*x.^3 - 2.5.* y.^3 + 0.25 .*(x.* y.^2) - 0.3*( x.^2 .* y ) - 0.5.* x .* y - x + y - 2.5*( x.^2) - y.^2 + 0.5 .* x .*y + 1;
% come to scale [0 1]
profile = profile - min(profile(:));
profile = profile / max(profile(:));
figure,imshow(profile,[]); %introduced bias profile
%% corrupt the image
biasedImage = (I .* profile);
figure,imshow(biasedImage,[]); %biased Image
cImage = log(biasedImage+1);% conversion to log domain/ +1 is needed to avoid infinite values in case of 0 intensty values in corrupted image.
%% forming the input for prediction of surface
colorChannel = cImage;
rows = size(colorChannel, 1);
columns = size(colorChannel, 2);
[X,Y] = meshgrid(1:columns, 1:rows);
z = colorChannel;
x1d = reshape(X, numel(X), 1);
y1d = reshape(Y, numel(Y), 1);
z1d = double(reshape(z, numel(z), 1)); %dependent variables
x = [x1d y1d]; % two regressors
%% surface fitting
m = 3; %define the order of polynomial
p = polyfitn(x,z1d,m); %prediction step
zg = polyvaln(p, x);
modeledColorChannel = reshape(zg, [rows columns]); % predicted surface
%modeledColorChannel = exp(modeledColorChannel)-1; if we use this step then the step below will be division instead of subtraction
%f = biasedImage./ modeledColorChannel; Same as the step below but as we are using exponential, it will be devision.
%% correction
f = cImage- modeledColorChannel; %getting the original image back.
%grayImage = f(21:end-20,21:end-20);
%modeledColorChannel = modeledColorChannel(21:end-20,21:end-20); %to remove the padding
figure,imshow(f,[]);
figure,imshow(modeledColorChannel,[]);
%% measure the RMSE for image
y = (I - f);
RMSE = sqrt(mean(y(:).^2));
disp(RMSE);
% RMSE for profile
z = (modeledColorChannel - profile);
RMSE = sqrt(mean(z(:).^2));
disp(RMSE);
结果:
如果是:f = cImage-modeledColorChannel
1.0000
0.2127
更正图像:enter image description here
除法时:f = cImage ./ modeledColorChannel(虽然理论上不正确。)
0.0190
0.2127
更正图像:enter image description here
现在,问题是:如果我在对数域中进行除法而不是像我在这里做的那样进行减法,我最后得到的 RMSE 值较低(参见 %% 更正 部分)。在理论上正确的情况下如何有更高的 RMSE 用于减法?根据我的理解,如果我将所有计算都保留在对数域中,图像除法将变成图像 subtraction.It 如果您 运行 代码并且在除法和减法校正结束时看到图像 f 则很明显在日志域中。
注意:在这两种情况下,引入和感知轮廓之间的 RMSE 与我在 cases.Either 图像分割或图像减法中的对数域中进行的估计相同。
查看 polyfitn 工具箱。 www.mathworks.com/matlabcentral/fileexchange/34765-polyfitn
让我补充一下我的问题的答案,因为我发现了我的错误,以防将来有人遇到同样的问题。
错误 1:减法后,我不需要应用 exp(log C - log B) 很明显。只需要归一化即可获得 [0 255] 范围。
我的直觉是,我不需要应用 exp() 来取回原始值。但实际上我必须应用 exp()。 LHS 和 RHS 上的 Log-log 永远不会相互抵消。
如果 log(A) = log(B),要返回 A 的值,我需要 A = exp(log(B))。
错误2:在对数域中,我将两个图像相减,这样我就不必在对数域中面临无穷大的问题,我们通常在除法的情况下会遇到。
所以简单地在日志域中转换图像时,我可以这样做,
cImage = log(biasedImage);
而不是,
cImage = log(biasedImage+1);
在这里,添加 +1 会在图像中产生不需要的模糊,因为在预测表面的估计中,它会将表面推到黑暗区域的高值。