Parfor for tossing a line on a binary image
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
ramin bba
am 13 Dez. 2014
Kommentiert: ramin bba
am 15 Dez. 2014
Is there a way to enhance the following code (the parallel loop)? If I increase the number of iterations (Itr) it takes a long time to run. In the code, I basically want to toss a line of random length on a binary image in a random fashion. The line has to be vertical.
% code
function [DATA]=Directional_S2(Img1)
[M,N]=size(Img1);
Max_L=floor(max(M,N)/2)+1; %maximum length of the line
Itr=10^4; %number of randomly tossed lines
DATA=zeros(Max_L+1,1);
Temp=zeros(Itr,1);
parpool('local',16)
for i=0:Max_L
disp(i)
parfor j=1:Itr %pick a column randomly and then toss the top point of the line on it randomly
picked_col=randi(N,1);
picked_row=randi([1,M-i],1); %position of the top of the line
top=Img1(picked_row,picked_col);
bottom=Img1(picked_row+i,picked_col);
Temp(j)=top*bottom;
end
DATA(i+1)=mean(Temp);
Temp=0*Temp;
end
delete(gcp('nocreate'))
3 Kommentare
Guillaume
am 14 Dez. 2014
Yes, it makes a lot more sense now.
I believe Image Analyst has answered your question.
Akzeptierte Antwort
Image Analyst
am 14 Dez. 2014
You can define picked_row and picked_column outside the j loop so that you get the values for all j in one shot before the loop even starts. I don't have the parallel Toolbox but when they were inside the loop, for a 256x256 image, my time was 14 seconds and when I pulled them out it went way down to 0.16 seconds. You can make it even faster by doing logical operations and getting rid of the call to disp().
for i=0:Max_L
disp(i)
picked_col=randi(N,1, Itr);
picked_row=randi([1,M-i], 1, Itr); %position of the top of the line
for j=1:Itr %pick a column randomly and then toss the top point of the line on it randomly
% picked_col=randi(N,1);
% picked_row=randi([1,M-i],1); %position of the top of the line
top=Img1(picked_row(j),picked_col(j));
bottom=Img1(picked_row(j)+i,picked_col(j));
Temp(j)= top & bottom;
end
DATA(i+1)=mean(Temp);
Temp=0*Temp;
end
toc
7 Kommentare
Image Analyst
am 15 Dez. 2014
I don't know what those axes represent but it looks like after about 15, the curve is about the same, just bounding around within a narrow noise range.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!