Java OpenCV图像处理之仿射变换,透视变换,旋转详解
作者:深色風信子 时间:2022-04-18 23:56:01
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);
}
}
来源:https://blog.csdn.net/qq_34814092/article/details/127298078
标签:Java,OpenCV,仿射变换,透视变换,旋转
0
投稿
猜你喜欢
Android 悬浮窗权限各机型各系统适配大全(总结)
2022-04-27 10:09:53
MyEclipse2017创建Spring项目的方法
2023-01-02 17:00:25
详解java中float与double的区别
2021-09-12 11:04:47
Android实现多个连续带数字圆圈效果
2021-09-10 02:46:23
浅谈C#与Java两种语言的比较
2023-09-26 13:05:19
五分钟手撸一个Spring容器(萌芽版)
2021-07-29 02:41:54
Java 中的弱引用是什么
2021-08-21 08:10:46
详解C#如何优雅地终止线程
2023-11-21 11:41:31
Java并发包之CopyOnWriteArrayList类的深入讲解
2022-10-06 09:15:21
C语言实现贪吃蛇小游戏开发
2022-08-20 00:03:07
Android沉浸式状态栏设计的实例代码
2023-11-14 13:03:14
Java SoftReference类案例详解
2023-04-07 06:52:29
C#实现将字符串转换成日期格式的方法
2021-07-18 06:19:11
Android应用中仿今日头条App制作ViewPager指示器
2023-02-01 15:45:24
Java枚举类使用Lombok方式
2022-06-28 22:07:02
超详细的Spring Boot入门笔记(总结)
2022-10-26 18:44:21
SpringCloud服务实现同时使用eureka和nacos方法
2022-01-14 13:58:57
Java文本文件操作方法实例详解
2021-12-14 10:03:21
java微信公众号支付示例详解
2023-11-15 05:52:01
如何解决@NotBlank不生效的问题
2022-04-01 13:41:08