2011-04-07 29 views
2

我正在創建一個與圖像中的牌照提取相關的android應用程序。我正在按照提取牌照的算法基於圖像中對象的連接組件標籤(blob)。在MATLAB中,我可以很容易地進行CCL使用bwlabel(),但我不能找到像bwlabel任何的Android(Eclipse IDE中)Android中的連接組件標籤

有一些預定義的方法或其他任何方式,可以幫助我標記在Android中的圖像中的對象?

+0

實現它嗎?它是一個相當簡單的遞歸算法。 [見此](http://en.wikipedia.org/wiki/Connected-component_labeling) – by0 2013-05-01 16:48:09

回答

2

男人,我對Java很不好,但是我創建了我自己的連接組件標籤類。使用TwoPass功能:

public class ConnectedComponentsLabelling { 
public static int[][] twoPass(int[][] matrix) { 

    int nextLabel = 1; 

    int rowLength = matrix.length; 
    int columnLength = matrix[0].length; 

    List<Set<Integer>> linked = new ArrayList<Set<Integer>>(); 
    int[][] labels = new int[rowLength][columnLength]; 

    //Primeiro Passo 
    for (int row = 0; row < rowLength; row++) { 
     for (int column = 0; column < columnLength; column++) { 
      if (matrix[row][column] != 0) { 
       int[] neibours = neibours(row, column, labels); 
       if (neibours.length == 0) { 
        linked.add(new HashSet()); 
        linked.get(nextLabel - 1).add(nextLabel); 
        labels[row][column] = nextLabel; 
        nextLabel += 1; 
       } else { 
        Arrays.sort(neibours); 
        labels[row][column] = neibours[0]; 
        for (int i = 0; i < neibours.length; i++) { 
         for (int j = 0; j < neibours.length; j++) { 
          linked.get(neibours[i] - 1).add(neibours[j]); 
         } 
        } 
       } 

      } 
     } 
    } 

    //Segundo Passo 
    int[] vector = new int[nextLabel]; 
    for (int i= 0; i < nextLabel - 1; i++){ 
     vector[i] = Collections.min(linked.get(i), null); 
    } 

    for (int row = 0; row < rowLength; row++) { 
     for (int column = 0; column < columnLength; column++) { 
      if (matrix[row][column] != 0) { 
       labels[row][column] = vector[labels[row][column] - 1]; 
      } 
     } 
    } 
    return labels; 
} 

public static int[] neibours(int row, int column, int[][] matrix) { 

    int[] neibours = {}; 
    int rowLength = matrix.length; 
    int columnLength = matrix[0].length; 


    if (row ==0 && column ==0) { return neibours; 
    } 
    else if (row == 0) { 
     neibours = add_element(matrix[row][column - 1], neibours); 
    } else if (column == 0) { 
     neibours = add_element(matrix[row - 1][column], neibours); 
    } else if ((row > 0) && (column > 0) && (column < columnLength - 1)) { 
     neibours = add_element(matrix[row][column - 1], neibours); 
     neibours = add_element(matrix[row - 1][column - 1], neibours); 
     neibours = add_element(matrix[row - 1][column], neibours); 
     neibours = add_element(matrix[row - 1][column + 1], neibours); 
    } else if (row > 0 && column > 0) { 
     neibours = add_element(matrix[row][column - 1], neibours); 
     neibours = add_element(matrix[row - 1][column - 1], neibours); 
     neibours = add_element(matrix[row - 1][column], neibours); 
    } 

    int[] neibours2 = {}; 
    for (int i = 0; i < neibours.length; i++) { 
     if (neibours[i] != 0) { 
      neibours2 = add_element(neibours[i], neibours2); 
     } 
    } 
    return neibours2; 
} 

public static int max(int[] vector){ 
    int max = 0; 
    for (int number = 0; number < vector.length; number++) { 
     if (number > max){max = number;} 
    } 
    return max; 
} 

public static int[] areaCount(int[][] matrix){ 
    int[] vectorLabel = {}; 
    int[] vectorArea = {}; 
    int positionNew = 0; 
    boolean teste; 

    int rowLength = matrix.length; 
    int columnLength = matrix[0].length; 

    for (int row = 0; row < rowLength; row++) { 
     for (int column = 0; column < columnLength; column++) { 
      teste = true; 

      for (int position = 0; position < vectorLabel.length; position++){ 
       if (vectorLabel[position] == matrix[row][column]) {positionNew = position; teste = false;} 
      } 
      if (teste){ 
       vectorLabel = add_element(matrix[row][column], vectorLabel); 
       vectorArea = add_element(1, vectorArea); 
      } else { 
       vectorArea[positionNew] = vectorArea[positionNew] + 1; 
      } 
     } 

    } 

    return vectorArea; 
} 


public static int[] add_element(int element, int[] neibours) { 
    neibours = Arrays.copyOf(neibours, neibours.length + 1); 
    neibours[neibours.length - 1] = element; 
    return neibours; 
} 
} 

但在使用它,因爲它是如此之慢,我不會建議...更好的進口OPENCV和使用findCountours類似的想法,但它是用C寫

我用它在一個代碼中查找圖像中不同元素的區域:

List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 
Mat imgMat = new Mat();  
List<Double> areasList = new ArrayList<Double>(); 

Imgproc.findContours(imgMat, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE); 
//matrix = ConnectedComponentsLabelling.twoPass(matrix); 
for (int idx = 0; idx < contours.size(); idx++) { 
     Mat contour = contours.get(idx); 
     double contourarea = Imgproc.contourArea(contour); 
     areasList.add(contourarea); 
    } 
+0

感謝Dinidiniz,4年前,當我提出這個問題時,我也最終實現了它。這很慢。 – Waqar 2015-10-17 13:45:40