image processing with parallel computing gives incorrect results

2 views (last 30 days)
i need to read all images in a folder in groups of 3 and process them for that i am using a broadcast variable to store the names of all the images, i want to do it by using parallel computing but results are different from the results i get by using simple for loop. Here is the example code, any suggestion how can i get correct results?
inDir ='./abc/';
Imgs = dir([inDir '*.jpg']);
parfor indx=2:length(Imgs)-1
im1=imread([inDir Imgs(indx-1).name]);
im2=imread([inDir Imgs(indx).name]);
im3=imread([inDir Imgs(indx+1).name]);
% resultimg=someprocessing using im1,im2 and im3
% write resultimg
end

Answers (2)

Ahmet Cecen
Ahmet Cecen on 12 Nov 2016
I am not very knowledgeable with the implementation of ParFor, but I would wager a guess the problem is your indexing: specifically the presence of indx-1 and indx+1.
Try to see if creating three lists of images and indexing them all with indx fixes the problem. Something like:
Imgs = dir([inDir '*.jpg']);
ImgsBefore = circshift(Imgs,1);
ImgsAfter = circshift(Imgs,-1)
parfor indx=2:length(Imgs)-1
im1=imread([inDir ImgsBefore (indx).name]);
im2=imread([inDir Imgs(indx).name]);
im3=imread([inDir ImgsAfter (indx).name]);
% resultimg=someprocessing using im1,im2 and im3
% write resultimg
end
As I mentioned, this is simply a guess and might not work. Just come back and let it known if it doesn't.
  2 Comments
Maria Kanwal
Maria Kanwal on 13 Nov 2016
results are different than before but still do not match for loop results
Ahmet Cecen
Ahmet Cecen on 13 Nov 2016
I have tried a basic operation of this form (a triple convolution) and I get consistent results with for and parfor. I have no idea what else can be the problem.

Sign in to comment.


Walter Roberson
Walter Roberson on 13 Nov 2016
inDir ='./abc/';
Imgs = dir( fullfile(inDir, '*.jpg') );
numimg = length(Imgs);
parfor indx = 1 : numimg
im{indx} = imread( fullfile(inDir, Imgs(indx).name) );
end
parfor indx=2:length(Imgs)-1
im1 = im{indx-1};
im2 = im{indx};
im3 = im{indx+1};
% resultimg=someprocessing using im1,im2 and im3
% write resultimg
end
If the result is not the same as before then it might have to do with the processing you are doing.
  3 Comments
Walter Roberson
Walter Roberson on 13 Nov 2016
I think the differences you are observing have to do with the processing you are doing (which you did not show us.)

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!