JavaOpenCV图像处理之仿射变换,透视变换,旋转详解

2022-11-13 18:11:52 详解 变换 图像处理

1 仿射变换

仿射变换:一种二维坐标到二维坐标的线性变换,它保持二维图像的平直性与平行性,即变换后直线依然是直线,平行的线依然平行。

package com.xu.OpenCV.image;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.highgui.HighGui;
import org.opencv.imGCodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;


public class ImageChange {

    static {
        String os = System.getProperty("os.name");
        String type = System.getProperty("sun.arch.data.model");
        if (os.toUpperCase().contains("windows")) {
            File lib;
            if (type.endsWith("64")) {
                lib = new File("lib\\OpenCV-455\\x64\\" + System.mapLibraryName("opencv_java455"));
            } else {
                lib = new File("lib\\OpenCV-455\\x86\\" + System.mapLibraryName("opencv_java455"));
            }
            System.load(lib.getAbsolutePath());
        }
    }

    public static void main(String[] args) {
        warpAffine();
    }

    
    public static void warpAffine() {
        Mat src = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\1.png");
        MatOfPoint2f point1 = new MatOfPoint2f(new Point(0, 0), new Point(0, src.rows()), new Point(src.cols(), 0));
        MatOfPoint2f point2 = new MatOfPoint2f(new Point(src.cols() * 0.1, src.cols() * 0.1), new Point(src.cols() * 0.2, src.cols() * 0.7),
                new Point(src.cols() * 0.7, src.cols() * 0.2));
        // 获取 放射变换 矩阵
        Mat dst = Imgproc.getAffineTransfORM(point1, point2);
        // 进行 仿射变换
        Mat image = new Mat();
        Imgproc.warpAffine(src, image, dst, src.size());
        HighGui.imshow("原图", src);
        HighGui.imshow("仿射变换", image);
        HighGui.waiTKEy(0);
    }

}

2 透视变换

透视变换:透视变换是将一个平面投影到另一个平面的过程,也称投影映射。是一种非线性变换,表现为可将梯形变换为平行四边形,因此需要四个点来确定透视变换矩阵

package com.xu.opencv.image;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;


public class ImageChange {

    static {
        String os = System.getProperty("os.name");
        String type = System.getProperty("sun.arch.data.model");
        if (os.toUpperCase().contains("WINDOWS")) {
            File lib;
            if (type.endsWith("64")) {
                lib = new File("lib\\OpenCV-455\\x64\\" + System.mapLibraryName("opencv_java455"));
            } else {
                lib = new File("lib\\OpenCV-455\\x86\\" + System.mapLibraryName("opencv_java455"));
            }
            System.load(lib.getAbsolutePath());
        }
    }

    public static void main(String[] args) {
        warpPerspective();
    }

    
    public static void warpPerspective() {
        Mat src = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\1.png");
        MatOfPoint2f point1 = new MatOfPoint2f();
        List<Point> before = new ArrayList<>();
        before.add(new Point(0, 0));
        before.add(new Point(src.cols(), 0));
        before.add(new Point(0, src.rows()));
        before.add(new Point(src.cols(), src.rows()));
        point1.fromList(before);
        MatOfPoint2f point2 = new MatOfPoint2f();
        List<Point> after = new ArrayList<>();
        after.add(new Point(src.cols(), src.rows()));
        after.add(new Point(src.cols() * 0.1, src.rows() * 0.8));
        after.add(new Point(src.cols() * 0.7, src.rows() * 0.3));
        after.add(new Point(0, 0));
        point2.fromList(after);
        // 获取 透视变换 矩阵
        Mat dst = Imgproc.getPerspectiveTransform(point1, point2);
        // 进行 透视变换
        Mat image = new Mat();
        Imgproc.warpPerspective(src, image, dst, src.size());
        HighGui.imshow("原图", src);
        HighGui.imshow("透视变换", image);
        HighGui.waitKey(0);
    }

}

3 图像旋转

package com.xu.opencv.image;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;


public class ImageChange {

    static {
        String os = System.getProperty("os.name");
        String type = System.getProperty("sun.arch.data.model");
        if (os.toUpperCase().contains("WINDOWS")) {
            File lib;
            if (type.endsWith("64")) {
                lib = new File("lib\\OpenCV-455\\x64\\" + System.mapLibraryName("opencv_java455"));
            } else {
                lib = new File("lib\\OpenCV-455\\x86\\" + System.mapLibraryName("opencv_java455"));
            }
            System.load(lib.getAbsolutePath());
        }
    }

    public static void main(String[] args) {
        rotate();
    }

    
    public static void rotate() {
        Mat src = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\1.png");
        // 图像中心
        Point center = new Point(src.cols() / 2, src.rows() / 2);
        // 获取 旋转 矩阵
        Mat dst = Imgproc.getRotationMatrix2D(center, 45, 0.5);
        // 进行 图像旋转
        Mat image = new Mat();
        Imgproc.warpAffine(src, image, dst, src.size());
        HighGui.imshow("原图", src);
        HighGui.imshow("图像旋转", image);
        HighGui.waitKey(0);
    }

}

到此这篇关于Java OpenCV图像处理之仿射变换,透视变换,旋转,平移,缩放详解的文章就介绍到这了,更多相关Java OpenCV图像处理内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章