通过X.XX°旋转矩阵(2D阵列)

时间:2015-03-30 19:45:58

标签: java arrays matrix

我无法在以下主题中找到任何内容,因为大多数问题要求将阵列旋转90°,180°,270°,这实际上很容易。

我需要为所有可能的度数转换矩阵,就好像我在photoshop中旋转图像一样(以编程方式,我不需要GUI或类似的东西)。这将导致结果中的小错误,这不会太麻烦。

旋转图片应该是一个非常类似的方法,所以我认为应该有很多解决方案来解决这个问题。在java中有一种简单的方法可以做到这一点,还是我必须自己计算每个条目的位置?

注意:我正在使用的数组是一个“二进制”数组,填充了值为“0”和“1”的整数。

PS:这会改变矩阵周围“边界框”的大小。现在这没问题。


编辑:我试着给出一个例子:

初始

{ 0, 0, 1, 0 }
{ 0, 0, 1, 0 }
{ 0, 0, 1, 0 }
{ 0, 0, 1, 0 }
{ 0, 0, 1, 0 }
{ 0, 0, 1, 0 }

在旋转点[3] [2]处旋转20.12°。

结果:

{ 0, 0, 0, 1 }
{ 0, 0, 1, 0 }
{ 0, 0, 1, 0 }
{ 0, 0, 1, 0 }
{ 0, 0, 1, 0 }
{ 0, 1, 0, 0 }

这看起来非常无用,但是这个矩阵的大小越多,这些结果就越准确。我使用比这个例子大得多的矩阵。 :)

2 个答案:

答案 0 :(得分:1)

我会写两个方法:一个将数组转换为BufferedImage,另一个转换为BufferedImage。这两件事都应该易于实施。可以使用AffineTransform等旋转BufferedImage 不是很优雅,但与标准的java代码没有第三方的东西 并且您有图像以图形方式显示结果。

这个实现做了上面提到的事情:

package rasterImage;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

public class MatrixRotator extends javax.swing.JFrame {

    public MatrixRotator() {
        initComponents();
        setLocationRelativeTo(null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButtonDoIt = new javax.swing.JButton();
        jPanelOrg = new javax.swing.JPanel();
        jPanelRot = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButtonDoIt.setText("Do it");
        jButtonDoIt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDoItActionPerformed(evt);
            }
        });

        jPanelOrg.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanelOrgLayout = new javax.swing.GroupLayout(jPanelOrg);
        jPanelOrg.setLayout(jPanelOrgLayout);
        jPanelOrgLayout.setHorizontalGroup(
            jPanelOrgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanelOrgLayout.setVerticalGroup(
            jPanelOrgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jPanelRot.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanelRotLayout = new javax.swing.GroupLayout(jPanelRot);
        jPanelRot.setLayout(jPanelRotLayout);
        jPanelRotLayout.setHorizontalGroup(
            jPanelRotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanelRotLayout.setVerticalGroup(
            jPanelRotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonDoIt)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanelOrg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanelRot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanelRot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonDoIt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanelOrg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void printRaster(int[][] raster) {
        int rows = raster.length;
        int cols = raster[0].length;

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                int content = raster[r][c];
                System.out.print(content);
            }
            System.out.println();
        }
    }

    private BufferedImage convert2image(int[][] raster) {
        int rows = raster.length;
        int cols = raster[0].length;

        BufferedImage img = new BufferedImage(cols, rows, BufferedImage.TYPE_BYTE_BINARY);
        Graphics2D g = img.createGraphics();

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                int content = raster[r][c];
                if (content == 1) {
                    g.fillRect(c, r, 1, 1);
                }
            }
        }

        return img;
    }

    private int[][] convert2raster(BufferedImage img) {
        int cols = img.getWidth();
        int rows = img.getHeight();
        int[][] raster = new int[rows][cols];

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                int content = img.getRGB(c, r);
                if (content == -1) {
                    raster[r][c] = 1;
                } else {
                    raster[r][c] = 0;
                }
            }
        }

        return raster;
    }

    private void jButtonDoItActionPerformed(java.awt.event.ActionEvent evt) {                                            
        int[][] raster = {{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}};
        printRaster(raster);

        //convert raster to image:
        BufferedImage img = convert2image(raster);

        //create rotated image ('large enough'):
        BufferedImage imgRot = new BufferedImage(10, 10, BufferedImage.TYPE_BYTE_BINARY);

        //rotate the image:
        AffineTransform tx = new AffineTransform();
        tx.rotate(Math.toRadians(20.12));
        imgRot.createGraphics().drawImage(img, tx, null);

        //convert image to raster:
        raster = convert2raster(imgRot);
        printRaster(raster);

        //draw it:
        jPanelOrg.getGraphics().drawImage(img, 0, 0, null);
        jPanelRot.getGraphics().drawImage(imgRot, 0, 0, null);
    }                                           

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MatrixRotator().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButtonDoIt;
    private javax.swing.JPanel jPanelOrg;
    private javax.swing.JPanel jPanelRot;
    // End of variables declaration                   
}

答案 1 :(得分:1)

正如评论中已经指出和讨论的那样,实施这一点时有一定的自由度。

特别是,在某些情况下,根本不清楚应该发生什么。首先,数组的“边界”在旋转时会发生变化。然后,当旋转后输入数组的一个条目恰好位于输出数组的两个单元格之间时,您将遇到锯齿伪像。

然而,无视这些小问题,基本上只有两种解决方案:

  • <强> “前向映射”

    对于 input 数组的每个单元格,可以计算单元格的旋转位置,并将输入单元格中的值写入输出数组的旋转位置。

    < / LI>
  • <强> “向后映射”

    对于输出数组的每个单元格,可以计算此单元格在输入数组中的位置,并从那里获取值。

在这两种情况下,只需使用绕某一点旋转的AffineTransform即可完成旋转。

从实际的角度来看,这并没有多大区别 - 只要两个阵列的分辨率(即本例中的大小)相同即可!然而,在混叠伪像方面会有不同之处:虽然“前向”方法可能偶尔会“吞下”点(并且在你期望1的位置放置0),但是后向方法可能偶尔会将1放在你想要的位置。期待一个0.

这两种方法的实现可能只是这样:

import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;

public class MatrixRotate
{
    public static void main(String[] args)
    {
        int a[][] = new int[][]{
            { 0, 0, 1, 0 },
            { 0, 0, 1, 0 },
            { 0, 0, 1, 0 },
            { 0, 0, 1, 0 },
            { 0, 0, 1, 0 },
            { 0, 0, 1, 0 },
        };

        System.out.println("Initial:");
        System.out.println(toString(a));

        for (int angleDeg=10; angleDeg<=180; angleDeg+=10)
        {
            int[][] r0 = rotateForward(a, 2.0, 2.0, Math.toRadians(angleDeg));
            System.out.println("Rotated about "+angleDeg+
                " degrees using forward method:");
            System.out.println(toString(r0));

            int[][] r1 = rotateBackward(a, 2.0, 2.0, Math.toRadians(angleDeg));
            System.out.println("Rotated about "+angleDeg+
                " degrees using backward method:");
            System.out.println(toString(r1));

        }
    }


    private static int[][] rotateForward(int a[][], 
        double cx, double cy, double angleRad)
    {
        AffineTransform at = new AffineTransform();
        at.setToRotation(angleRad, cx, cy);
        int rotated[][] = create(a);
        for (int r=0; r<a.length; r++)
        {
            for (int c=0; c<a[r].length; c++)
            {
                Point p0 = new Point(c,r);
                Point p1 = new Point();
                at.transform(p0, p1);
                if (isValid(p1, rotated))
                {
                    rotated[p1.y][p1.x] = a[p0.y][p0.x];
                }
            }
        }
        return rotated;
    }

    private static int[][] rotateBackward(int a[][], 
        double cx, double cy, double angleRad)
    {
        AffineTransform at = new AffineTransform();
        at.setToRotation(angleRad, cx, cy);
        try
        {
            at.invert();
        }
        catch (NoninvertibleTransformException e)
        {
            // Can not happen here
        }
        int rotated[][] = create(a);
        for (int r=0; r<a.length; r++)
        {
            for (int c=0; c<a[r].length; c++)
            {
                Point p0 = new Point(c,r);
                Point p1 = new Point();
                at.transform(p0, p1);
                if (isValid(p1, a))
                {
                    rotated[p0.y][p0.x] = a[p1.y][p1.x];
                }
            }
        }
        return rotated;
    }

    private static boolean isValid(Point p, int a[][])
    {
        return 
            p.x >= 0 && p.y >= 0 &&
            p.y < a.length && p.x < a[p.y].length;
    }

    private static int[][] create(int a[][])
    {
        int b[][] = new int[a.length][];
        for (int i=0; i<a.length; i++)
        {
            b[i] = new int[a[i].length];
        }
        return b;
    }

    private static String toString(int a[][])
    {
        StringBuilder sb = new StringBuilder();
        String format = "%2d";
        for (int r=0; r<a.length; r++)
        {
            for (int c=0; c<a[r].length; c++)
            {
                sb.append(String.format(format, a[r][c]));
            }
            sb.append("\n");
        }
        return sb.toString();
    }

}