android 中的连接组件标签

发布于 2024-10-30 18:13:26 字数 193 浏览 1 评论 0原文

我正在创建一个与从图像中提取车牌相关的 Android 应用程序。我用来提取车牌的算法基于图像中对象(blob)的连通分量标签。在matlab中,我可以使用bwlabel()轻松执行CCL,但我在android(eclipse IDE)中找不到类似bwlabel的任何内容

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

I am creating an android app related to license plate extraction from an image. The algorithm that i am following to extract the license plate is based on Connected Component Labeling of objects(blob) in an image. In matlab i can easily perform CCL using bwlabel() but i cant find anything like bwlabel in android (eclipse IDE)

Is there some predefined method or any other way that can help me labeling the objects in an image in Android?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手长情犹 2024-11-06 18:13:26

伙计,我真的不擅长 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);
  }

Man, I am really bad at Java, but I created my own connected component labelling class. Use the TwoPass function:

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

But i won't recommending using it because it is so slow... Better import OPENCV and use findCountours that is similar the idea but it is written in C.

I used it in one code to find the areas of different elements in a image:

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);
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文