2

I have image with black and white points(pixels). I need to create different sets for every different cluster of white pixels which contains x,y coordinates of that white point ( for example if I have black image with three not connected islands of white images I need to generate three sets with coordinates). Can anyone suggest me algorithm for this ?

Cells are connected if abs(x1-x2) <=1 && abs(y1-y2)<=1

Desmond Hume
  • 8,037
  • 14
  • 65
  • 112
Damir
  • 54,277
  • 94
  • 246
  • 365

4 Answers4

3

Connected-component labeling algorithms are intended to isolate and enumerate such clusters

MBo
  • 77,366
  • 5
  • 53
  • 86
2

Perhaps the flood filling algorithm.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

Region growing, which should do the trick. That link is answering a different question, but the basic algorithm should fit your needs just right. You just need to pass another argument, which tells the number of the cluster. Start with 1 and whenever you enter a new cluster, increment this value.

This new argument will take the place of the 1 for foreground and 2 for background. This will give you the total number of all clusters, as well as all their locations.

Community
  • 1
  • 1
Bill
  • 698
  • 1
  • 5
  • 22
0

This uses flood filling algorithm. to find the number of connected regions import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator;

import javax.xml.transform.Source;
import java.awt.*;
import java.awt.font.ImageGraphicAttribute;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;

/**
 * Created by IntelliJ IDEA.
 * User: ruzbeh
 * Date: 6/29/12
 * Time: 12:58 AM
 * To change this template use File | Settings | File Templates.
 */
public class Solution {


    static int[][] img;

    static void compLabel(int i, int j, int m,int  n) {
        if(i<0 || j<0 ||i > n-1||j > n-1) return;
        if (img[i][j] == 1) {
            img[i][j] = m;
            compLabel(i - 1, j - 1, m,n);
            compLabel(i - 1, j, m,n);
            compLabel(i - 1, j + 1, m,n);
            compLabel(i, j - 1, m,n);
            compLabel(i, j + 1, m,n);
            compLabel(i + 1, j - 1, m,n);
            compLabel(i + 1, j, m,n);
            compLabel(i + 1, j + 1, m,n);
        }
    }


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int T = Integer.parseInt(br.readLine());
        for (int t = 0; t < T; t++) {
            int n = Integer.parseInt(br.readLine());
            img = new int[n][n];
            int label = 2;
            for (int i = 0; i < n; i++) {
                int j = 0;
                for (String str : br.readLine().split(" ")) {
                    int value = Integer.parseInt(str);

                    img[i][j] = value;

                    j++;
                }

            }
            for (int y = 0; y < n; y++)
                for (int x = 0; x < n; x++)
                    if (img[x][y] == 1) {
                        compLabel(x, y, ++label,n);
                    }

            System.out.println(label - 2);
        }
    }
}
Ruzbeh Irani
  • 2,318
  • 18
  • 10