I am looking for a way to extract patches of size m x n
from a matrix in Matlab by using GPU. The location of the patch extraction is determined by shifting a m x n
window for s
pixels horizontally and vertically.
Originally, I solved this problem by just using naive 2 nested for loops. However, it gets slow when I have a large image and small m
and n
. Because, this task has to be done multiple times, I want to increase the speed. I thought this process could be done in parallel, so I tried using parfor
to solve the problem. But, it gets even slower than the normal for loops method.
Right now I am trying to use GPU for helping me in this task. But, currently I have now idea how to implement it. I have checked arrayfun
, but it seems that it only support for element-wise calculation.
So, is it possible to use GPU to help solving the problem? It seems like it could be done in parallel.
EDIT: an example to the problem
Let's say I have a 3 x 3
matrix (I will write the matrix in MATLAB format).
`
A = [1 2 3
4 5 6
7 8 9];
`
I want to extract patches with size of 2 x 2
. And I want the each patches are generated by 1 pixel shift. So, the result should be
`p1 = [1 2; 4 5]; p2 = [2 3; 5 6]; p3 = [4 5; 7 8]; p4 = [5 6; 8 9];`
And here is an example of what I did in the for loops method.
A = magic(3);
patchSize = [2 2];
shift = 1;
finalI = floor((size(A,1) - patchSize(1)) / shift);
finalJ = floor((size(A,2) - patchSize(2)) / shift);
patch = cell(finalI,finalJ);
for i = 0:finalI
for j = 0:finalJ
patch{i+1,j+1} = A(1+i*shift : patchSize(1)+i*shift, 1+j*shift : patchSize(2)+j*shift);
end
end