How to effectively use bwlabel function as a tool to find clusters of a binary image
조회 수: 9 (최근 30일)
이전 댓글 표시
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
답변 (0개)
참고 항목
카테고리
Help Center 및 File 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!