网格上直方图的快速计算

发布于 2024-11-29 20:27:20 字数 267 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2024-12-06 20:27:20

如果您有图像处理工具箱,则可以使用函数 blockproc< /code>这是循环的编译后的通用版本。只需将回调函数定义为您的直方图计算即可。

B = blockproc(I, [8 8], @myhistfun)

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.

B = blockproc(I, [8 8], @myhistfun)
浊酒尽余欢 2024-12-06 20:27:20

我认为下面的代码可以回答您的问题。诀窍是不要在循环内调用任何函数并预先分配所有数组。参见例如 http://www.quantiphile.com/2010/10/ 16/optimizing-matlab-code/ 了解有关循环加速的更多信息。无论如何,在我的机器上,加速循环速度快了 17 倍。

% image size
height = 800;
width = 1200;
window = 8;

% histogram bin centers
bin_centers = 0.05:0.1:1;

% here a random image as input
img = rand(height, width);

% verion using accelerated loops (for this to work there cannot be any
% function calls to not built-in functions)
tic
img3 = zeros(window^2, height*width/window^2);
ind = 1;
for i=1:window:height
    for j=1:window:width
       patch_ = img(i:i+window-1,j:j+window-1);
       img3(:,ind) = patch_(:);
       ind = ind + 1;
    end
end
hist_img3 = hist(img3, bin_centers);
toc


% probably version of user499372 calling hist function within the loop
tic
hist_img4 = zeros(size(hist_img3));
ind = 1;
for i=1:window:height
    for j=1:window:width
       patch_ = img(i:i+window-1,j:j+window-1);
       hist_img4(:,ind) = hist(patch_(:), bin_centers);
       ind = ind + 1;
       % compute histogram for the patch
    end
end
toc

% test the results
all(all(hist_img3==hist_img4))

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.

% image size
height = 800;
width = 1200;
window = 8;

% histogram bin centers
bin_centers = 0.05:0.1:1;

% here a random image as input
img = rand(height, width);

% verion using accelerated loops (for this to work there cannot be any
% function calls to not built-in functions)
tic
img3 = zeros(window^2, height*width/window^2);
ind = 1;
for i=1:window:height
    for j=1:window:width
       patch_ = img(i:i+window-1,j:j+window-1);
       img3(:,ind) = patch_(:);
       ind = ind + 1;
    end
end
hist_img3 = hist(img3, bin_centers);
toc


% probably version of user499372 calling hist function within the loop
tic
hist_img4 = zeros(size(hist_img3));
ind = 1;
for i=1:window:height
    for j=1:window:width
       patch_ = img(i:i+window-1,j:j+window-1);
       hist_img4(:,ind) = hist(patch_(:), bin_centers);
       ind = ind + 1;
       % compute histogram for the patch
    end
end
toc

% test the results
all(all(hist_img3==hist_img4))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文