How to effectively use bwlabel function as a tool to find clusters of a binary image
Show older comments
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:
- "image" is the binary 2D input
- X is an image after clustering "image" (8 connected pixels)
- 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
darova
on 19 Aug 2021
Attach the image. Show the result you expect
Image Analyst
on 19 Aug 2021
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().
AliHg
on 21 Aug 2021
AliHg
on 21 Aug 2021
Answers (0)
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!