【发布时间】:2021-03-31 17:22:19
【问题描述】:
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import java.util.Arrays;
public class Main {
private static double getRGB(int i, int j, Mat matrix) { //this method is used to obtain pixel values of an image
double rgbVal;
double[] pixel = matrix.get(i, j);
rgbVal = pixel[0] + pixel[1] + pixel[2]; //error on this line
rgbVal = rgbVal / 3;
return rgbVal;
}
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Imgcodecs imageCodecs = new Imgcodecs();
Mat matrix = imageCodecs.imread("/Users/brand/Downloads/SPACE.JPG");
System.out.println("Image loaded");
System.out.println("Image size : " + matrix.width() + " x " + matrix.height());
double[][] rgb = new double[matrix.width()][matrix.height()]; //this is where i want to store the values
for (int i = 0 ; i < matrix.width(); i++) {
for (int j = 0; j < matrix.height(); j++) {
rgb[i][j] = getRGB(i, j, matrix); //iterating through my Mat object and storing here
}
}
System.out.println(Arrays.deepToString(rgb)); //checking if it works
}
}
我在第 11 行抛出一个空指针异常:线程“main”java.lang.NullPointerException 中的异常:无法从双精度数组加载,因为“pixel”为空。
我已尝试添加代码:System.out.println(Arrays.deepToString(pixel));
执行此操作后,我可以看到我的程序实际上对前几个饥饿的像素值按预期工作,但由于某种原因,它一直停止在相同的值上,然后在抛出异常之前读取 null。任何建议将不胜感激。
【问题讨论】:
-
@fantaghirocco 不幸的是它没有:(。关于这一点最大的头疼是它只是在迭代数百次后抛出空指针异常。我不明白是什么让它突然为空。
标签: java arrays opencv nullpointerexception