Parfor for tossing a line on a binary image

1 visualización (últimos 30 días)
ramin bba
ramin bba el 13 de Dic. de 2014
Comentada: ramin bba el 15 de Dic. de 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 comentarios
ramin bba
ramin bba el 14 de Dic. de 2014
Editada: ramin bba el 14 de Dic. de 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 el 14 de Dic. de 2014
Yes, it makes a lot more sense now.
I believe Image Analyst has answered your question.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 14 de Dic. de 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 comentarios
Image Analyst
Image Analyst el 15 de Dic. de 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 el 15 de Dic. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by