MATLAB Answers

Reduce allocated memory, speed up algorithm?, split matrix in "inequal" sized matrices?

2 views (last 30 days)
Andreas
Andreas on 17 Jan 2017
Answered: Prasad Mendu on 30 Jan 2017
Hello, I've the following task where I want to minimize the memory consumption (and also speed up). I've a matrix with rows of different height (varies about 1 pixel). Now I want to get about 80% of the center of the each block (which means ignoring the first 10% and last 10% of the block). The center of block is in the vector yc
I calculate the mean over the regions denoted with 1 in each block (I've about 50 blocks)
0000000
1111111
1111111
1111111
1111111
1111111
1111111
1111111
1111111
0000000
When the blocksize would be exactly the same, each time I could you use blockproc with the following code fragment
tic
row80centerproz = round(0.1*nPixOfRow):round(0.9*nPixOfRow);
mean80FilterFunction = @(theBlockStructure) mean(theBlockStructure.data(row80centerproz,:));
blockSize = [nPixOfRow 1];
RowMW = blockproc(Imagenew, blockSize, mean80FilterFunction,'PadPartialBlocks',true,'PadMethod', 'replicate');
t2 = toc;
but this would result in an increasing "shift" in the end I do not get the 80% in the middle, but 80% in the end or the beginning (depending if I create nPixOfRow with ceil or floor ) Here the peak and allocated memory is fine
at the moment I use
tic
for irow = 1:nrows
tmpSubImg = SubImg2(ImageIN,round([1 yc(irow)-halfHeight, ImSize(2), height]));
RowMW(irow,:) = mean(tmpSubImg);
end
t1 = toc;
function subimg=SubImg2(img,rect)
ylo = max([1 rect(2)]);
yhi = min([size(img,1) rect(2)+rect(4)-1]);
xlo = max([1 rect(1)]);
xhi = min([size(img,2) rect(1)+rect(3)-1]);
subimg = img(ylo:yhi,xlo:xhi,:);
end
There I've 21662772.00 Kb allocated memory
and a peak memory of 564400.00 Kb
Can I speed up the process with less memory allocation without using a loop, for example be reshaping the matrix into different sized matrix
tic RowMW = blockproc(Imagenew, blockSize, mean80FilterFunction,'PadPartialBlocks',true,'PadMethod', 'replicate'); t2 = toc;
Blockproc has an allocated memory of 540720.00 Kb and a peak memory of 528776.00 Kb ...
Is there a way to change my implemention to reduce the allocated memory (and even more to speed up the process?)
Thank you

Community Treasure Hunt

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

Start Hunting!