2

私はこのようなイメージを持っています:

元の画像

そして、次のようにイメージに変形する必要があります。

変形画像

たくさんのコードで作ってみましたが解決しませんでした。私の最後の試みがありますが、最終的な画像は見栄えがよくありません

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

class MyPanel2 extends JPanel {

    private final static double DEG_TO_RAD = Math.PI / 360;

    private BufferedImage imageA;
    private BufferedImage imageB;

    static String IMG_URL1 = "img/upg.png";

    public MyPanel2() {

        try {
            imageA = ImageIO.read(new File(IMG_URL1));
        } catch (IOException e) {
            System.err.println("Couldn't find input file. ");
            System.exit(1);
        }

        double rotationRequired = Math.toRadians(90);
        double locationX = imageA.getWidth() / 2;
        double locationY = imageA.getHeight() / 2;
        AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

        BufferedImage pom_2 = imageA;
        imageA = new BufferedImage(imageA.getWidth(), imageA.getHeight(), BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2 = imageA.createGraphics();
        g2.setStroke(new BasicStroke(5));
        g2.setColor(Color.BLACK);

        g2.drawImage(op.filter(pom_2, null),10,10, null);

        BufferedImage pom = new BufferedImage(imageA.getWidth()*2, imageA.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g3 = pom.createGraphics();
        g3.setColor(Color.white);
        g3.drawImage(imageA, imageA.getWidth()/5, 0, null);
        imageA = pom;

        imageB = new BufferedImage(imageA.getWidth(), imageA.getWidth(), BufferedImage.TYPE_INT_ARGB);
        // pruchod obrazkem pixel po pixelu
        for (int i = imageB.getWidth(); i > 0; i--) {
            for (int j = imageB.getHeight(); j > 0; j--) {
                int r = (int) (Math.sqrt(i * i + j *j));
                int fi = (int) (Math.atan2(j, i) / DEG_TO_RAD);

                if (r < pom.getWidth() && fi < pom.getHeight()) {
                    imageB.setRGB(i, j, pom.getRGB(r, fi));
                }
            }
        }

        this.setPreferredSize(new Dimension(800, 600));

        imageA = pom_2;
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;

        g2.drawImage(imageA,0,0, null);


        double rotationRequired = Math.toRadians(-135);
        double locationX = imageB.getWidth() / 2;
        double locationY = imageB.getHeight() / 2;
        AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

        g2.drawImage(op.filter(imageB, null), 0, -imageB.getHeight()/2, null);
    }
}

このコードを使用すると、最終的な画像は次のようになります。 最終イメージ

4

2 に答える 2

1

あなたは正しい道を進んでいます。極座標から直交座標への変換が得られたからです。ソース画像と生成された画像をスケーリング/変換するだけです (または、相対座標を変換するだけです)。

直観的に、生成された画像では、fi は PI/2 のように広がりすぎていますが、目的の画像では、角度の広がりはほとんどありません (曲率がはるかに小さいことがわかりますか?)

お役に立てれば。

于 2013-04-01T20:28:32.383 に答える