从OpenCV中的Mat对象填充二维数组时出现NullPointerException import org.opencv.core.core; 导入org.opencv.core.Mat; 导入org.opencv.imgcodecs.imgcodecs; 导入java.util.array; 公共班机{ 私有静态双getRGB(inti,intj,Mat matrix){//此方法用于获取图像的像素值 双rgbVal; double[]像素=matrix.get(i,j); rgbVal=像素[0]+像素[1]+像素[2];//此行出错 rgbVal=rgbVal/3; 返回rgbVal; } 公共静态void main(字符串[]args){ System.loadLibrary(Core.NATIVE\u LIBRARY\u NAME); Imgcodecs imageCodecs=新的Imgcodecs(); Mat matrix=imageCodecs.imread(“/Users/brand/Downloads/SPACE.JPG”); System.out.println(“图像加载”); System.out.println(“图像大小:”+matrix.width()+“x”+matrix.height()); double[]rgb=new double[matrix.width()][matrix.height()];//这是我要存储值的地方 对于(int i=0;i

从OpenCV中的Mat对象填充二维数组时出现NullPointerException import org.opencv.core.core; 导入org.opencv.core.Mat; 导入org.opencv.imgcodecs.imgcodecs; 导入java.util.array; 公共班机{ 私有静态双getRGB(inti,intj,Mat matrix){//此方法用于获取图像的像素值 双rgbVal; double[]像素=matrix.get(i,j); rgbVal=像素[0]+像素[1]+像素[2];//此行出错 rgbVal=rgbVal/3; 返回rgbVal; } 公共静态void main(字符串[]args){ System.loadLibrary(Core.NATIVE\u LIBRARY\u NAME); Imgcodecs imageCodecs=新的Imgcodecs(); Mat matrix=imageCodecs.imread(“/Users/brand/Downloads/SPACE.JPG”); System.out.println(“图像加载”); System.out.println(“图像大小:”+matrix.width()+“x”+matrix.height()); double[]rgb=new double[matrix.width()][matrix.height()];//这是我要存储值的地方 对于(int i=0;i,java,arrays,opencv,nullpointerexception,Java,Arrays,Opencv,Nullpointerexception,我在第11行为线程“main”java.lang.NullPointerException:中的异常抛出空指针异常:无法从双数组加载,因为“pixel”为空 我尝试添加代码:System.out.println(Arrays.deepToString(pixel)) 完成此操作后,我可以看到我的程序实际上正在按照预期的方式为前几个百像素值工作,但出于某种原因,它一直在相同的值上停止,然后在引发异常之前读取null。如有任何建议,将不胜感激 import org.opencv.core.Core;

我在第11行为线程“main”java.lang.NullPointerException:中的异常抛出空指针异常:无法从双数组加载,因为“pixel”为空

我尝试添加代码:System.out.println(Arrays.deepToString(pixel))

完成此操作后,我可以看到我的程序实际上正在按照预期的方式为前几个百像素值工作,但出于某种原因,它一直在相同的值上停止,然后在引发异常之前读取null。如有任何建议,将不胜感激

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
}
}
我相信你的问题是像素阵列实际上没有3个元素。考虑了像素[0]和像素[1],但像素[2]不存在


RGB应该有三个值,我会为RGB中的蓝色值添加另一个int,然后通过OpenCV中的数组,图像被视为2D(或3D)数组,并通过矩阵索引(
)而不是笛卡尔坐标(
x
y
)索引。问题是您的代码使用索引
i
来表示
x
坐标,而不是
索引,并且使用图像的
宽度
作为限制。要解决此问题,请使用
i
作为
索引,并限制图像中的行数,该行数基本上是
matrix.height()
。类似的逻辑也适用于
j
索引。请检查下面对嵌套循环的更正:

double rgbVal; 
double[] pixel = matrix.get(i, j);
rgbVal = pixel[0] + pixel[1] + pixel[2]; 
for(inti=0;i
matrix.get(i,j)返回这些坐标处的值,即3个rgb值,并将它们存储在我的pixel[]变量中。如果每次像素更新时我都添加代码来打印像素,它就会正常工作,在最终引发异常之前会运行一段时间。我希望这是有意义的。好吧,这很有趣,如果它无限期地工作到某一点,那么这意味着图像中可能有什么东西弄乱了它。也许它是透明的(比如图像有内容,它用魔杖使其透明),而没有实际的rgb值?我只是在这里吐痰,但我可以看出这是一个错误issue@fantaghirocco不幸的是,它没有:(.关于这一点,最让人头疼的是,它只是在迭代几百次后抛出nullpointerexception。我不明白是什么让它突然变为null。更好的是,我会更明确,更喜欢使用
r
c
作为循环计数器的名称,而不是含糊不清的
I
and
j
索引。非常感谢!!!这真是一场噩梦!有时你在自己的脑海里,没有意识到从一开始就应该很明显的事情。这是一个很棒的学习experience@Stonezarcon,您可以标记出具有启发性且解决了您问题的答案。
    for (int i = 0 ; i < matrix.height(); i++) { // Height is the # of rows not width.
        for (int j = 0; j < matrix.width(); j++) { // Width is the # of columns not height.
            rgb[i][j] = getRGB(i, j, matrix); //iterating through my Mat object and storing here
        }
    }