java - 像素阵列的旋转

标签 java arrays rotation trigonometry

我是java初学者。

我想自己进行旋转,我知道如何使用 Graphics 类,但我尝试了解它在这些类中的工作原理。

我从谷歌图片中随机选择了一张图片。我把它变成了一个像素数组,用这个函数,它工作得很好:

BufferedImage immeuble = ImageIO.read( new File( "....jpg" ) );
int width = immeuble.getWidth();
int height = immeuble.getHeight();
int[] pixArray;
pixArray = Util.convertToPixels( immeuble );



public static int[] convertToPixels(Image img) {
            int width = img.getWidth(null);
            int height = img.getHeight(null);
            int[] pixel = new int[width * height];

        PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
            throw new IllegalStateException("Error: Interrupted Waiting for Pixels");
        }
        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
            throw new IllegalStateException("Error: Image Fetch Aborted");
        }
        return pixel;
    }

然后,我根据给定的角度旋转这个像素阵列。

pixArray = util.rotate( angle, pixArray, width, height );

public int[] rotate( double angle, int[] pixels, int width, int height ) {
        final double radians = Math.toRadians( angle );
        final double cos = Math.cos( radians );
        final double sin = Math.sin( radians );
        final int[ ] pixels2 = new int[ pixels.length ];
        for( int pixel = 0; pixel < pixels2.length; pixel++ ) {
            pixels2[pixel] = 0xFFFFFF;
        }
        for( int x = 0; x < width; x++ ) {
            for( int y = 0; y < height; y++ ) {
                final int centerx = width / 2;
                final int centery = height / 2;
                final int m = x - centerx;
                final int n = y - centery;
                final int j = ( ( int ) ( m * cos + n * sin ) ) + centerx;
                final int k = ( ( int ) ( n * cos - m * sin ) ) + centery;
                if( j >= 0 && j < width && k >= 0 && k < height ){
                    pixels2[ ( y * width + x ) ] = pixels[ ( k * width + j ) ];
                }
            }
        }
        return pixels2;
    }

然后,我通过旋转显示新图像,并将数组 (pixArray) 转换为二维数组:

array2d = util.monoToMulti( pixArray, height, width );
public int[][] monoToMulti(final int[] array, final int height, final int width) {
        if (array.length != (height * width))
            throw new IllegalArgumentException("Invalid array length");

        int[][] multi = new int[height][width];

        for ( int i = 0; i < height; i++ )
            System.arraycopy(array, (i*width), multi[i], 0, width);

        return multi;
    }

并创建一个新的 BufferedImage :

BufferedImage bufferedImage = new BufferedImage( array2d.length, array2d[0].length,
                                                         BufferedImage.TYPE_INT_ARGB );
        for ( int x = 0; x < array2d.length; x++ )
        {
            for ( int y = 0; y < array2d[x].length; y++ )
            {
                bufferedImage.setRGB( x, y, array2d[x][y] );
            }
        }


        util.saveToPNG( bufferedImage, "rotation_test" );

我有几个问题:

  • 第一个是:我的图像,即使我没有进行旋转部分(util.rotate)。输出中的图像旋转了 90°C 的角度。而且我不知道为什么,我看不出问题出在哪里。

  • 然后,如果我应用旋转部分,图像就会被剪切。因为我保留输入图像的宽度和高度,并且我想计算输出图像的范围,所以我可以在输出中获得完整的图像。我认为其中有一些正弦或余弦计算,但我对三角学一点也不擅长。

感谢您的帮助!

编辑:例如角度为 18°

double angle = 18;


public void saveToPNG(Image image, String name) throws IOException {
        File f = new File (name + ".png");
        try {
            ImageIO.write((RenderedImage) image, "png", f);
        } catch (IOException e) {
            e.printStackTrace();
        }

最佳答案

首先,我想说我同意 ControlAltDel 的观点,即使用内置功能可能会更好。

但是,我会尽力回答您的问题:

First one is : my image even if I don't make the rotation part (util.rotate). The image in output is rotated by an angle of 90°C. And I don't know why, I can't see where the problem comes from.

这可能来自您从 2d 像素数组创建新图像的代码:

数组的第一维是高度,即每个元素都是一行像素的数组。但是,当将像素设置为新数组时,您将交换 y 和 x,即迭代行并将行索引分配给 x 。这就是您得到的交换错误。

示例:您正确应用了 1D 数组布局,即它是第 1 行的所有像素,然后是第 2 行的所有像素,等等,也在您的代码中指出:pixels2[ ( y * width + x ) ]

当您将该数组拆分为二维数组时,您可以按如下方式声明它:

int[][] multi = new int[height][width];

如您所见,第一个维度是高度,但是,当再次循环该数组时,您将访问如下值:

bufferedImage.setRGB( x, y, array2d[x][y] );

所以你基本上将 x 坐标作为高度的索引传递,反之亦然,因此 90 度旋转(你基本上交换 x 和 y)。

要解决这个问题,只需将循环更改为首先以 y 开头。

Then, if I apply the rotation part, the image is cut. Because I keep the width and height of the input image, And I would like to calculate the extent of the output image, so I can have the full image in output. I think there is some sinus ou cosinus calculation in that, but I'm not good at all in trigonometry.

您可以首先计算角点的旋转位置(您实际上只需要其中两个),然后计算它们在 x 和 y 上到中心的距离,并使用最高值来为旋转图像创建边界框.

由于您围绕中心旋转,因此您只需要两个角以及它们在 x 和 y 上到中心的最大距离。最终尺寸将使这些距离加倍。

示例:

您只需要两个角,因此可以使用第一行的第一个和最后一个像素。它们的坐标 (x/y) 0/0 和宽度/0。

现在将它们输入旋转代码(顺便说一句,计算循环外矩形的中心)并获取旋转坐标,例如x1/y1x2/y2

获取这些坐标距 x 和 y 中心的最大距离,即使用 Math.max( Math.abs(x1-center_x), Math.abs(x2-center_x)) 等.

将最大距离加倍,即可获得新数组的尺寸。

现在围绕其中心旋转原始图像,但在将它们放入新数组之前向坐标添加偏移量。偏移量将是两个数组之间宽度和高度差异的一半。

关于java - 像素阵列的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31789004/

相关文章:

java - Linux 核心转储与堆转储?

Java .getText 与硬编码字符串不返回相同的结果

javascript - JavaScript 数组的 "in operator"和 "includes()"有什么区别

java - Ant 使用带有命名空间的 antlib

java - Selenium - 如何等到页面完全加载

java - 在消息驱动 Bean (MDB) 中捕获异常

ios - 如何遍历 Swift 数组并在 iOS 图表中使用数据?

ios - ImageView 一直处于侧边状态

ios - 从 CGAffineTransform 获取比例和旋转角度?

Java GUI 旋转和平移矩形