Why get(int rows, int cols) in java returns a null array?
I am building an document scanner app in Android Studio to demonstrate my recently proposed algorithm. However, I encountered a problem while creating the "maxFilter" function, which should return the maximum value of every n x n block of the image.
My code is shown below:
private Mat maxFilter(Mat src, int windowSize) {
Mat max = new Mat();
int size = (int) (src.total());
double[] d = new double[size];
double window[];
for (int j = 0; j < src.rows(); j++){
for (int i = 0; i < src.cols(); i++){
window = new double [windowSize * windowSize];
int count = 0;
for (int r = j - (windowSize / 2); r <= j + (windowSize / 2); r++){
for (int c = i - (windowSize / 2); c <= i + (windowSize / 2); c++) {
if (r < 0 || r >= src.rows() || c < 0 || c >= src.cols()) {
/** Some portion of the mask is outside the image. */
window[count] = 0;
} else {
window[count] = src.get(c, r)[0];
}
count++;
}
}
java.util.Arrays.sort(window);
d[i + j * src.cols()] = window[(windowSize*windowSize)-1];
}
}
for(int j = 0; j < src.rows(); j++) {
for (int i = 0; i < src.cols(); i++) {
src.put(i, j, d[i + j * src.cols()]);
}
}
return max;
}
There is no error message shown in the editor, but while running on actual phones, an fatal exception is shown in logcat.
"java.lang.NullPointerException: Attempt to read from null array at com.myapps.documentscanner.ImageProcessor.maxFilter(ImageProcessor.java:382)"
which refers to the line "window[count] = src.get(c, r)[0];" So what's wrong with this line?