Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6861741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:34:25+00:00 2026-05-27T02:34:25+00:00

I am working on Connected Components labeling, and my matrix is: 1 1 0

  • 0

I am working on Connected Components labeling, and my matrix is:

 1     1     0     2     2     2     0     3
 1     1     0     2     0     2     0     3
 1     1     1     1     0     0     0     3
 0     0     0     0     0     0     0     3
 4     4     4     4     0     5     0     3
 0     0     0     4     0     5     0     3
 6     6     0     4     0     0     0     3
 6     6     0     4     0     7     7     7

and now, i want to do the second scan over it, for this i have made following code:

for i=1:1:r
    for j=1:1:c
        if (bw(i,j)>=1) 

            if (i-1>0 & i+1<=r) 
                % if ( bw(i,j)~= bw(i-1,j) | bw(i,j)~= bw(i+1,j))
                if ( (bw(i,j)~= bw(i-1,j) & bw(i-1,j)>0))
                    bw(i,j)= min (bw(i-1,j),bw(i,j))
                elseif ((bw(i,j)~= bw(i+1,j) & bw(i+1,j)>0))
                    bw(i,j) = min(bw(i+1,j),bw(i,j));
                end
            end

            if (j-1>0 & j+1<=c) 
                if ( (bw(i,j)~= bw(i,j-1) & bw(i,j-1)>0))
                    bw(i,j) = min (bw(i,j-1),bw(i,j));
                elseif((bw(i,j)~= bw(i,j+1) & bw(i,j+1)>0))
                    bw(i,j) = min (bw(i,j+1),bw(i,j))  ; 
                end
            end

        end
    end
end
disp(bw);

but the problem is, when i run this code, i get the following output

 1     1     0     2     2     2     0     3
 1     1     0     1     0     2     0     3
 1     1     1     1     0     0     0     3
 0     0     0     0     0     0     0     3
 4     4     4     4     0     5     0     3
 0     0     0     4     0     5     0     3
 6     6     0     4     0     0     0     3
 6     6     0     4     0     7     7     7

only one value changes (2nd row, 4 col) in my result, whereas, I want:

     1     1     0     1     1     1     0     3
     1     1     0     1     0     1     0     3
     1     1     1     1     0     0     0     3
     0     0     0     0     0     0     0     3
     4     4     4     4     0     5     0     3
     0     0     0     4     0     5     0     3
     6     6     0     4     0     0     0     3
     6     6     0     4     0     3     3     3

could somebody please help? where am i making a mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:34:26+00:00Added an answer on May 27, 2026 at 2:34 am

    Nice solution by Jonas. As I was a bit into coding when I saw it added, I thought I’d show you my solution as well. Hopefully it is a bit more similar to your original code.

    One of the mistakes you made was assuming this connection could be done in just one pass. Generaly it can not, as detections of some of the “snake elements” are dependent on the way you iterate throught the matrix. Becuase of this, I added an outer while loop.

     bw =[  1     1     0     2     2     2     0     3;
            1     1     0     2     0     2     0     3;
            1     1     1     1     0     0     0     3;
            0     0     0     0     0     0     0     3;
            4     4     4     4     0     5     0     3;
            0     0     0     4     0     5     0     3;
            6     6     0     4     0     0     0     3;
            6     6     0     4     0     7     7     7 ];
    
    
     %Set up matrix   
     [r,c] = size(bw);   
    
     %Zero pad border
     bwZ = [zeros(1,c+2);[zeros(r,1) bw zeros(r,1)];zeros(1,c+2)];
    
     %Iterate over all elements within zero padded border
     done=0;%Done flag 
     cc=1;  %Infinite loop protection
     while not(done) && cc<r*c
         done=1;cc=cc+1;
         for i=2:r+1
             for j=2:c+1
                %Point should be evaluated
                p = bwZ(i,j);
                if p >= 1
                    %Pick out elements around active elements
                    if bwZ(i-1,j)==0;ue=inf;else ue = bwZ(i-1,j); end;%Up element
                    if bwZ(i+1,j)==0;de=inf;else de = bwZ(i+1,j); end;%Down element
                    if bwZ(i,j-1)==0;le=inf;else le = bwZ(i,j-1); end;%Left element
                    if bwZ(i,j+1)==0;re=inf;else re = bwZ(i,j+1); end;%Right element
    
                    bwZ(i,j) = min([ue de le re]);
                    %Set flag, if something has changed 
                   if bwZ(i,j) ~= p
                        done = 0;
                   end
                end
             end
         end
     end
    
     %Remove zero padding
     bw = bwZ(2:end-1,2:end-1)
    

    Output:

    bw =

     1     1     0     1     1     1     0     3
     1     1     0     1     0     1     0     3
     1     1     1     1     0     0     0     3
     0     0     0     0     0     0     0     3
     4     4     4     4     0     5     0     3
     0     0     0     4     0     5     0     3
     6     6     0     4     0     0     0     3
     6     6     0     4     0     3     3     3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working connected components analysis code working in C. It's actually a
i am working on VC#.net i have connected my combox with my database when
I am working on a interface in java swing.we have four system connected with
So I have been doing this awhile now and have run into a really
I'm working on a project that has components, components have features, features have bugs.
I have a client/server program and I want to make sure that its components
I have Red5 oflaDemo Connected and working fine When I try to rtmp stream
I'm currently working on a Silverlight application connected to a SQL Server 2008 Express
We're working on a rich client (written in Flex) that is connected to a
I am working with a set of data that I have converted to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.