Kotlin Bytedeco OpenCV 图像图像53 图像透视变换
- [1 添加依赖](#1 添加依赖)
- [2 测试代码](#2 测试代码)
- [3 测试结果](#3 测试结果)
1 添加依赖
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xu</groupId>
<artifactId>KotlinOpenCV</artifactId>
<version>1.0</version>
<properties>
<kotlin.version>2.0.0</kotlin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
</properties>
<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.29</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.27.0</version>
</dependency>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.9.0-RC</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.opencv</groupId>-->
<!-- <artifactId>opencv</artifactId>-->
<!-- <version>4100</version>-->
<!-- <scope>system</scope>-->
<!-- <systemPath>${project.basedir}/lib/opencv/opencv-4100.jar</systemPath>-->
<!-- </dependency>-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv-platform</artifactId>
<version>4.10.0-1.5.11</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.bytedeco</groupId>-->
<!-- <artifactId>ffmpeg-platform</artifactId>-->
<!-- <version>6.1.1-1.5.10</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>MainKt</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
2 测试代码
kotlin
package com.xu.com.xu.image
import org.bytedeco.javacpp.Loader
import org.bytedeco.javacpp.Pointer
import org.bytedeco.opencv.global.opencv_core
import org.bytedeco.opencv.global.opencv_highgui
import org.bytedeco.opencv.global.opencv_imgcodecs
import org.bytedeco.opencv.global.opencv_imgproc
import org.bytedeco.opencv.opencv_core.Mat
import org.bytedeco.opencv.opencv_core.Point
import org.bytedeco.opencv.opencv_core.Point2f
import org.bytedeco.opencv.opencv_core.Scalar
import org.bytedeco.opencv.opencv_highgui.MouseCallback
object ImageChange {
init {
Loader.load(opencv_core::class.java)
}
@JvmStatic
fun main(args: Array<String>) {
perspective(1)
}
/** 透视变换 */
private fun perspective(type: Int) {
// 读取图像
val src = opencv_imgcodecs.imread("C:\\Users\\xuyq\\Desktop\\1.png")
if (src == null || src.empty()) {
return
}
// 创建源点矩阵4个点
val org = Mat(1, 4, opencv_core.CV_32FC2)
org.ptr(0, 0).put<Pointer>(Point2f(0f, 0f))
org.ptr(0, 1).put<Pointer>(Point2f(src.cols().toFloat(), 0f))
org.ptr(0, 2).put<Pointer>(Point2f(src.cols().toFloat(), src.rows().toFloat()))
org.ptr(0, 3).put<Pointer>(Point2f(0f, src.rows().toFloat()))
// 创建目标点矩阵4个点
val dst = Mat(1, 4, opencv_core.CV_32FC2)
if (1 == type) {
val target = click(src)
for (i in target.indices) {
dst.ptr(0, i).put<Pointer>(target[i])
}
} else {
dst.ptr(0, 0).put<Pointer>(Point2f(21f, 20f))
dst.ptr(0, 1).put<Pointer>(Point2f(953f, 74f))
dst.ptr(0, 2).put<Pointer>(Point2f(847f, 574f))
dst.ptr(0, 3).put<Pointer>(Point2f(109f, 643f))
}
// 获取透视变换矩阵
val matrix = opencv_imgproc.getPerspectiveTransform(org, dst)
// 应用透视变换
val images = Mat()
opencv_imgproc.warpPerspective(src, images, matrix, src.size())
// 显示结果
opencv_highgui.imshow("Perspective", images)
opencv_highgui.waitKey(0)
}
/** 平移变换 */
private fun affine() {
// 读取图像
val src = opencv_imgcodecs.imread("C:\\Users\\hyacinth\\Desktop\\1.png")
if (src == null || src.empty()) {
return
}
// 创建源点矩阵三个点
val mat1 = Mat(1, 3, opencv_core.CV_32FC2)
mat1.ptr(0, 0).put<Pointer>(Point2f(0f, 0f))
mat1.ptr(0, 1).put<Pointer>(Point2f(0f, src.rows().toFloat()))
mat1.ptr(0, 2).put<Pointer>(Point2f(src.cols().toFloat(), 0f))
// 创建目标点矩阵三个点
val mat2 = Mat(1, 3, opencv_core.CV_32FC2)
mat2.ptr(0, 0).put<Pointer>(Point2f(src.cols() * 0.3f, src.cols() * 0.3f))
mat2.ptr(0, 1).put<Pointer>(Point2f(src.cols() * 0.2f, src.cols() * 0.6f))
mat2.ptr(0, 2).put<Pointer>(Point2f(src.cols() * 0.7f, src.cols() * 0.2f))
// 获取旋转矩阵
val matrix = opencv_imgproc.getAffineTransform(mat1, mat2)
// 应用透视变换
val images = Mat()
opencv_imgproc.warpAffine(src, images, matrix, src.size())
// 显示结果
opencv_highgui.imshow("Affine", images)
opencv_highgui.waitKey(0)
}
/** 旋转变换 */
private fun rotation() {
// 读取图像
val src = opencv_imgcodecs.imread("C:\\Users\\hyacinth\\Desktop\\1.png")
if (src == null || src.empty()) {
return
}
val center = Point2f((src.cols() / 2).toFloat(), (src.rows() / 2).toFloat())
// 获取旋转矩阵
val matrix = opencv_imgproc.getRotationMatrix2D(center, 45.0, 0.5)
// 应用透视变换
val images = Mat()
opencv_imgproc.warpAffine(src, images, matrix, src.size())
// 显示结果
opencv_highgui.imshow("Rotation", images)
opencv_highgui.waitKey(0)
}
private fun click(image: Mat): List<Point2f> {
// 创建画布(白色背景)
val window = "Click"
// 创建窗口
opencv_highgui.namedWindow(window, opencv_highgui.WINDOW_AUTOSIZE)
val points = listOf<Point2f>().toMutableList()
// 创建鼠标回调对象
val callback = object : MouseCallback() {
override fun call(event: Int, x: Int, y: Int, flags: Int, params: Pointer?) {
when (event) {
opencv_highgui.EVENT_LBUTTONDOWN -> {
println("点击点: ($x, $y)")
points.add(Point2f(x.toFloat(), y.toFloat()))
// 在原图上绘制点
opencv_imgproc.circle(
image, Point(x, y), 5,
Scalar(0.0, 0.0, 255.0, 0.0), -1, opencv_imgproc.LINE_AA, 0
)
opencv_highgui.imshow(window, image)
}
}
}
}
// 设置鼠标回调
opencv_highgui.setMouseCallback(window, callback, null)
// 主循环
while (true) {
opencv_highgui.imshow(window, image)
if (opencv_highgui.waitKey(1).toChar() == 27.toChar() || points.size >= 4) {
opencv_highgui.destroyWindow(window)
break
}
}
return points
}
}