2012-08-08 49 views
2

我具有黑白点(像素)的图像。我需要为包含该白点的x,y坐标的每个不同的白色像素簇创建不同的集合(例如,如果我有三个未连接的白色图像岛的黑色图像,我需要生成三组坐标)。任何人都可以建议我这个算法吗?用于在黑白图像上查找未连接区域(岛)的算法

单元连接如果abs(x1-x2) <=1 && abs(y1-y2)<=1

+0

如果图像为黑色和白色的确实,一个在计算上更有效的测试连接细胞将被'is_not_connected = A [N,M]^A [N-1,M]; //表示异或,不是指数!'。 B/W是你的图像吗? – 2012-08-10 08:09:37

+0

投票结束,作为工具rec。 – 2016-04-29 21:42:31

回答

2

Region growing,应该这样做。该链接回答了一个不同的问题,但基本算法应该适合您的需求。你只需要传递另一个参数,它告诉集群的编号。从1开始,每当你进入一个新的群集,增加这个值。

这个新的参数将代替1代表前景,2代表背景。这会给你所有群集的总数,以及它们的所有位置。

0

这使用洪水填充算法。找到连接区域的数量 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); 
     } 
    } 
}