Parfor for tossing a line on a binary image

1 view (last 30 days)
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 Comments
ramin bba
ramin bba on 14 Dec 2014
Edited: ramin bba on 14 Dec 2014
thanks for the comment. I did not give much info about the problem so as not to make it too long. Here are the answers to your questions:
"tossing a line (with random length) on an image (in a random manner) and finding the probability of having the two ends land on the same phase (both on 0 or both on 1)" is a well-known (computational) characterization method in materials science and is known as "2-point correlation function".
I am interested in finding the probability of the two ends being in phase one (represented by 1 in a binary image) and hence the 3 lines you mentioned. Also, if the length of the line is know, I only need the position of one of its points (for example the top) to know where exactly it is thrown on the image.
The correlation between the material constituents usually dies out way before the half size of the image and hence I considered that as the maxi. length of the tossed line. Also, the inner loop corresponds to Monte Carlo sampling.
last but not least, this material is anisotropic and hence behaves different in different directions. I am interested in how it behaves in the vertical direction and as such all the line that I throw on the image are vertical.
Hope I answered all of your questions.
Regards,
Ramin
Guillaume
Guillaume on 14 Dec 2014
Yes, it makes a lot more sense now.
I believe Image Analyst has answered your question.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 14 Dec 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 Comments
Image Analyst
Image Analyst on 15 Dec 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.
ramin bba
ramin bba on 15 Dec 2014
the X axis is the length of the tossed line and the y axis is the variable DATA calculated above. The correlation dies out after a certain length as you mentioned.
Thanks a lot for the thorough comments.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!