Grouping connected components based on threshold

I am trying to use bwlabel

to group connected components and then combine them using some threshold distance. This is part of my code for the same -

L = bwlabel(image,8) ;%Calculating connected components
mx=max(max(L));
for i=1:mx
    [r1,c1] = find(L==i);
    rc1 = [r1 c1];
    maxr1=max(r1);
    minr1=min(r1);
    maxc1=max(c1);
    minc1=min(c1);

    for j=i+1:mx
        [r2,c2] = find(L==j);
        n1=zeros(imx,imy);
        rc2 = [r2 c2];
        maxr2=max(r2);
        minr2=min(r2);
        maxc2=max(c2);
        minc2=min(c2);
        if abs(maxc2-minc1) <=4 || abs(maxc2-maxc1) <=4 || abs(minc2-maxc1)<=4|| abs(minc1-maxc2)<=4 
            disp('Yes');
        end
    end
end

      

So after the if condition, instead, disp('Yes')

I want to group the components. If the components L==1

and L==2

satisfy this condition, I want to make the component L==2

as well as 1

. How can i do this?

+3


source to share


1 answer


Well, it can be done, but the complexity of the time will be enormous -

L = bwlabel(image,8) %Calculating connected components
mx=max(max(L));
for i=1:mx
    [r1,c1] = find(L==i);
    n1=zeros(imx,imy);
    rc1 = [r1 c1];
    maxr1=max(r1);
    minr1=min(r1);
    maxc1=max(c1);
    minc1=min(c1);

    for j=i+1:mx
        [r2,c2] = find(L==j);

        rc2 = [r2 c2];
        maxr2=max(r2);
        minr2=min(r2);
        maxc2=max(c2);
        minc2=min(c2);
        if abs(maxc2-minc1) <=4 | abs(maxc2-maxc1) <=4 | abs(minc2-maxc1)<=4 | abs(minc1-maxc2)<=4 
            disp('Yes');
            for k=minr2:maxr2
                for l=minc2:maxc2
                    if L(k,l)==j
                      L(k,l)=i;
                    end 
                end
            end

        end
        [r1,c1] = find(L==i);
        rc1 = [r1 c1];
        maxr1=max(r1);
        minr1=min(r1);
        maxc1=max(c1);
        minc1=min(c1);
    end
end

      



The time complexity for your algorithm will be O (N ^ 4), which is pretty huge.

+3


source







All Articles