Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to rotate my BufferedImage on 3 axis (x, y and z), by the angles given in 3 integers. Is there any native methods in java? If not, how would I achieve that?

Update #1: I've done some of it with OpenCV... Will update when finished!

Update #2: Since this was just a part of my project, I realized that solving just a part of the problem wouldn't be good, so I used OpenCV getPerspectiveTransform() and then warpPerspective() methods from Imgproc class to transform image. I have basically just ported this code to java and it works fine :)

Also I have changed the thread name due the changes to make it fit the actual question/solution.

Code (I used OpenCV 3.1, since it's the latest version):

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;

public class Main extends JFrame {
    private static final long serialVersionUID = 1L;

    BufferedImage transformed = null;

    //These locations are just the corners of the 4 reference points. I am writing the auto recognition part right now :)
    Point p4 = new Point(260, 215);
    Point p1 = new Point(412, 221);
    Point p2 = new Point(464, 444);
    Point p3 = new Point(312, 435);

    public Main() {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        File f = new File("FILEPATH ");

        MatOfPoint2f corners = new MatOfPoint2f();

        Mat src = Imgcodecs.imread(f.getAbsolutePath());

        corners.push_back(new MatOfPoint2f(p1));
        corners.push_back(new MatOfPoint2f(p2));
        corners.push_back(new MatOfPoint2f(p3));
        corners.push_back(new MatOfPoint2f(p4));

        Point center = new Point(0, 0);
        for (int i = 0; i < corners.toArray().length; i++) {
            center.x += corners.toArray()[i].x;
            center.y += corners.toArray()[i].y;
        }

        center.x /= corners.toArray().length;
        center.y /= corners.toArray().length;
        sortCorners(corners, center);

        Mat quad = Mat.zeros(1000, 1900, CvType.CV_8U);

        MatOfPoint2f quad_pts = new MatOfPoint2f();

        quad_pts.push_back(new MatOfPoint2f(new Point(0, 0)));
        quad_pts.push_back(new MatOfPoint2f(new Point(quad.width(), 0)));
        quad_pts.push_back(new MatOfPoint2f(new Point(quad.width(), quad.height())));
        quad_pts.push_back(new MatOfPoint2f(new Point(0, quad.height())));

        Mat transmtx = Imgproc.getPerspectiveTransform(corners, quad_pts);

        Imgproc.warpPerspective(src, quad, transmtx, quad.size());

        transformed = matToBufferedImage(quad);

        setSize(500, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void paint(Graphics g) {
        g.clearRect(0, 0, this.getWidth(), this.getHeight());
        g.drawImage(transformed, 0, 22, null);
    }

    public MatOfPoint2f sortCorners(MatOfPoint2f corners, Point center) {
        MatOfPoint2f top = new MatOfPoint2f();
        MatOfPoint2f bot = new MatOfPoint2f();

        for (int i = 0; i < corners.toArray().length; i++) {
            if (corners.toArray()[i].y < center.y){
                top.push_back(new MatOfPoint2f(corners.toArray()[i]));
            }
            else
                bot.push_back(new MatOfPoint2f(corners.toArray()[i]));
        }

        Point tl = p4;
        Point tr = p1;
        Point bl = p2;
        Point br = p3;

        tl = top.toArray()[0].x > top.toArray()[1].x ? top.toArray()[1] : top.toArray()[0];
        tr = top.toArray()[0].x > top.toArray()[1].x ? top.toArray()[0] : top.toArray()[1];
        bl = bot.toArray()[0].x > bot.toArray()[1].x ? bot.toArray()[1] : bot.toArray()[0];
        br = bot.toArray()[0].x > bot.toArray()[1].x ? bot.toArray()[0] : bot.toArray()[1];

        corners.release();
        corners.push_back(new MatOfPoint2f(tl));
        corners.push_back(new MatOfPoint2f(tr));
        corners.push_back(new MatOfPoint2f(br));
        corners.push_back(new MatOfPoint2f(bl));
        System.out.println(corners.toArray()[0] + ", " + corners.toArray()[1] + ", " + corners.toArray()[2] + ", " + corners.toArray()[3] + ", ");
        return corners;

    }

    public BufferedImage matToBufferedImage(Mat image) {
        Mat image_tmp = image;
        MatOfByte matOfByte = new MatOfByte();

        Imgcodecs.imencode(".jpg", image_tmp, matOfByte);

        byte[] byteArray = matOfByte.toArray();
        BufferedImage bufImage = null;

        try {

            InputStream in = new ByteArrayInputStream(byteArray);
            bufImage = ImageIO.read(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bufImage;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
150 views
Welcome To Ask or Share your Answers For Others

1 Answer

I think that the TransformJ package does what you want, but I don't think it contains native code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...