How to speed up this for-loop?

I would like to calculate the maximum number of translated images along the direction of a given axis. I know about ordfilt2 , however I would like to avoid using the Image Processing Toolbox.

So, here is the code that I still have:

 imInput = imread('tire.tif'); n = 10; imMax = imInput(:, n:end); for i = 1:(n-1) imMax = max(imMax, imInput(:, i:end-(ni))); end 

Is it possible to avoid using a for loop to speed up the calculation and, if so, how?


The first edit . Using Octave code for im2col is actually 50% slower.

Second edit : preliminary highlighting did not improve the result.

 sz = [size(imInput,1), size(imInput,2)-n+1]; range_j = 1:size(imInput, 2)-sz(2)+1; range_i = 1:size(imInput, 1)-sz(1)+1; B = zeros(prod(sz), length(range_j)*length(range_i)); counter = 0; for j = range_j % left to right for i = range_i % up to bottom counter = counter + 1; v = imInput(i:i+sz(1)-1, j:j+sz(2)-1); B(:, counter) = v(:); end end imMax = reshape(max(B, [], 2), sz); 

Third edit . I will show the timings.

+4
source share
2 answers

See the answer to this question on how to make a moving median in c. I have successfully turned it into a mex function, and it is much faster than even ordfilt2. It takes a bit of work, but I'm sure it is possible.

Rolling Median in C Implementation - Turlach

+2
source

What is the vector solution for using the IM2COL function from the Image Processing Toolbox?

 imInput = imread('tire.tif'); n = 10; sz = [size(imInput,1) size(imInput,2)-n+1]; imMax = reshape(max(im2col(imInput, sz, 'sliding'),[],2), sz); imshow(imMax) 

screenshot

Perhaps you can write your own version of IM2COL, as it simply consists of well-crafted indexing, or even considers how Octave implements it.

+3
source

All Articles