2011-08-15 5 views
2

그레이 스케일 200x200 이미지가 있고 이미지의 각 8x8 창에 대한 강도 막대 그래프를 계산하고 싶습니다. 그렇게 빨리 계산하려면 어떻게해야합니까? 나는 지금 루프를 사용하지만 너무 느리다. 당신은 이미지 처리 도구 상자가있는 경우그리드에서 히스토그램의 빠른 계산

I = imread('image.jpg'); 
for i=1:8:height-7 
    for j=1:8:width-7 
     patch = I(i:i+7,j:j+7); 
     % compute histogram for the patch 
    end 
end 

답변

5

당신이 당신의 루프의 컴파일 및 일반 버전 기능 blockproc를 사용할 수 있습니다처럼 내 현재 코드 보인다. 콜백 함수를 히스토그램 계산으로 정의하십시오.

B = blockproc(I, [8 8], @myhistfun) 
0

아래 코드는 귀하의 질문에 대한 답변입니다. 트릭은 루프 내에서 함수를 호출하지 않고 모든 배열을 사전 할당하는 것입니다. 예 : 루프 가속에 대한 자세한 내용은 http://www.quantiphile.com/2010/10/16/optimizing-matlab-code/을 참조하십시오. 어쨌든, 내 컴퓨터에서 가속 루프는 17 배 빠릅니다.

% image size 
height = 800; 
width = 1200; 
window = 8; 

% histogram bin centers 
bin_centers = 0.05:0.1:1; 

% here a random image as input 
img = rand(height, width); 

% verion using accelerated loops (for this to work there cannot be any 
% function calls to not built-in functions) 
tic 
img3 = zeros(window^2, height*width/window^2); 
ind = 1; 
for i=1:window:height 
    for j=1:window:width 
     patch_ = img(i:i+window-1,j:j+window-1); 
     img3(:,ind) = patch_(:); 
     ind = ind + 1; 
    end 
end 
hist_img3 = hist(img3, bin_centers); 
toc 


% probably version of user499372 calling hist function within the loop 
tic 
hist_img4 = zeros(size(hist_img3)); 
ind = 1; 
for i=1:window:height 
    for j=1:window:width 
     patch_ = img(i:i+window-1,j:j+window-1); 
     hist_img4(:,ind) = hist(patch_(:), bin_centers); 
     ind = ind + 1; 
     % compute histogram for the patch 
    end 
end 
toc 

% test the results 
all(all(hist_img3==hist_img4))