这篇文章主要为大家详细介绍了JavaOpenCV图像处理中仿射变换,透视变换,旋转的实现,文中的示例代码讲解详细,快跟随小编一起学习一下
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;
/**
* @author Administrator
*/
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();
}
/**
* OpenCV 仿射变换
*
* @return void
* @Author: hyacinth
* @Title: warpAffine
* @Description: TODO
* @date: 2022年2月22日12点32分
*/
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;
/**
* @author Administrator
*/
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();
}
/**
* OpenCV 透视变换
*
* @return void
* @Author: hyacinth
* @Title: warpPerspective
* @Description: TODO
* @date: 2022年2月22日12点32分
*/
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;
/**
* @author Administrator
*/
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();
}
/**
* OpenCV 透视变换
*
* @return void
* @Author: hyacinth
* @Title: rotate
* @Description: TODO
* @date: 2022年2月22日12点32分
*/
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图像处理内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:Java OpenCV图像处理之仿射变换,透视变换,旋转详解
基础教程推荐
猜你喜欢
- springboot自定义starter方法及注解实例 2023-03-31
- java实现多人聊天系统 2023-05-19
- Java并发编程进阶之线程控制篇 2023-03-07
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- java基础知识之FileInputStream流的使用 2023-08-11
- Java实现查找文件和替换文件内容 2023-04-06
- Java数据结构之对象比较详解 2023-03-07
- Java实现线程插队的示例代码 2022-09-03
- Java文件管理操作的知识点整理 2023-05-19
- JDK数组阻塞队列源码深入分析总结 2023-04-18