0

I am trying to implement the 8 connectivity connected components labelling algorithm over an image where certain points are highlighted to identify regions of connected highlighted points.

My code seems to be working except that the amount of objects is massively overestimated, for example in an image where 500 points are highlighted it might say that there are 2000 objects.

My code so far (for the first pass of the algorithm) is:

    public int[][] connectedComp(int[][] array){     // Connected Compoent Labling method
            int nextlabel = 1;    
            for(int v =y1; v<y1 + height;v++){
                for(int u = x1;u<x1 + width;u++){
                    if(array[v][u]==1){
                        for (int j=-1; j<=1; j++){
                            for (int i=-1; i<=1; i++){
                                if(v+j>=0 && u+i>=0){
                                if(array[v+j][u+i]>0){

                                    array[v][u]= nextlabel;
                                }
                                else{
                                    array[v][u] = nextlabel++;
                                }
                                }
                            }
                        }    
                    }
                }
            }
            return array;
}

X1 and Y1 are the starting coordinates of the ROI I am interested in and width and height are the dimensions of said ROI.

Can anyone see what might be causing this?

Edit

I changed my code to only compare the pixels I had already visited and also changed the structure slightly. It works better now, if not perfectly.

Here is the updated code:

public int[][] connectedComp(int[][] array){     // Connected Compoent Labling method
            int nextlabel = 1;    
            for(int v =y1; v<y1 + height;v++){
                for(int u = x1;u<x1 + width;u++){
                    if(array[v][u]==1){
                        if(v-1>=0 && u-1>=0){
                            if(array[v-1][u-1]!=0){
                                array[v][u]= nextlabel;
                            }
                            if(array[v-1][u]!=0){
                                array[v][u]= nextlabel;
                            }
                            if(array[v-1][u+1]!=0){
                                array[v][u]= nextlabel;
                            }
                            if(array[v][u-1]!=0){
                                array[v][u]= nextlabel;
                            }
                            else{
                                array[v][u] = nextlabel++; 
                            }
                        }
                    }
                }                
            }
            return array;
}
MattG
  • 103
  • 3

1 Answers1

0

I think your problem is in your inner loop.

Lets say you have a single pixel that is surrounded by zeros

for (int j=-1; j<=1; j++)
{
   for (int i=-1; i<=1; i++)
   {
      if(v+j>=0 && u+i>=0)
      {
         if(array[v+j][u+i]>0)
         {
            array[v][u]= nextlabel;
         }
         else{
            array[v][u] = nextlabel++;
         }
      }
   }
}

for all of you inner loop you will keep increasing the label because the pixel is surrounded by zeros. So if nextlabel = someValue in the begining of the loop in the end it will be equal to someValue + 9.

Amitay Nachmani
  • 3,259
  • 1
  • 18
  • 21
  • I think thats it, thank you, how would I go about making sure the value of nextlabel only increased by 1 every time a pixel is surrounded by 0's? – MattG Nov 21 '16 at 14:12
  • I don't know which algorithm for connected component labeling you are trying to implement (One pass or Two pass(https://en.wikipedia.org/wiki/Connected-component_labeling#Two-pass) or something i don't familiar with). But for two pass algorithm for example in the first pass you don;t need to look at all the neighbors around you but only on the ones that you already visited. The ones above the current pixel and the one to the left – Amitay Nachmani Nov 21 '16 at 16:46
  • I am trying to implement the two pass method but the code I posted was my attempt at the first pass, I've changed it a bit and think I may have solved the problem. I'll post my updated code as an edit. Thanks for the help! – MattG Nov 21 '16 at 17:11