2012-04-10 2 views
1

이미지의 여러 ROI (관심 영역)에서 표준 단어 가방 오브 히스토그램을 계산하기 위해 이미 기존의 함수/도구를 찾고 있습니다. 설명해 드리죠 :이미지 ROI에서 효율적인 히스토그램 계산

(1) 각 "픽셀"정수를 전달하는 이미지가 있다고 가정 : 1 ... K 이러한 각각의 "픽셀"정보를

  1. X를 다음있다, y는
  2. 좌표 모든 형식에서 이미지 샘플
  3. 1 값이

(2) 고정 된 크기의 영역을 다량 K 가정한다 :

    ,174 51,515,
  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 's MKL 그러나 적절한 것은 발견되지 않았다. Matlab의 프로파일 러를 사용하여 roi_feat_ids()에서 상당한 시간을 보냈고 sph_roi() 함수의 각 영역에서 바깥 루프가 너무 느리다는 것을 알게되었습니다. MEX 파일을 구현하기 전에 기존 코드를 재활용 할 수 있는지 확인하고 싶습니다.

답변

1

속도를 높이려면 몇 가지 방법이 있습니다. , 거의 찾기 문이 일하는 것이 모든 경우. 나는이 상당히 함수를 가속화 할 생각

매우 마지막 줄을 제거해야
  1. (ids = find(ids);. 논리적 마스크는 찾기를 사용하는 것보다 훨씬 더 빨리, 그리고 그들이에서 작동 기능/가독성을 잃지 않고
  2. s1, s2, s3 및 s4 문 중 일부를 결합하면 더 빠를 수 있습니다. 필요하지 않으면 for 루프에서 큰 데이터 집합을 만들지 마십시오. 특히 다음을 수행하기 위해 두 줄을 제거합니다. ids = roi_feat_ids(scw(j, :), feat);

후자의 두 가지 기능을 사용하면 시간을 절약 할 수 있지만 처음에는 시간을 크게 절약해야합니다. 행운을 빕니다!

+0

제안 해 주셔서 감사합니다. 이 기능의 MEX 버전을 구현했습니다 (아직 완전히 디버깅하지는 않았습니다). 나는 이것이이 가속화 된 코드와 어떻게 비교되는지 보게 될 것이다. 건배! –

+0

Matlab MEX에서 간단하고 효율적인 구현은 내 블로그에서 볼 수 있습니다. http://bit.ly/IgurHD –

관련 문제