2012-04-10 70 views
1

我正在寻找一些现有的函数/工具来计算图像中多个ROI(感兴趣区域)的标准Bag视觉词直方图。让我来解释:从图像ROI获得有效的直方图计算

(1)假设你有其中每个 “象素” 携带整数图像:1 ...,K 每一个这样的 “像素” 具有以下信息

  1. 的x,y坐标
  2. 从值1到K

(2)假设固定大小区域的大量是从所有图像样本中的格式:

  1. (X1,Y1) - 顶部,左侧协调
  2. (x2,y2) - 底部,右侧坐标

(3)对于每一个区域:计算A 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. 尽量不要在for循环中创建大型数据集,除非它们是必需的。具体来说,我会删除两行来执行以下操作:ids = roi_feat_ids(scw(j, :), feat);

后两者可能会为你节省一点时间,但第一个应该是一个巨大的节省时间。祝你好运!

+0

感谢您提供您的建议。我已经实现(尚未完全调试)此功能的MEX版本。我会看到它将如何与这个加速代码进行比较。干杯! – 2012-04-11 08:38:09

+0

Matlab MEX中的一个简单高效的实现可以在我的博客上找到:http://bit.ly/IgurHD – 2012-04-11 11:15:07