Windows平台下java使用OpenCV

opencv官网下载 

运行后得到opencv文件夹 

里面有opencv-470的jar包

还有提专门提供给x64和x86平台的java运行所需dll

把jar包添加到依赖

运行时加上-Djava.library.path=C:\\opencv\\build\\java\\x64(我的电脑是64位 所以使用的是x64)

 

 

以下是完整使用demo案例

xml的获取可以从opencv官网找到 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         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>org.example</groupId>
    <artifactId>facedetact</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openpnp</groupId>
            <artifactId>opencv</artifactId>
            <version>4.7.0-0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.15</version>
        </dependency>
    </dependencies>

</project>

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;

public class ServerDetact {
    public static void main(String[] args) {
        ServerDetact serverDetact = new ServerDetact();
        serverDetact.detact();
    }
    public void detact(){
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        String filePath = "C:\\files\\test3.jpg";

        String outputFilePath = "C:\\files\\detacted3.jpg";

        Resource resource = new ClassPathResource("model/haarcascade_frontalface_default.xml");
        File cascadeFile = null;
        try {
            cascadeFile = resource.getFile();
            System.out.println(cascadeFile.length());
        } catch (IOException e) {
            e.printStackTrace();
        }
        CascadeClassifier faceDetector = new CascadeClassifier(cascadeFile.getAbsolutePath());

        Mat image = Imgcodecs.imread(filePath);

        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);

        for (Rect rect : faceDetections.toArray()) {
            Imgproc.rectangle(image, rect.tl(), rect.br(), new Scalar(0, 255, 0), 3);
        }
        Imgcodecs.imwrite(outputFilePath, image);

    }
}

 

效果如下: 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值