1
votes

I have several gray-scale images and I want to store that in a 3d array(hieghtXwidthXnumber-of-images) in matlab.

my code looks like this

train_img = [];
parfor i=1:100
      a = imread(image-file);
      a1 = imresize(a, 0.5);
      b = rgb2gray(a1);
      d = im2double(b);
      train_label = [train_label;p];
      train_img = cat(3,train_img(:,:,:),d);
end

Error: The temporary variable train_img in a parfor is uninitialized. See Parallel for Loops in MATLAB, "Uninitialized Temporaries".

In the above code the parfor i=1:100, I don't know whats the upper limit of loop. Its decided at run time. Could anybody let me know what this error means and how to overcome this?

1

1 Answers

0
votes

You write train_img on the left as well as the right hand side of an expression. Therefore, next iteration depends on the previous value of train_img. This prevents MATALB from employing parfor. If you modify the code as follows, it should work:

parfor i=1:your_upper_bound %it can be a variable too, just define it
      a = imread(image_file);
      a1 = imresize(a, 0.5);
      b = rgb2gray(a1);
      d = im2double(b);
      train_label(i,1) = p; % if this doesn't work -> define train_label before parfor
      train_img(:,:,i) = d; % if this doesn't work -> define train_img before parfor
end