image processing with parallel computing gives incorrect results

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

回答(2 个)

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 个评论

results are different than before but still do not match for loop results
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.

请先登录,再进行评论。

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 个评论

having all frames in memory is not feasible for me, number of images are large that's why i wanted to use parallel computing
I think the differences you are observing have to do with the processing you are doing (which you did not show us.)

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by