1

In order to optimize my code I did memory allocation but due to memory allocation I get the following error: The following error occurred converting from cell to double:

Error using double:

Conversion to double from cell is not possible:

patches(ind)= arrayfun(@(ind) reshape(neigh(:,ind),[3 1 3]), 1:numel(ind), 'uni', 0);%create cell arrays for each patch

The code is as follows:

M1=4;

N1=4;
patches=zeros(1,80000);

for i=1:nframes

Apad = padarray(I2(:,:,:,i), floor([M1 N1]/2), 'replicate');%To grab pixel neighbourhoods along the borders of the image padding is done

B =im2col_rgb(Apad,[3 3]);%sliding neighbourhood Im2col is used to transform each pixel neighbourhood into a single column. Each block is choosen on a column basis
%in our case a 3*3 neighbourhood is found for each pixel
%and stored in a column im2col_rgb is for color  images

for y=1:240

for x=1:320

for z=1:3

ind = sub2ind([size(I2(:,:,z,i),1) size(I2(:,:,z,i),2)], y, x);%x,y co-ordinates are converted to linear indices to easily select the neighbourhood

neigh = B(:,ind);%select neighbourhood

patches(ind)= arrayfun(@(ind) reshape(neigh(:,ind),[3 1 3]), 1:numel(ind), 'uni', 0);%create cell arrays for each patch

end

In the absence of memory allocation the code takes a really really long time (close to 3 hrs) to execute. So I need to make it as efficient as possible.

Dan
  • 45,079
  • 17
  • 88
  • 157
GPK
  • 11
  • 4
  • You can't put a cell array into a matrix of doubles. Debug and check what `arrayfun(@(ind) reshape(neigh(:,ind),[3 1 3]), 1:numel(ind), 'uni', 0)` is returning. If it's not a scalar double (and since you've added `'uni', 0` it clearly isn't, then you can't assign it to `patches(ind)`. Also please indent your code!!! – Dan Mar 31 '16 at 08:08
  • According to the answer given in http://stackoverflow.com/questions/27733477/how-do-i-efficiently-extract-image-patches-at-specified-locations , I guess this says that a cell array can be stored in a matrix. – GPK Mar 31 '16 at 08:29
  • If you want to do this many times then you'll need to use a cell array so `patches{ind}=...` which means you can't call `patches = zeros(...`. You can stuff the resulting cell array from your `arrayfun` line into a single element of a double array, dare I say it, obviously. Also, it's a pretty bad idea to have used `@(ind)` as the input variable name for your anonymous function which `ind` is already a variable name in your script. Since those two `ind`s will be totally different variables, this could become very confusing. Stick with `@(x)` like the original code. – Dan Mar 31 '16 at 08:36
  • Thank you very much for your detailed explanation :) – GPK Mar 31 '16 at 08:47
  • Oops, comment above should have read "You ***can't*** stuff the resulting..." – Dan Mar 31 '16 at 08:59
  • Is there any way to further increase the efficiency of my code? – GPK Mar 31 '16 at 11:02

0 Answers0