image processing with parallel computing gives incorrect results

3 次查看(过去 30 天)
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 个)

Ahmet Cecen
Ahmet Cecen 2016-11-12
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 个评论
Maria Kanwal
Maria Kanwal 2016-11-13
results are different than before but still do not match for loop results
Ahmet Cecen
Ahmet Cecen 2016-11-13
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.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2016-11-13
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 个评论
Walter Roberson
Walter Roberson 2016-11-13
I think the differences you are observing have to do with the processing you are doing (which you did not show us.)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by