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

조회 수: 9 (최근 30일)
AliHg
AliHg 2021년 8월 19일
댓글: AliHg 2021년 8월 21일
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
AliHg
AliHg 2021년 8월 21일
Dear darova,
You may find the input image (sample7.jpg), and the output of the above function (Result), in the attachments.
Thanks
AliHg
AliHg 2021년 8월 21일
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

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by