加快normxcorr2最大值的计算 [英] Speed up calculation of maximum of normxcorr2

查看:154
本文介绍了加快normxcorr2最大值的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算百万个粒子的归一化互相关的最大值. normxcorr2两个参数的大小为56 * 56.我无法并行计算.有什么建议可以加快代码的速度,特别是我不需要所有结果,而只需要每个互相关的最大值(知道位移)即可.

I need to calculate the maximum of normalized cross correlation of million of particles. The size of the two parameters of normxcorr2 is 56*56. I can't parallelize the calculations. Is there any suggestion to speed up the code especially that I don't need all the results but only the maximum value of each cross correlation (to know the displacement)?

算法示例

%The choice of 170 particles is because in each time 
%the code detects 170 particles, so over 10000 images it's 1 700 000 particles
particle_1=rand(54,54,170);
particle_2=rand(56,56,170);
for i=1:170
    C=normxcorr2(particle_1(:,:,i),particle_2(:,:,i));
    L(i)=max(C(:));
end

推荐答案

我没有MATLAB,因此我在此站点上运行了以下代码:

I don't have MATLAB so I ran the following code on this site: https://www.tutorialspoint.com/execute_matlab_online.php which is actually octave. So I implemented "naive" normalized cross correlation and indeed for these small images sizes the naive performs better:

经过的时间是2.62645秒-对于normxcorr2
经过的时间是0.199034秒-对于我的naive_normxcorr2

Elapsed time is 2.62645 seconds - for normxcorr2
Elapsed time is 0.199034 seconds - for my naive_normxcorr2

代码基于文章 http://scribblethink.org/Work/nvisionInterface/nip.pdf ,其中描述了如何使用

The code is based on the article http://scribblethink.org/Work/nvisionInterface/nip.pdf which describes how to calculate the standard deviation needed for the normalization in an efficient way using integral image, this is the box_corr function.

此外,MATLAB的normxcorr2返回一个填充的图像,因此我在未填充的部分上使用了最大值.

Also, MATLAB's normxcorr2 returns a padded image so I took the max on the unpadded part.

pkg load image

function [N] = naive_corr(pat,img)

[n,m] = size(img);
[np,mp] = size(pat);

 N = zeros(n-np+1,m-mp+1);

 for i = 1:n-np+1
    for j = 1:m-mp+1
        N(i,j) = sum(dot(pat,img(i:i+np-1,j:j+mp-1)));
    end
 end

end


%w_arr the array of coefficients for the boxes
%box_arr of size [k,4] where k is the number boxes, each box represented by
%4 something ... 
function [C] = box_corr2(img,box_arr,w_arr,n_p,m_p)

% construct integral image + zeros pad (for boundary problems)
I = cumsum(cumsum(img,2),1);
I = [zeros(1,size(I,2)+2); [zeros(size(I,1),1) I zeros(size(I,1),1)]; zeros(1,size(I,2)+2)];

% initialize result matrix
[n,m] = size(img);
C = zeros(n-n_p+1,m-m_p+1);
%C = zeros(n,m);

jump_x = 1;
jump_y = 1;

x_start = ceil(n_p/2);
x_end = n-x_start+mod(n_p,2);
x_span = x_start:jump_x:x_end;

y_start = ceil(m_p/2);
y_end = m-y_start+mod(m_p,2);
y_span = y_start:jump_y:y_end;

arr_a = box_arr(:,1) - x_start;
arr_b = box_arr(:,2) - x_start+1;
arr_c = box_arr(:,3) - y_start;
arr_d = box_arr(:,4) - y_start+1;

% cumulate box responses
k = size(box_arr,1); % == numel(w_arr)
for i = 1:k
    a = arr_a(i);
    b = arr_b(i);
    c = arr_c(i);
    d = arr_d(i);

    C = C ...
        + w_arr(i) * ( I(x_span+b,y_span+d) ...
                       - I(x_span+b,y_span+c) ...
                       - I(x_span+a,y_span+d) ...
                       + I(x_span+a,y_span+c) );
end

end



function [NCC]  = naive_normxcorr2(temp,img)

    [n_p,m_p]=size(temp);

    M = n_p*m_p;

    % compute template mean & std
    temp_mean = mean(temp(:));
    temp = temp - temp_mean;

    temp_std = sqrt(sum(temp(:).^2)/M);

    % compute windows' mean & std
    wins_mean =  box_corr2(img,[1,n_p,1,m_p],1/M,  n_p,m_p);
    wins_mean2 = box_corr2(img.^2,[1,n_p,1,m_p],1/M,n_p,m_p);


    wins_std = real(sqrt(wins_mean2 - wins_mean.^2));
    NCC_naive = naive_corr(temp,img);

    NCC = NCC_naive ./ (M .* temp_std .* wins_std);
end 

n = 170;

particle_1=rand(54,54,n);
particle_2=rand(56,56,n);

[n_p1,m_p1,c_p1]=size(particle_1);
[n_p2,m_p2,c_p2]=size(particle_2);

L1 = zeros(n,1);
L2 = zeros (n,1);

tic
for i=1:n
    C1=normxcorr2(particle_1(:,:,i),particle_2(:,:,i));

    C1_unpadded = C1(n_p1:n_p2 , m_p1:m_p2);
    L1(i)=max(C1_unpadded(:));

end

toc

tic
for i=1:n

    C2=naive_normxcorr2(particle_1(:,:,i),particle_2(:,:,i));
    L2(i)=max(C2(:));
end

toc

这篇关于加快normxcorr2最大值的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆