变半径高斯模糊,逼近内核
我正在编写具有可变半径(标准差)的高斯模糊,即图像的每个像素都使用不同的内核进行卷积。计算高斯模糊的标准技术在这里不起作用:FFT、轴分离、重复框模糊——它们都假设整个图像的内核是相同的。
现在,我尝试使用以下方案对其进行近似:
使用由一组 N 轴对齐矩形 Rk 定义的分段常数函数 f(x,y) 来近似高斯核 K(x,y) 和系数 αk 为:
f(x,y) = Σk=1N αk·χRk(x,y )
设 g(x,y) 为我们的图像,则
∬ℝ2 K(x,y)·g(x,y ) dxdy ≈ ∬ℝ2 f(x,y)·g(x,y) dxdy = Σk=1N αk·∬Rkg(x,y) dxdy
RHS 上的积分是矩形上的简单积分,如下这样可以计算出通过预先计算整个图像的部分和来在恒定时间内完成。
所得算法的运行时间为 O(W·H·N),其中 W 和 H 是图像的尺寸,N 与近似值的误差成反比。
剩下的部分是找到一个好的近似函数f(x,y)。 当给定矩形数量 N(最小化误差)或给定误差(最小化矩形数量)时,如何找到高斯的最佳近似值?
I'm writing a Gaussian blur with variable radius (standard deviation), i.e. each pixel of the image is convolved using a different kernel. The standard techniques to compute Gaussian blur don't work here: FFT, axis-separation, repeated box-blur—they all assume that the kernel is the same for the whole image.
Now, I'm trying to approximate it using the following scheme:
Approximate the Gaussian kernel K(x,y) with a piecewise constant function f(x,y) defined by a set N of axis-aligned rectangles Rk and coefficients αk as:
f(x,y) = ∑k=1N αk·χRk(x,y)
Let g(x,y) be our image, then
∬ℝ2 K(x,y)·g(x,y) dxdy ≈
∬ℝ2 f(x,y)·g(x,y) dxdy = ∑k=1N αk·∬Rkg(x,y) dxdy
The integral on the RHS is a simple integral over a rectangle, and as such can be computed in constant time by precomputing the partial sums for the whole image.
The resulting algorithm runs in O(W·H·N) where W and H are the dimensions of the image and N is (AFAIK) inverse proportional to the error of the the approximation.
The remaining part is to find a good approximation function f(x,y). How to find the optimal approximation to the Gaussian when given either the number of rectangles N (minimizing the error) or given the error (minimizing the number of rectangles)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给定矩形的位置和大小,计算出系数应该相当容易,因此真正的问题是计算出矩形的放置位置。
由于您正在逼近高斯分布,因此将我们的注意力限制在中心与高斯中心重合的矩形上似乎至少是合理的,因此我们实际上只有一个一维问题 - 计算出一组嵌套矩形的大小如果您的纵横比不是统一的,我认为要么是正方形,要么类似于高斯。
这可以通过动态规划来解决。假设您从外部到中间工作。在第 N 阶段,您已经制定了一个 nxk 表,该表为您提供了来自 1,2...N 个外部像素环(最多 1,2,..k 个不同矩形)的最佳近似误差,以及最里面矩形的大小对该最佳错误负责。为了计算出第 N+1 阶段,您需要考虑到目前为止最里面的矩形的所有可能大小,为外部区域贡献 x 个像素环。您计算出该矩形的 alpha,它为新环中的像素提供了最佳拟合,并且其外部的环不留给外部矩形。使用表中已经计算出的值,您知道当您留下最多 k 个外部矩形来覆盖这些区域时,您将得到的最佳可能误差,因此您可以计算出现在 N+1 个像素环所贡献的最佳总误差。这允许您填写 N+1 个外部像素的表条目。当您进入该区域的中间时,您将能够找出整个区域的最佳解决方案。
Given the locations and size of the rectangles, it should be fairly easy to work out your coefficients, so the real problem is working out where to put the rectangles.
Since you are approximating a Gaussian, it seems at least reasonable to restrict our attention to rectangles whose centre coincides with the centre of the Gaussian, so we actually have only a 1-dimensional problem - working out the sizes of a nested set of rectangles which I presume are either squares or are similar to the Gaussian if you have an aspect ratio other than unity.
This can be solved via dynamic programming. Suppose that you work from the outside into the middle. At stage N you have worked out an n x k table that gives you the best possible approximation error coming from 1,2...N rings of outer pixels for up 1,2,..k different rectangles, and the size of the innermost rectangle responsible for that best error. To work out stage N+1 you consider every possible size for what will be an innermost rectangle so far, contributing x rings of pixels to the outer area. You work out the alpha for that rectangle that gives the best fit for the pixels in the new ring and the rings outside it not left to outer rectangles. Using the values in the table already calculated you know the best possible error you will get when you leave up to k outer rectangles to cover those areas, so you can work out the best total error contributed from what is now N+1 rings of pixels. This allows you to fill in the table entries for N+1 outer pixels. When you have worked your way into the middle of the area you will be able to work out the optimal solution for the whole area.