How to reduce the number of "for" loops (while implementing an overlapping block-wise processing on an image)?

1 回表示 (過去 30 日間)
I have the following code section to implement overlapping blocks in any square image (my image size 128*128). For each block, I would like to find HOG (Histogram of Oriented Gradients) features and then concatenate them at last to use it for classification.
The block size = 64
The step size = 16 (Both Horizontal & Vertical)
How can I implement a faster version of the following code section without using "for" loops? :
window_size=64;
step_size=16;
%the input image is stored in the variable "image"
for k=0:4
for n=0:4
for i=((step_size*n)+1):window_size+(step_size*n)
for j=((step_size*k)+1):window_size+(step_size*k)
img(p,q)=image(i,j); %get the block and store it in "img"
q=q+1;
end
p=p+1;q=1;
end
hog_block{k+1,n+1}=extractHOGFeatures(img); %storing each block's HOG features in a cell
p=1;
end
end
hog_blocks_mat=cell2mat(lbp); %convert to matrix
hog_vector=reshape(hog_blocks_mat',1,numel(hog_blocks_mat)); %convertto a row vector
Please help.

採用された回答

Jan
Jan 2017 年 1 月 13 日
These loops:
for i=((step_size*n)+1):window_size+(step_size*n)
for j=((step_size*k)+1):window_size+(step_size*k)
img(p,q)=image(i,j); %get the block and store it in "img"
q=q+1;
end
p=p+1;q=1;
end
can be rewritten to:
iIni = (step_size*n) + 1);
iFin = window_size+(step_size*n);
jIni = (step_size * k) + 1;
jFin = window_size + (step_size * k);
img = image(iIni:iFin, jIni:jFin);
  1 件のコメント
Tintumon
Tintumon 2017 年 1 月 14 日
Dear Jan Simon,
Thank you. This is exactly what I needed. It's working fine. :)

サインインしてコメントする。

その他の回答 (1 件)

Tohru Kikawada
Tohru Kikawada 2017 年 1 月 13 日
You can use blockproc for block processing. 'UseParallel' option enables to execute in parallel. See this link for details.
  2 件のコメント
Tintumon
Tintumon 2017 年 1 月 13 日
Dear Tohru Kikawada,
Thank you for the reply.
I have also used "blockproc" function. It is as follows:
fun = @(block_struct) extractHOGFeatures(block_struct.data);
hog_blocks_mat=blockproc(image,[window_size window_size],fun);
hog_vector=reshape(hog_blocks_mat,1,numel(hog_blocks_mat));
My question is: 1) By how much step (in horizontal and vertical direction) does the block move using "blockproc" function?
2) And, is there any way to change the step size in the function?
Note that I got an improvement in testing accuracy of 2%, while I used the code with the "for" loops, as compared to the "blockproc".
Image Analyst
Image Analyst 2017 年 1 月 14 日
Yes to both questions. It will move in steps/jumps of window_size. You can change the value of window_size. You can also move sliding (not full jumps). See the help.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeComputer Vision with Simulink についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by