网格上直方图的快速计算
我有一个 200x200 的灰度图像,我想计算图像中每个 8x8 窗口的强度直方图。我怎样才能计算得那么快?我现在使用 for 循环,但它太慢了。我当前的代码如下所示:
I = imread('image.jpg');
for i=1:8:height-7
for j=1:8:width-7
patch = I(i:i+7,j:j+7);
% compute histogram for the patch
end
end
I have a greyscale 200x200 image and I would like to compute the histogram of the intensity for each 8x8 window in the image. How can I compute that fast? I use for loops now but it is so slow. My current code looks like:
I = imread('image.jpg');
for i=1:8:height-7
for j=1:8:width-7
patch = I(i:i+7,j:j+7);
% compute histogram for the patch
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有图像处理工具箱,则可以使用函数
blockproc< /code>
这是循环的编译后的通用版本。只需将回调函数定义为您的直方图计算即可。
If you have the Image Processing Toolbox you can use the function
blockproc
which is a compiled and general version of your loop. Just define the callback function to be your histogram calculation.我认为下面的代码可以回答您的问题。诀窍是不要在循环内调用任何函数并预先分配所有数组。参见例如 http://www.quantiphile.com/2010/10/ 16/optimizing-matlab-code/ 了解有关循环加速的更多信息。无论如何,在我的机器上,加速循环速度快了 17 倍。
I think below code may answer your question. The trick is not to call any functions inside a loop and have all arrays preallocated. See e.g. http://www.quantiphile.com/2010/10/16/optimizing-matlab-code/ for more on loop acceleration. Anyways, on my machine below accelerated loop is 17 times faster.