Kotlin Bytedeco OpenCV 图像图像57 图像ROI

Kotlin Bytedeco OpenCV 图像图像57 图像ROI

  • [1 添加依赖](#1 添加依赖)
  • [2 测试代码](#2 测试代码)
  • [3 测试结果](#3 测试结果)

1 添加依赖

kotlin 复制代码
<?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.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_core.Size
import org.bytedeco.opencv.opencv_imgproc.Vec4fVector


object Circles {

    init {
        Loader.load(opencv_core::class.java)
    }

    @JvmStatic
    fun main(args: Array<String>) {
        roi()
    }

    /** ROI */
    private fun roi() {
        // 读取图像
        val src = opencv_imgcodecs.imread("C:\\Users\\xuyq\\Desktop\\2.png")
        if (src == null || src.empty()) {
            return
        }
        val dst = Mat()
        opencv_imgproc.getRectSubPix(
            src,
            Size(100, 100), // 裁剪大小
            Point2f((src.rows() / 2.0).toFloat(), (src.cols() / 2.0).toFloat()), // 裁剪图片中心
            dst
        )
        // 显示ROI
        opencv_highgui.imshow("src", src)
        opencv_highgui.imshow("dst", dst)
        opencv_highgui.waitKey(0)
    }

    /** 圆形检测 */
    private fun circles() {
        // 读取图像
        val src = opencv_imgcodecs.imread("C:\\Users\\hyacinth\\Desktop\\2.png")
        if (src == null || src.empty()) {
            return
        }
        // 中值模糊(滤波-->平滑)
        val img = Mat()
        opencv_imgproc.medianBlur(src, img, 1)
        // 图片转灰色
        val gray = Mat()
        opencv_imgproc.cvtColor(img, gray, opencv_imgproc.COLOR_BGR2GRAY)
        // 霍夫变换-圆形检测
        val point = Vec4fVector()
        opencv_imgproc.HoughCircles(
            gray.getPointer(),
            point,
            opencv_imgproc.HOUGH_GRADIENT,
            1.0,
            20.0,
            50.0,
            30.0,
            20,
            40
        )
        println(point.get().size)

        // 遍历每个圆并绘制到图像上
        for (i in 0 until point.size()) {
            val circle = point[i]
            val x = circle[0].toInt() // 圆心 x 坐标
            val y = circle[1].toInt() // 圆心 y 坐标
            val radius = circle[2].toInt() // 半径

            // 绘制圆心
            opencv_imgproc.circle(
                src,
                Point(x, y),
                5,
                Scalar(0.0, 255.0, 0.0, 0.0), // 绿色圆心
                -1, // 填充
                opencv_imgproc.LINE_AA,
                0
            )

            // 绘制圆周
            opencv_imgproc.circle(
                src,
                Point(x, y),
                radius,
                Scalar(255.0, 0.0, 0.0, 0.0), // 红色圆周
                2, // 边框宽度
                opencv_imgproc.LINE_AA,
                0
            )
        }

        // 显示结果
        opencv_highgui.imshow("Detected Circles", src)
        opencv_highgui.waitKey(0)
    }

}

3 测试结果

相关推荐
Thomas_YXQ1 分钟前
Unity3D BEPUphysicsint定点数3D物理引擎详解
开发语言·3d·unity·unity3d·游戏开发·热更新
z千鑫3 分钟前
【Python】Python之locust压测教程+从0到1demo:基础轻量级压测实战(1)
开发语言·python
黑口罩4 分钟前
【JAVA 基础 第(19)课】Hashtable 类用法和注意细节,是Map接口的实现类
java·开发语言
Code侠客行15 分钟前
Swift语言的多线程编程
开发语言·后端·golang
Code侠客行21 分钟前
Swift语言的软件开发工具
开发语言·后端·golang
黑客老陈23 分钟前
Electron的应用安全测试基础 | 安装与检测基于Electron的应用程序
开发语言·javascript·网络·安全·web安全·electron·策略模式
d3f4u1t1 小时前
PHP反序列化
开发语言·php
2501_901839521 小时前
Ruby语言的软件开发工具
开发语言·后端·golang
蒙娜丽宁1 小时前
【Python】深入探讨Python中的单例模式:元类与装饰器实现方式分析与代码示例
开发语言·python·单例模式