Ask Your Question
0

Why get(int rows, int cols) in java returns a null array?

asked 2019-05-21 04:14:21 -0600

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2019-05-21 04:32:31 -0600

LBerger gravatar image

read java doc : https://docs.opencv.org/3.4.6/javadoc... ->

   get

    public double[] get(int row,
                        int col)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-05-21 04:14:21 -0600

Seen: 324 times

Last updated: May 21 '19