简体   繁体   中英

Connected Component Labeling in Java

I'm trying to implement a method that will find the number of groups of a given size. The input is an n-by-n two-dimensional array, where each cell has a value of either 0 or 1. Cells are considered to be in the same group if they are horizontally or vertically adjacent (not diagonal) and both have a value of 1.
The group size is the number of cells in that group. Along with the two dimensional array, an array of integers "t" is also passed. For each integer t[i], the function should determine the number of groups with size equal to that value. That information is returned in the integer array "answer".
I have attempted to solve this problem, but am falling short of passing all of the provided test cases, of which I cannot see the input or output. I have tried numerous custom test cases, but have not identified any issues. Are there any scenarios in which my function does not behave correctly?

static int[] countGroups(int[][] m, int[] t) {

    int[] answer;
    answer = new int[t.length];
    Arrays.fill(answer, 0);
    int[] sizes;
    int length = m.length;
    sizes = new int[(length * length)];
    Arrays.fill(sizes, 0);
    int[] subList;
    subList = new int[length * length];
    Arrays.fill(subList, 0);
    int groupID = 2;

    for(int j = 0; j < length; j++){
        for(int k = 0; k < length; k++){
            if(m[j][k] > 0){
                if((j != 0) && (k != 0)){
                    if((m[j-1][k] > 0) && (m[j][k-1] > 0)){
                        if(m[j-1][k] != m[j][k-1]){
                            if(m[j-1][k] < m[j][k-1]){
                                subList[(subList[(m[j][k-1])])] = m[j-1][k];
                                subList[(m[j][k-1])] = m[j-1][k];
                                m[j][k] = m[j-1][k];
                            }
                            else{
                                subList[(subList[(m[j-1][k])])] = m[j][k-1];
                                subList[(m[j-1][k])] = m[j][k-1];
                                m[j][k] = m[j][k-1];
                            }
                        }
                        else{
                            m[j][k] = m[j-1][k];
                        }
                    }
                    else if(m[j][k-1] > 0){
                        m[j][k] = m[j][k-1];
                    }
                    else if(m[j-1][k] > 0){
                        m[j][k] = m[j-1][k];
                    }
                    else{
                        groupID++;
                        m[j][k] = groupID;
                    }
                }
                else if((j == 0) && (k == 0)){
                    m[j][k] = groupID;
                }
                else if(k != 0){
                    if(m[j][k-1] > 0){
                        m[j][k] = m[j][k - 1];
                    }
                    else{
                        groupID++;
                        m[j][k] = groupID;
                    }
                }
                else if(k == 0){
                    if(m[j-1][k] > 0){
                        m[j][k] = m[j-1][k];
                    }
                    else{
                        groupID++;
                        m[j][k] = groupID;
                    }

                }
            }
            else{
            }
        }
    }
    for(int j = 0; j < length; j++){
        for(int k = 0; k < length; k++){
            if(m[j][k] > 0){
                if(subList[(m[j][k])] > 0){
                    m[j][k] = subList[(m[j][k])];
                }
            }
        }
    }
    for(int j = 0; j < length; j++){
        for(int k = 0; k < length; k++){
            if(m[j][k] > 0){
                sizes[(m[j][k])]++;
            }
        }
    }
    for(int x = 0; x < t.length; x++){
        for(int n = 0; n < sizes.length; n++){
            if(sizes[n] == t[x]){
                answer[x]++;
            }
        }
    }
    return answer;
}

The problem split in two parts:

1) identify groups

2) calculate the size of each group

problem 1) requires a stabiliser algorithm:

  • give each cell a unique value
  • for every pair of adjacent cells, change the one with highest value to the (lower) value of the adjacent cell
  • repeat the above until no more changes occur

problem 2 then becomes fairly simple, its just a frequency table of all the occurring values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM