图像透视校正

时间:2016-02-24 18:00:56

标签: java image-processing bufferedimage

我需要在3轴(x,y和z)上旋转我的BufferedImage,以3个整数给出的角度。 java中有任何本机方法吗?如果没有,我将如何实现?

更新#1:我已经使用OpenCV做了一些...完成后会更新!

更新#2:由于这只是我项目的一部分,我意识到解决问题的一部分并不好,所以我使用了OpenCV getPerspectiveTransform(),然后使用了Imgproc类的warpPerspective()方法变换图像。我基本上只是将this代码移植到java,它工作正常:)

此外,由于更改,我更改了线程名称,使其适合实际的问题/解决方案。

代码(我使用OpenCV 3.1,因为它是最新版本):

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;
    }
}

1 个答案:

答案 0 :(得分:1)

我认为TransformJ package符合您的要求,但我认为它不包含原生代码。