using parfor loop for marking the image/advice on other method for marking image

Hello and good day,
im still new with using Parallel Computing Toolbox so im having a problem to apply parfor loop for marking the image. below is the codes that bottleneck my algorithm for majority of the time taken to process.
imshow(image);
axis on %this will show you the axis ticks; note the y axis is reversed!
hold on
for Y=1:length(Index)
sz = [XImage YImage];
[row,col] = ind2sub(sz,Index(Y));
Result=textureAnalysis(Index(Y),image,ClassificationFunc);
if Result =="Yes"
plot(col,row,'y.', 'MarkerSize', 10);
else
plot(col,row,'bx', 'MarkerSize', 10);
end
disp([Y,length(Index)]);%tracking progress of marking image
end
What the codes should happened:
this function will run and process each index for classification, if it is yes, mark it with yellow dot,and vice versa with blue 'X'. The image shown later will show yellow dot's and Blue 'X' in the image.
what the codes process happened:
the parfor loop works but however the image doesnt contain any of the marking at all.
i think i understand the reason why parfor loop wont work with marking image. it is due to using a graph plot on the image.my Index is averaging to 10,000 iteration thus the for loop is a long process . im pretty sure by doing parfor loop will substantially reduce the process time.
i wonder, did i have misunderstanding of how parfor loops works? or graph plot method arent possible for parfor loop?
i would to request help for current method and if it is not possible,an advice of any other method to mark the image without manipulating the real image(masking) that can be used with parfor loop or something similiar would be very appreciated.

 Accepted Answer

The issue is that your plotting in the parfor loop, which has no display. Create a DataQueue and pipe the data back to the client MATLAB. For example (I've marked what I've added with % NEW)
imshow(image);
axis on %this will show you the axis ticks; note the y axis is reversed!
hold on
D = parallel.pool.DataQueue; % NEW
afterEach(D,@(data)lUpdatePlot(data)); % NEW
for Y=1:length(Index)
sz = [XImage YImage];
[row,col] = ind2sub(sz,Index(Y));
Result=textureAnalysis(Index(Y),image,ClassificationFunc);
if Result =="Yes"
% plot(col,row,'y.', 'MarkerSize', 10); % NEW
marker = 'y.'; % NEW
else
% plot(col,row,'bx', 'MarkerSize', 10); % NEW
marker = 'bx'; % NEW
end
send(D,{col row marker}) % NEW
disp([Y,length(Index)]);%tracking progress of marking image
end
function lUpdatePlot(data)
plot(data{1},data{2},data{3},'MarkerSize',10);
end

3 Comments

Sorry for the very late reply, i wasnt able to test it out as im not near my own computer to test the code.i just arrive back and test it out and it worked
Before=~150 +-1sec
After= ~60 +-1sec
im gratefull for the fast reply to my questions and it worked fine with my current codes. i have a few questions to understand the codes you suggest
i would like to ask for confirmation, with your addition of codes, it means the codes work as follows:
D = parallel.pool.DataQueue; % create a variable to collect data within a process
afterEach(D,@(data)lUpdatePlot(data)); % call both data collected and variable
send(D,{col row marker}) % collect the data and send back to aftereach
function lUpdatePlot(data)
plot(data{1},data{2},data{3},'MarkerSize',10); %plot data on image after obtained 3 data? i dont quite understand data{1},data{2},data{3}
end
this is first time i heard of "parallel.pool.DataQueue" and "afterEach" function. im still reading the documentation and trying out.
im just dont quite understand the relation of lUpdatePlot(data)
Here are my comments
D = parallel.pool.DataQueue; % create DataQueue to push data from worker to client
afterEach(D,@(data)lUpdatePlot(data)); % assign afterEach method the function pointer to lPlotData
send(D,{col row marker}) % send col, row, and marker back to client via DataQueue
function lUpdatePlot(data)
col = data{1}; rol = data{2}; marker = data{3};
plot(col,rol,marker,'MarkerSize',10); % after each iteration, plot col, row, with marker style
end
lUpdatePlot is responsible for updating the figure on the client (MATLAB instance) after each parfor iteration is complete.
ahh
i understand it now
Thank you again for explaining your codes suggestion

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!