Performance 基于图像ROI的高效直方图计算

Performance 基于图像ROI的高效直方图计算,performance,matlab,histogram,Performance,Matlab,Histogram,我正在寻找一些已经存在的功能/工具来计算图像中多个ROI(感兴趣区域)的标准视觉单词直方图。让我解释一下: (1) 假设您有一个图像,其中每个“像素”携带一个整数:1。。。K 每个这样的“像素”具有以下信息 x、 y坐标 从1到K的值 (2) 假设从所有格式的图像中采样了大量固定大小的区域: (x1,y1)-顶部,左坐标 (x2,y2)-底部,右坐标 (3) 对于每个区域:计算一个K-bin直方图,该直方图统计落在该区域的“像素”值的出现次数 我在MATLAB中实现了以下函数,但由于代码中有多个

我正在寻找一些已经存在的功能/工具来计算图像中多个ROI(感兴趣区域)的标准视觉单词直方图。让我解释一下:

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

  • x、 y坐标
  • 从1到K的值
  • (2) 假设从所有格式的图像中采样了大量固定大小的区域:

  • (x1,y1)-顶部,左坐标
  • (x2,y2)-底部,右坐标
  • (3) 对于每个区域:计算一个K-bin直方图,该直方图统计落在该区域的“像素”值的出现次数

    我在MATLAB中实现了以下函数,但由于代码中有多个for循环,因此速度非常慢

    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 );
    
    function[H words]=sph\u roi(风、树、描述、专长、箱子)
    %函数为一组窗口计算SPH直方图。空间的
    %通过将窗口水平拆分为多个存储箱来捕获信息。
    %
    %[H词]=sph_roi(风、树、描述、壮举,[bin]);
    %
    %输入:
    %风采样ROI窗口
    %[左x、上y、右x、下y]-参见示例roi()
    %树-词汇树
    %描述矩阵
    %专长-特征矩阵
    %箱子-水平单元格的数量(1=BOVW,2…SPH)
    %默认设置为窗高的倍数。
    %
    %输出:
    %H-SPH直方图
    %word-为每个描述符找到的单词ID
    %
    详细=0;
    %输入参数编号检查
    如果nargin<4
    错误('至少需要4个输入参数');
    结束
    %默认水平单元格数
    如果nargin<5
    箱子=-1;%将以每个窗口高度对应的倍数设置。
    结束
    %窗口数
    风的数量=大小(风,1);
    %视觉单词数
    num_words=tree.K;
    %预先计算所有可视单词
    words=vl_hikmeanspush(树,描述);
    %初始化SPH直方图矩阵
    H=零(字数*箱数,风数);
    %计算每个ROI的BOVW
    对于i=1:num\u风
    如果verbose==1
    fprintf('sph\u roi():处理%d/%d\n',i,num\u wind);
    结束
    %挑窗
    风i=风(i,:);
    %获取窗口的尺寸
    [w h]=风大小(风i);
    %如果未设置-水平料仓的数量
    如果箱子==-1
    料仓=圆形(w/h);
    结束
    %返回子单元窗口的列表
    scw=创建风(风、垃圾箱);
    对于j=1:料仓
    %选择一个单元
    风tmp=scw(j,:);
    %获取该单元格中的描述符ID
    ids=投资回报(roi)和专长(wind和tmp,专长);
    %计算当前单元格的BOVW直方图
    h=vl_hikmeanshist(树,单词(id));
    %直接在输出矩阵中组装SPH直方图
    H(1+(j-1)*num_字:j*num_字,i)=H(2:结束);
    结束
    结束
    函数ID=roi\u专长\u ID(w,f)
    %函数返回落在窗口中的要素ID。
    %
    %ids=投资回报率(w,f);
    %
    %输入:
    %w窗
    %f-所有特征点
    %
    %输出:
    %ids-功能ID
    %
    %输入参数编号检查
    如果nargin~=2
    错误('需要两个输入参数');
    结束
    左_x=1;
    top_y=2;
    右_x=3;
    底部y=4;
    %提取兴趣点坐标并将其舍入
    x=圆形(f(1,:);
    y=圆形(f(2,:);
    %连续绑定兴趣点
    s1=(x>w(左);%大于左_x
    s2=(xw(顶部y));%大于顶部y
    s4=(y

    我看过OpenCV甚至Intel的MKL中提出的例程,但没有找到合适的。使用Matlab的profiler,我发现在roi_feat_ids()中花费了相当多的时间,函数sph_roi()中每个区域的外循环也很慢。在尝试实现MEX文件之前,我想看看是否可以回收一些现有代码

    我要做几件事来加快速度

  • 最后一行应该被删除(
    ids=find(ids);
    。逻辑掩码比使用find快得多,并且它们几乎在find语句可以工作的所有情况下都能工作。我怀疑这将大大加快您的功能,同时不会损失功能性/可读性
  • 如果将一些s1、s2、s3和s4语句组合在一起,速度可能会更快
  • 尽量不要在for循环中创建大数据集,除非它们是必需的。具体来说,我将删除两行代码来执行以下操作:
    ids=roi\u feat\u ids(scw(j,:),feat);

  • 后两种方法可能会为您节省一些时间,但第一种方法应该可以节省大量的时间。祝您好运!

    为了加快速度,我会做一些事情

  • 最后一行应该被删除(
    ids=find(ids);
    。逻辑掩码比使用find快得多,并且它们几乎在find语句可以工作的所有情况下都能工作。我怀疑这将大大加快您的功能,同时不会损失功能性/可读性
  • 如果将一些s1、s2、s3和s4语句组合在一起,速度可能会更快
  • 尽量不要在for循环中创建大数据集,除非它们是必需的。具体来说,我将删除两行代码来执行以下操作:
    ids=roi\u feat\u ids(scw(j,:),feat);

  • 后两种方法可能会为您节省一些时间,但第一种方法应该可以节省大量时间。祝您好运!

    感谢您提供建议。我已经实现了(尚未完全调试)这个函数的MEX版本。我将看看它将如何与这个加速代码进行比较。干杯!在我的博客上有一个简单而高效的Matlab MEX实现:谢谢你提供的建议。我已经实现了(尚未完全调试)这个函数的MEX版本。我将看看它将如何与这个加速代码进行比较。干杯!一个简单而有效的