android - android中的连接组件标签

标签 android image-processing components labeling

我正在创建一个与从图像中提取车牌相关的 Android 应用程序。我遵循的提取车牌的算法基于图像中对象(blob)的连接组件标记。在 matlab 中,我可以使用 bwlabel() 轻松执行 CCL,但我在 android (eclipse IDE) 中找不到类似 bwlabel 的东西

是否有一些预定义的方法或任何其他方式可以帮助我在 Android 中标记图像中的对象?

最佳答案

伙计,我真的不擅长 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 并使用与想法类似但它是用 C 编写的 findCountours。

我在一个代码中使用它来查找图像中不同元素的区域:

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);
  }

关于android - android中的连接组件标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5585575/

相关文章:

java - 使用 Firestore 和 Firebase Storage 将图像检索到 ImageView

python - 如何学习计算机视觉编程和实时图像处理的基本概念

c - 如何知道硬件/软件协同设计是否对特定应用有用?

javascript - 如何像GMap那样按需加载一部分Image?

javascript - Vue.js 向父组件发射对象

java - 在 Java 中将形状转换到组件中?

Android 应用重定向到自定义商店而不是 Google Play 商店

java - 为 View 禁用 onClick 声音

Android - 使用configuration.locale强制应用程序语言不会更改启动器中的app_name

c# - 更改 TreeView 中子节点的背景色