2012-04-10 18 views
1

私は、イメージ内の複数のROI(関心領域)からの標準的なBag of Visual Wordsヒストグラムを計算するために既存の関数/ツールを探しています。私に説明してみましょう:画像ROIから効率的なヒストグラム計算

(1)は、各 "ピクセル" は整数を担持画像があると仮定:1 ... K ような各 "ピクセル" は、情報を

  1. Xを以下ており、Yは
  2. 座標1からKに

(2)固定サイズの領域を大量に仮定フォーマット内のすべての画像からのサンプルである:

  1. (X1、Y1) - トップは、
  2. (X2、Y2)の座標左 - 右座標、底

(3)領域毎に:計算の出現回数をカウントKビンヒストグラムその領域に落ちる「ピクセル」の値は、それは非常に遅い私は、コードのループのために複数のMATLABではなく、原因、次の機能を実装している

ある

function [H words] = sph_roi(wind, tree, desc, feat, bins) 
% FUNCTION computes an SPH histogram for a collection of windows. Spatial 
% information is captured by splitting the window in bins horizontally. 
% 
% [H words] = sph_roi(obj_wind, tree, desc, feat, [ bins ]); 
% 
% INPUT : 
% wind  - sampled ROI windows 
%     [left_x, top_y, right_x, bottom_y] - see sample_roi() 
% tree  - vocabulary tree 
% desc  - descriptors matrix 
% feat  - features matrix 
% bins  - number of horizontal cells (1=BOVW, 2... SPH) 
%     by default set to the multiples of window height. 
% 
% OUTPUT : 
% H   - SPH histograms 
% words  - word IDs found for every descriptor 
% 

verbose = 0; 

% input argument number check 
if nargin < 4 
error('At least 4 input arguments required.'); 
end 

% default number of horizontal cells 
if nargin < 5 
bins = -1; % will be set in multiples of each window height corresp. 
end 

% number of windows 
num_wind = size(wind, 1); 

% number of visual words 
num_words = tree.K; 

% pre-compute all visual words 
words = vl_hikmeanspush(tree, desc); 

% initialize SPH histograms matrix 
H = zeros(num_words * bins, num_wind); 

% compute BOVW for each ROI 
for i = 1 : num_wind 

if verbose == 1 
    fprintf('sph_roi(): processing %d/%d\n', i, num_wind); 
end 

% pick a window 
wind_i = wind(i, :); 

% get the dimensions of the window 
[w h] = wind_size(wind_i); 

% if was not set - the number of horizontal bins 
if bins == -1 
    bins = round(w/h); 
end 

% return a list of subcell windows 
scw = create_sph_wind(wind_i, bins); 

for j = 1 : bins 

    % pick a cell 
    wind_tmp = scw(j, :); 

    % get the descriptor ids falling in that cell 
    ids = roi_feat_ids(wind_tmp, feat); 

    % compute the BOVW histogram for the current cell 
    h = vl_hikmeanshist(tree, words(ids)); 

    % assemble the SPH histogram in the output matrix directly 
    H(1+(j-1)*num_words : j*num_words, i) = h(2:end); 

end 

end 

function ids = roi_feat_ids(w, f) 
% FUNCTION returns those feature ids that fall in the window. 
% 
% ids = roi_feat_ids(w, f); 
% 
% INPUT : 
% w - window 
% f - all feature points 
% 
% OUTPUT : 
% ids - feature ids 
% 

% input argument number check 
if nargin ~= 2 
error('Two input arguments required.'); 
end 

left_x = 1; 
top_y = 2; 
right_x = 3; 
bottom_y = 4; 

% extract and round the interest point coordinates 
x = round(f(1,:)); 
y = round(f(2,:)); 

% bound successively the interest points 
s1 = (x > w(left_x)); % larger than left_x 
s2 = (x < w(right_x)); % smaller than right_x 
s3 = (y > w(top_y)); % larger than top_y 
s4 = (y < w(bottom_y)); % smaller than bottom_y 

% intersection of these 4 sets are the ROI enclosed interest points 
ids = s1 & s2 & s3 & s4; 

% convert ids to real 
ids = find(ids); 

私はによって提案されたルーチンを見てきましたOpenCVとIntでさえelのMKLは何も適切ではないことが分かった。 Matlabのプロファイラを使用して、roi_feat_ids()にかなりの時間が費やされ、関数sph_roi()の各領域の外側ループが遅くなることがわかりました。 MEXファイルを実装する前に、既存のコードをリサイクルできるかどうかを確認したいと思います。

答えて

1

これをスピードアップするためにいくつかのことがあります。

  1. 非常に最後の行はids = find(ids);(除去されなければならない。論理マスクは検索を使用するよりもはるかに高速であり、彼らはで見つける文がうまくいくことはほぼすべての場合に動作します。私は、これはかなりあなたの機能をスピードアップします疑い、
  2. s1、s2、s3、およびs4ステートメントの一部を組み合わせた方が速いかもしれません。
  3. 大規模なデータセットを必要とされない限り作成しないでください。具体的には、次のように2行を削除します:ids = roi_feat_ids(scw(j, :), feat);

後者の2つは時間の節約になるかもしれませんが、最初は巨大な節約になるはずです。がんばろう!

+0

ご提案いただきありがとうございます。私はまだ完全にデバッグされていないこの機能のMEX版を実装しました。私はこの高速化されたコードとどのように比較するのかを見ていきます。乾杯! –

+0

Matlab MEXのシンプルで効率的な実装は、私のブログで利用できます:http://bit.ly/IgurHD –

関連する問題