Finding the Best Gaussian Smoothing Kernel to Minimize the Discrepancy Between Two Images - Signal Processing Stack Exchange most recent 30 from dsp.stackexchange.com 2019-07-22T21:44:24Z https://dsp.stackexchange.com/feeds/question/49121 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://dsp.stackexchange.com/q/49121 2 Finding the Best Gaussian Smoothing Kernel to Minimize the Discrepancy Between Two Images eriophora https://dsp.stackexchange.com/users/35666 2018-05-11T05:35:35Z 2018-05-12T06:45:21Z <p>Suppose we have two grayscale images, $A$ and $B$. $A$ and $B$ <em>very strongly</em> resemble each other, such that the mean of the absolute difference $\lvert A - B\rvert$ is fairly small. Suppose further that $B$ actually appears to be a blurred version of $A$, although it was not necessarily obtained through a blurring process. I'd like to find the ideal Gaussian kernel $g$ to smooth $A$ with to maximize its correspondence to $B$, in other words, select $g$ s.t.</p> <p>$$\underset{g}{\arg\min}\sum{\lvert(g*A) - B\rvert}$$</p> <p>where $g$, $A$, and $B$ are 2D arrays and $*$ is the convolution operator. </p> https://dsp.stackexchange.com/questions/49121/-/49128#49128 1 Answer by Royi for Finding the Best Gaussian Smoothing Kernel to Minimize the Discrepancy Between Two Images Royi https://dsp.stackexchange.com/users/128 2018-05-11T16:39:30Z 2018-05-12T06:45:21Z <p>This is closely related to <a href="https://en.wikipedia.org/wiki/Blind_deconvolution" rel="nofollow noreferrer">Blind Deconvolution</a>.<br> The only difference is we limit our self to a very specific type of blur kernels. </p> <p>The nice thing about the Gaussian Kernel is being defined by single parameter - The Standard Deviation of the kernel.<br> The less nice thing is the connection isn't linear. </p> <h2>Optimization Problem</h2> <p>Let's define a classic non linear model for this problem:</p> <p>$$\arg \min_{\sigma} \frac{1}{2} {\left\| A \left( \sigma \right) x - b \right\|}_{2}^{2}$$</p> <p>Where $A \left( \sigma \right)$ is the convolution matrix generated by a Gaussian Kernel parameterized by $\sigma$, $x$ is the original image ($A$ in your question) in a vector shape, and $b$ is the blurred image ($B$ in your question).<br> This is a classical <a href="https://en.wikipedia.org/wiki/Non-linear_least_squares" rel="nofollow noreferrer">Non Linear Least Squares</a> problem which can be solved by MATLAB using <a href="https://www.mathworks.com/help/optim/ug/lsqnonlin.html" rel="nofollow noreferrer"><code>lsqnonlin()</code></a>.</p> <h2>Code Sample</h2> <p>This is the main part of the code:</p> <pre><code>mA = rand([numRows, numCols]); gaussianKernelStd = kernelStdLowerBound + ((kernelStdUpperBound - kernelStdLowerBound) * rand(1)); mB = ApplyGaussianBlur(mA, gaussianKernelStd, STD_TO_RADIUS_FACTOR); % Objective Functions hObjFun = @(kernelStd) reshape(ApplyGaussianBlur(mA, kernelStd, STD_TO_RADIUS_FACTOR) - mB, [numPx, 1]); estKernelEst = lsqnonlin(hObjFun, initKernelStd, kernelStdLowerBound, kernelStdUpperBound, sSolverOptions); </code></pre> <h2>Results</h2> <p><a href="https://i.stack.imgur.com/g0Qfq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g0Qfq.png" alt="enter image description here"></a></p> <p>As can be seen, the estimation is almost perfect.<br> On large images it might take time (Using some tricks of the Gaussian Filter in the Fourier Domain one could do that there with major speed up), but still, it is not free. </p> <p>The full code is available on my <a href="https://github.com/RoyiAvital/StackExchangeCodes/tree/master/SignalProcessing/Q49121" rel="nofollow noreferrer">StackExchange Signal Processing Q49121 GitHub Repository</a>.</p> https://dsp.stackexchange.com/questions/49121/-/49139#49139 0 Answer by Cris Luengo for Finding the Best Gaussian Smoothing Kernel to Minimize the Discrepancy Between Two Images Cris Luengo https://dsp.stackexchange.com/users/33605 2018-05-12T01:03:26Z 2018-05-12T01:03:26Z <p>Considering that the convolution is a multiplication in the Fourier domain, this problem can be converted to a very simple fitting problem.</p> <p>$B$ is a blurred version of $A$. Thus we have $\hat{B} = \hat{A} G$, with $\hat B$ the DFT of $B$. This can be rewritten into an expression $G = \hat{B} / \hat{A}$, with a point-wise division. Next, simply fit a Gaussian to $G$. You can do this while ignoring the pixels where $\hat{A}$ is close to zero. </p>