C++ 两个相乘图像的自动分离
我正在寻找一个算法或C++/Matlab库,可以用来分离两个相乘的图像。下面给出了该问题的一个直观示例 图像1可以是任何对象(例如相对复杂的场景)。图2非常简单,可以用数学方法生成。图2始终具有相似的形态(即下降趋势)。通过将图像1乘以图像2(使用逐点乘法),我们得到一个变换后的图像 仅给出变换后的图像,我想估计图像1或图像2。有没有一种算法可以做到这一点 以下是Matlab代码和图像:C++ 两个相乘图像的自动分离,c++,image,matlab,image-processing,C++,Image,Matlab,Image Processing,我正在寻找一个算法或C++/Matlab库,可以用来分离两个相乘的图像。下面给出了该问题的一个直观示例 图像1可以是任何对象(例如相对复杂的场景)。图2非常简单,可以用数学方法生成。图2始终具有相似的形态(即下降趋势)。通过将图像1乘以图像2(使用逐点乘法),我们得到一个变换后的图像 仅给出变换后的图像,我想估计图像1或图像2。有没有一种算法可以做到这一点 以下是Matlab代码和图像: load('trans.mat'); imageA = imread('room.jpg'); imageB
load('trans.mat');
imageA = imread('room.jpg');
imageB = abs(response); % loaded from MAT file
[m,n] = size(imageA);
image1 = rgb2gray( imresize(im2double(imageA), [m n]) );
image2 = imresize(im2double(imageB), [m n]);
figure; imagesc(image1); colormap gray; title('Image 1 of Room')
colorbar
figure; imagesc(image2); colormap gray; title('Image 2 of Response')
colorbar
% This is image1 and image2 multiplied together (point-by-point)
trans = image1 .* image2;
figure; imagesc(trans); colormap gray; title('Transformed Image')
colorbar
更新
有很多方法可以解决这个问题。这是我的实验结果。感谢所有回答我问题的人
1。图像的低通滤波
正如Daskwuff所指出的,对变换后的图像进行低通滤波会返回图像2的近似值。在这种情况下,低通滤波器是高斯滤波器。您可以看到,使用低通滤波器可以识别图像中的乘性噪声
2。同态滤波
正如EitenT所建议的,我检查了同态滤波。知道了这种类型的图像过滤的名称,我设法找到了一些我认为对解决类似问题有用的参考资料
fminsearch进行优化
正如Serg所建议的,我使用了带有fminsearch
的目标函数。因为我知道噪声的数学模型,所以我能够将其作为优化算法的输入这种方法完全是针对具体问题的,可能并非在所有情况下都有用。
下面是图像2的重建:
这里是图像1的重建,通过除以图像2的重建而形成:
以下是包含噪声的图像:
源代码
这是我的问题的源代码。正如代码所示,这是一个非常特殊的应用程序,在所有情况下都不能很好地工作
N = 1001;
q = zeros(N, 1);
q(1:200) = 55;
q(201:300) = 120;
q(301:400) = 70;
q(401:600) = 40;
q(601:800) = 100;
q(801:1001) = 70;
dt = 0.0042;
fs = 1 / dt;
wSize = 101;
Glim = 20;
ginv = 0;
[R, ~, ~] = get_response(N, q, dt, wSize, Glim, ginv);
rows = wSize;
cols = N;
cut_val = 200;
figure; imagesc(abs(R)); title('Matrix output of algorithm')
colorbar
figure;
imagesc(abs(R)); title('abs(response)')
figure;
imagesc(imag(R)); title('imag(response)')
imageA = imread('room.jpg');
% images should be of the same size
[m,n] = size(R);
image1 = rgb2gray( imresize(im2double(imageA), [m n]) );
% here is the multiplication (with the image in complex space)
trans = ((image1.*1i)) .* (R(end:-1:1, :));
figure;
imagesc(abs(trans)); colormap(gray);
% take the imaginary part of the response
imagLogR = imag(log(trans));
% The beginning and end points are not usable
Mderiv = zeros(rows, cols-2);
for k = 1:rows
val = deriv_3pt(imagLogR(k,:), dt);
val(val > cut_val) = 0;
Mderiv(k,:) = val(1:end-1);
end
% This is the derivative of the imaginary part of R
% d/dtau(imag((log(R)))
% Do we need to remove spurious values from the matrix?
figure;
imagesc(abs(log(Mderiv)));
disp('Running iteration');
% Apply curve-fitting to get back the values
% by cycling over the cols
q0 = 10;
q1 = 500;
NN = cols - 2;
qout = zeros(NN, 1);
for k = 1:NN
data = Mderiv(:,k);
qout(k) = fminbnd(@(q) curve_fit_to_get_q(q, dt, rows, data),q0,q1);
end
figure; plot(q); title('q value input as vector');
ylim([0 200]); xlim([0 1001])
figure;
plot(qout); title('Reconstructed q')
ylim([0 200]); xlim([0 1001])
% make the vector the same size as the other
qout2 = [qout(1); qout; qout(end)];
% get the reconstructed response
[RR, ~, ~] = get_response(N, qout2, dt, wSize, Glim, ginv);
RR = RR(end:-1:1,:);
figure; imagesc(abs(RR)); colormap gray
title('Reconstructed Image 2')
colorbar;
% here is the reconstructed image of the room
% NOTE the division in the imagesc function
check0 = image1 .* abs(R(end:-1:1, :));
figure; imagesc(check0./abs(RR)); colormap gray
title('Reconstructed Image 1')
colorbar;
figure; imagesc(check0); colormap gray
title('Original image with noise pattern')
colorbar;
function [response, L, inte] = get_response(N, Q, dt, wSize, Glim, ginv)
fs = 1 / dt;
Npad = wSize - 1;
N1 = wSize + Npad;
N2 = floor(N1 / 2 + 1);
f = (fs/2)*linspace(0,1,N2);
omega = 2 * pi .* f';
omegah = 2 * pi * f(end);
sigma2 = exp(-(0.23*Glim + 1.63));
sign = 1;
if(ginv == 1)
sign = -1;
end
ratio = omega ./ omegah;
rs_r = zeros(N2, 1);
rs_i = zeros(N2, 1);
termr = zeros(N2, 1);
termi = zeros(N2, 1);
termr_sub1 = zeros(N2, 1);
termi_sub1 = zeros(N2, 1);
response = zeros(N2, N);
L = zeros(N2, N);
inte = zeros(N2, N);
% cycle over cols of matrix
for ti = 1:N
term0 = omega ./ (2 .* Q(ti));
gamma = 1 / (pi * Q(ti));
% calculate for the real part
if(ti == 1)
Lambda = ones(N2, 1);
termr_sub1(1) = 0;
termr_sub1(2:end) = term0(2:end) .* (ratio(2:end).^-gamma);
else
termr(1) = 0;
termr(2:end) = term0(2:end) .* (ratio(2:end).^-gamma);
rs_r = rs_r - dt.*(termr + termr_sub1);
termr_sub1 = termr;
Beta = exp( -1 .* -0.5 .* rs_r );
Lambda = (Beta + sigma2) ./ (Beta.^2 + sigma2); % vector
end
% calculate for the complex part
if(ginv == 1)
termi(1) = 0;
termi(2:end) = (ratio(2:end).^(sign .* gamma) - 1) .* omega(2:end);
else
termi = (ratio.^(sign .* gamma) - 1) .* omega;
end
rs_i = rs_i - dt.*(termi + termi_sub1);
termi_sub1 = termi;
integrand = exp( 1i .* -0.5 .* rs_i );
L(:,ti) = Lambda;
inte(:,ti) = integrand;
if(ginv == 1)
response(:,ti) = Lambda .* integrand;
else
response(:,ti) = (1 ./ Lambda) .* integrand;
end
end % ti loop
function sse = curve_fit_to_get_q(q, dt, rows, data)
% q = trial q value
% dt = timestep
% rows = number of rows
% data = actual dataset
fs = 1 / dt;
N2 = rows;
f = (fs/2)*linspace(0,1,N2); % vector for frequency along cols
omega = 2 * pi .* f';
omegah = 2 * pi * f(end);
ratio = omega ./ omegah;
gamma = 1 / (pi * q);
% calculate for the complex part
termi = ((ratio.^(gamma)) - 1) .* omega;
% for now, just reverse termi
termi = termi(end:-1:1);
%
% Do non-linear curve-fitting
% termi is a column-vector with the generated noise pattern
% data is the log-transformed image
% sse is the value that is returned to fminsearchbnd
Error_Vector = termi - data;
sse = sum(Error_Vector.^2);
function output = deriv_3pt(x, dt)
N = length(x);
N0 = N - 1;
output = zeros(N0, 1);
denom = 2 * dt;
for k = 2:N0
output(k - 1) = (x(k+1) - x(k-1)) / denom;
end
这将是一个困难的、不可靠的过程,因为您从根本上试图提取已被破坏的信息(两幅图像的分离)。把它完美地带回来是不可能的;你能做的就是猜测
如果第二个图像总是相对“平滑”,则可以通过对变换后的图像应用强低通滤波器来重建它(或至少是它的近似值)。有了它,您可以反转乘法,或者等效地使用互补高通滤波器来获得第一幅图像。它不会完全相同,但至少会有一些不同。我会尝试约束优化(
fmincon
在Matlab中)。
如果您了解第二幅图像的来源/性质,您可能可以定义一个多变量函数来生成类似的噪声模式。目标函数可以是生成的噪声图像和最后一幅图像之间的相关性。您在这里描述的是某种乘法干扰。您可能会发现适合您的任务。@EitanT:谢谢,EitanT。关于同态过滤问题,你能推荐一本好的参考书(书、论文或算法)吗?好吧,没有什么比这更简单的了(你马上就会得到关于这个主题的各种文章)。但是请记住,您所读到的任何方法都只是一种估计,您永远无法完全一致地恢复图像。@EitanT:啊,是的,在搜索中使用“乘性噪声”确实为我指明了更多正确的方向。谢谢,伊坦特。这不是一个容易的任务,即使有一个给定的算法。它们中的大多数都不能很好地处理各种各样的图像。不管怎样,祝你好运:)谢谢你,达斯沃夫;这是一个非常有趣的问题。第二张图片总是相对“平滑”,看起来总是“相似”。我会再探索一点,然后再给你回复。谢谢,塞格;这是一个有趣的建议。我可以定义一个产生“相似”噪声模式的多元模式。生成模式的函数
f(a)
实际上依赖于单个变量a
,但我不知道所有图像的a
。但是假设我选择a
作为近似值,我生成噪声模式,然后在频域中执行二维相关。2D相关性的输出也是矩阵吗?据我所知,fmincon
要求将目标函数输出作为实标量值最小化。从2D相关矩阵,如何获得标量值?当我将trans
矩阵的一列与image2
的同一列相关联时,使用xcorr
函数在零滞后时的1D相关性似乎最大化,因此我认为作为相关性的目标函数可能工作得很好。我将进一步探索,然后跟进。我将使用:c=corr(img1(:),img2(:)
其中img1
是带噪图像,img2
是生成的噪声模式