How to effectively use bwlabel function as a tool to find clusters of a binary image

Hello
I have a binary 2D image (logical). My goal is to calculate the probability of finding the beginning and ending points of random vectors in the same cluster. To do this task, one has to employ periodic boundary conditions because if he/she doesn't, some of the random vectors at the edges might fall outside of the 2D image. In other words, three images of the same size as the initial image should be positioned to the buttom, right, and diagonal sides.
Right now, the best way I can implement this is to use the following piece of code:
function Result = func (image)
X = bwlabel(image,8);
s = size(X);
RepSol = repmat(X,2,2);
image = repmat(image,2,2);
% Preallocation
p = zeros(s);
for m = 1:s(1)
for n = 1:s(1)
for i = 1:s(1)
for j = 1:s(1)
if RepSol(i,j) == RepSol(m+i-1,n+j-1)
p(m,n) = p(m,n) + image(i,j) * image(m+i-1,n+j-1);
end
end
end
end
end
Result = p / numel(X);
end
In this function:
  1. "image" is the binary 2D input
  2. X is an image after clustering "image" (8 connected pixels)
  3. RepSol is the periodic version of X
Any help regarding reducing the runtime of this code would be appreciated. This version of my code is really, really slow, and the algorithm I am working with calls this function more than 1e9 times, so I guess you can imagine the massive computational cost.
Thanks

4 Comments

Attach the image. Show the result you expect
Don't call your variable image because that's the name of a built-in function. Call it something else, like grayImage or binaryImage or whatever.
You don't need a periodic RepSol image to do what you want to do, at least not according to the word description you gave.
I could fix the code but you removed all the comments before posted the code. I'm sure you put them in like all professional programmers do, but when you removed them you made it very difficult to follow. For example, I don't see where the location of the random endpoints of the vectors is being determined. Put the comments back in, especially where you get the random endpoint locations. I'm looking for a call to rand() or randi().
Dear darova,
You may find the input image (sample7.jpg), and the output of the above function (Result), in the attachments.
Thanks
Dear Image Analyst,
You are right about the variable names, thanks for the tip.
In this version of code that I have written for calculating this type of function, we need to tackle outside-of-the-box vectors by considering periodic boundary conditions.
If I understand you correctly, what you are saying is that I have removed a couple of functions in the above code, right? If that's the case, I have to say no, that piece of code works fine by itself, and there's no need for rand or randi functions. The nested for loop and the if statement will do the job. Just copy and paste the code and import a binary image, then you can see starting and ending points of random vectors will be evaluated automatically.
Thank you

Sign in to comment.

Answers (0)

Asked:

on 19 Aug 2021

Commented:

on 21 Aug 2021

Community Treasure Hunt

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

Start Hunting!