Resize matrix in Java

I have a matrix double[][]with arbitrary sizes, but more than 300 (maybe in one or maybe in both dimensions). I want to scale it to double[300][300].

My basic approach is to interpolate the matrix and bring it up double[600][600], and then take the four elements, and to find their average value, i.e. the elements 0,0, 0,1, 1,0and 1,1are 0,0final matrix 300x300.

I found the interpolation library in JAVA, but I cannot figure out how to use it. Can someone provide some examples or information?

Library: http://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/Interpolation.html

Thnx.

+4
source share
1 answer

How about writing a simple method that maps source cells to destination and then averages?

public static boolean matrixReduce(double[][] dst, double[][] src) {
    double dstMaxX = dst.length - 1, dstMaxY = dst[0].length - 1;
    double srcMaxX = src.length - 1, srcMaxY = src[0].length - 1;
    int count[][] = new int[dst.length][dst[0].length];

    for (int x = 0; x < src.length; x++) {
        for (int y = 0; y < src[0].length; y++) {
            int xx = (int) Math.round((double) x * dstMaxX / srcMaxX);
            int yy = (int) Math.round((double) y * dstMaxY / srcMaxY);

            dst[xx][yy] += src[x][y];
            count[xx][yy]++;
        }
    }

    for (int x = 0; x < dst.length; x++) {
        for (int y = 0; y < dst[0].length; y++) {
            dst[x][y] /= count[x][y];
        }
    }

    return true;
}
0
source

Source: https://habr.com/ru/post/1606134/


All Articles