Affine transform with interpolation

牧云@^-^@ 提交于 2019-12-03 16:14:00

The same people you linked to have a C implementation with several interpolation options here. You could probably use JNI to wrap it. There is also JavaCV, which wraps OpenCV. OpenCV contains the warpAffine, which has interpolation. Also, check out the Java Advanced Imaging API here.

hyperknot

OK, here is the solution I ended up with.

  1. I transformed all my array[][] into a BufferedImage object

    static BufferedImage BImageFrom2DArray(float data[][]) {
        int width = data.length;
        int height = data[0].length;
        BufferedImage myimage = new BufferedImage(width, height,  BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int value = (int) ((1f - data[x][y]) * 255f);
                myimage.setRGB(y, x, (value << 16) | (value << 8) | value);
            }
        }
        return myimage;
    }
    
  2. Applied the affine transformation using AffineTransformOp with interpolation bicubic

        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BICUBIC);
        BufferedImage im_transformed = op.filter(im_src, null);
    
  3. Transformed back the BufferedImage object into array[][]:

        static float[][] ArrayFromBImage(BufferedImage bimage, int width, int height) {
        int max_x = bimage.getWidth();
        int max_y = bimage.getHeight();
        float[][] array = new float[width][height];
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                float red, alpha, value;
                int color;
                if (x >= max_x || y >= max_y) {
                    array[y][x] = 0;
                } else {
                    color = bimage.getRGB(x, y);
                    alpha = (color >> 24) & 0xFF;
                    red = (color >> 16) & 0xFF;
                    value = 1f - red / 255;
                    if (alpha == 0) {
                        array[y][x] = 0;
                    } else {
                        array[y][x] = value;
                    }
                }
            }
        }
        return array;
        }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!