首页 > 解决方案 > 我需要java中的函数opencv返回垫子中颜色的百分比,请

问题描述

我需要java中的函数opencv返回垫子中颜色的百分比,请

public boolean procentage(Mat imageOne, int porsontageDeChangemnt) {
    boolean tr = false;
    int width = (int) imageOne.width();
    int height = (int) imageOne.height();
    int maxPixel = width * height;
    int cont = 0;
    try {

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {

                double[] colorPixel = imageOne.get(i, j);

                if (((int) colorPixel[0] == 255)) {
                    cont++;
                }
            }
        }
    } catch (Exception e) {
    }
    int c = (int) ((cont * 100) / maxPixel);
    if (c >= porsontageDeChangemnt) {
        tr = true;
    }
    if (c > porsontageDeChangemnt) {
        tr = true;
    }
    return tr;
}

此行中的异常>> if (((int) colorPixel[0] == 255)) {

标签: javaopencvmat

解决方案


所以你没有提供一个完整的实现,但我稍微修改了它以获得类似的运行。

如果您将代码一直分解到基本级别并尝试以下操作:

    for (int j = 0; j < 300; j++) {

        double[] colorPixel = null;  //This what your code thinks it is doing

        if (((int) colorPixel[0] == 255)) {
            System.out.println(colorPixel.toString());
        }

您将在当前所在的同一行上获得相同的异常。问题是您在某个时候将null值传递给imageOne方法procentage

如果您想获得更清晰的答案,则需要提出更清晰的问题,因为您在此处编写的代码中没有显示确切的问题。


推荐阅读