Parfor for tossing a line on a binary image

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 commentaires

I don't know what 'tossing a line' mean. Do you mean draw?
I also don't understand the purpose of these lines:
top=Img1(picked_row,picked_col);
bottom=Img1(picked_row+i,picked_col);
Temp(j)=top*bottom;
This picks two pixels in the same column and just multiply their value. If as you say the image is binary, the result is either 0 or 1.
And why is the maximum length of a line half the biggest dimension of the image? If the lines are vertical, isn't the max. length the height of the image?
ramin bba
ramin bba le 14 Déc 2014
Modifié(e) : ramin bba le 14 Déc 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 le 14 Déc 2014
Yes, it makes a lot more sense now.
I believe Image Analyst has answered your question.

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 14 Déc 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 commentaires

ramin bba
ramin bba le 14 Déc 2014
tnx. the downside of generating the random numbers outside the parfor loop, however, is that I run out of RAM!
Image Analyst
Image Analyst le 14 Déc 2014
How many numbers are you generating!?!?
ramin bba
ramin bba le 14 Déc 2014
Itr is set to 10^8. so, if I generate the random numbers all at once and store them, I will run into memory issue.
Image Analyst
Image Analyst le 15 Déc 2014
I really doubt you need to do that many. Try it with 10,000 or 100,000 and see how much they differ from each other. I doubt the results will be much different. And I doubt 100 million lines will give you any practical difference than 100 thousand lines. I bet it's just way extreme overkill. You will get the same numbers with far far fewer lines.
ramin bba
ramin bba le 15 Déc 2014
You are correct. Here are the results for 10^5 and 10^7. My adviser prefers the latter though :(
Image Analyst
Image Analyst le 15 Déc 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 le 15 Déc 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by