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
}
}
I am throwing a null pointer exception on line 11 for : Exception in thread "main" java.lang.NullPointerException: Cannot load from double array because "pixel" is null.
I have tried adding the code: System.out.println(Arrays.deepToString(pixel));
After doing this I can see that my program is actually working as intended for the first several hungred pixel values, but for some reason it keeps stopping on the same value, and then reading null before throwing the exception. Any advice would be appreciated.