在 Matlab 中应用一组图像滤波器
我需要在 Matlab 中使用一组过滤器来过滤图像。我的第一次尝试是使用一个简单的 for 循环来重复调用库中每个过滤器的“imfilter”函数。
我需要为我的申请多次重复此过程,因此我需要使此步骤尽可能高效。因此,我想知道是否有任何方法可以对该操作进行矢量化以加快该过程。为了简化事情,我的所有过滤器内核都具有相同的大小(9x9)。
作为我想要的示例,我的过滤器设置在 9x9x32 元素块中,需要将其应用于我的图像。我考虑过将图像复制到一个块中(例如 100x100x32),但我不确定是否有一种方法可以应用卷积等操作而不诉诸循环。有人对解决这个问题的好方法有建议吗?
I need to filter an image using a bank of filters in Matlab. My first attempt was to use a simple for loop to repeatedly call the "imfilter" function for each filter in the bank.
I will need to repeat this process many times for my application, so I need to this step to be as efficient as possible. Therefore, I was wondering if there was any way this operation could be vectorized to speed up the process. In an effort to simplify things, all of my filter kernels are the same size (9x9).
As an example of what I am going for, my filters are set up in a 9x9x32 element block, which needs to be applied to my image. I thought about replicating the image into a block (e.g. 100x100x32), but I'm not sure if there's a way to apply an operation like convolution without resorting to loops. Does anyone have suggestions for a good way of tackling this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了预先分配空间之外,没有更快的方法来获得精确的解决方案。如果近似值没问题,那么您也许能够将 32 个滤波器分解为一组较少数量的滤波器(例如 8 个)的线性组合。例如,参见可操纵滤波器。
http://people.csail.mit.edu/billf/papers/steerpaper91FreemanAdelson.pdf
编辑:这是一个帮助将滤镜应用于图像的工具。
Other than pre allocating the space, there is not a faster way to arrive at an exact solution. If approximations are ok, then you might be able to decompose the 32 filters into a set of linear combinations of a smaller number of filters, say eight. See for instance Steerable filters.
http://people.csail.mit.edu/billf/papers/steerpaper91FreemanAdelson.pdf
edit: here is a tool to help apply filters to images.
新答案:使用 colfilt() 或块过滤样式。 Matlab 可以将图像转换为大矩阵,其中每个不同的 9x9 像素区域都是单列(81 个元素)。使用 im2col() 方法进行制作。如果您的图像是 N × M,则结果矩阵将为 81 X (N-8)*(M-8)。
然后,您可以将所有过滤器连接到单个矩阵(每个过滤器都是一行)并将这些巨大的矩阵相乘。这将为您提供所有过滤器的结果。现在您必须从结果矩阵重建 32 个结果图像。使用 col2im() 方法。
欲了解更多信息,请输入“doc colfilt”。
此方法的工作速度几乎与 mex 文件一样快,并且不需要任何“for”循环
旧答案:
您想要获得不同的 32 个结果还是过滤器组合的单个结果?
如果结果单一,那么有一个简单的方法。
如果您使用线性滤波器(如卷积),则将一个滤波器应用到另一个滤波器上。最后将生成的滤镜应用到图像上。因此图像只会被卷积一次。
如果您的滤波器是对称的(x 和 y 方向),则不要应用 9x9 滤波器,而是在 y 方向上应用 9x1,在 x 方向上应用 1x9。工作速度快一点。
最后,您可以尝试使用Mex文件
New Answer: Use colfilt() or block filtering style. Matlab can transform your image into large matrix where each distinct 9x9 pixel area is a single column (81 elements). Make it using im2col() method. If your image is N by M the result matrix would be 81 X (N-8)*(M-8).
Then you can concatenate all your filters to single matrix (each filter is a row) and multiply those huge matrices. This will give you the result of all filters. Now you have to reconstruct back 32 result images from the result matrix. use col2im() method.
For more information type 'doc colfilt'
This method works almost as fast as mex file and doesnt require any 'for' loop
Old answer:
Do you want to get different 32 results or single result for combination of filters?
If it is a sungle result than there is an easy way.
If you use linear filters (like convolutions) then apply filters one on another. Finally apply the resulting filter on the image. Thus image will be convolved only once.
If you filters are symmetric (x and y direction) then instead of applying the 9x9 filter apply 9x1 on y direction and 1x9 on x direction. Works a bit faster.
Finally, you can try using Mex file