java使用opencv

一、windows安装opencv

下载地址:https://opencv.org/releases/
在这里插入图片描述
下载后安装
在这里插入图片描述
本人安装目录
在这里插入图片描述
目录说明:

  • build:基于windows构建
    • java:开发关注
      • x64、x86对应windows操作系统位数
  • sources:开源源码

二、java使用opencv

1.项目中导入jar

  1. 打开项目结构
    在这里插入图片描述
  2. 选择安装目录下的jar包

在这里插入图片描述

三、人脸识别demo

1.demo展示

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class Test {

    public static void main(String[] args) {
        imageFaceDetection();
    }

    /**
     * 人脸检测
     */
    public static void imageFaceDetection() {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        // 从配置文件lbpcascade_frontalface.xml中创建一个人脸识别器,文件位于opencv安装目录中
        CascadeClassifier faceDetector = new CascadeClassifier("D:\\dev\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
        // 读取测试图片
        String imgPath = "C:\\Users\\Administrator\\Desktop\\img\\111.jpg";

        Mat image = Imgcodecs.imread(imgPath);
        HighGui.imshow("获取图片", image);
        HighGui.waitKey(0);
        if (image.empty()) {
            throw new RuntimeException("图片内存为空");
        }

        // 检测脸部
        MatOfRect face = new MatOfRect();
        // 检测图像中的人脸
        faceDetector.detectMultiScale(image, face);
        // 匹配Rect矩阵
        Rect[] rects = face.toArray();
        System.out.println("识别人脸个数: " + rects.length);

        // 识别图片中的所以人脸并分别保存
        int i = 1;
        for (Rect rect : face.toArray()) {

            Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0), 3);
            // 进行图片裁剪
            imageCut(imgPath, "D:\\user\\" + i + ".jpg", rect.x, rect.y, rect.width, rect.height);
            i++;
        }
        // 图片中人脸画框保存到本地
        Imgcodecs.imwrite("D:\\user\\test1.png", image);

        // 展示图片

        HighGui.imshow("人脸识别", image);
        HighGui.waitKey(0);
    }

    /**
     * 裁剪人脸
     *
     * @param readPath 读取文件路径
     * @param outPath  写出文件路径
     * @param x        坐标X
     * @param y        坐标Y
     * @param width    截图宽度
     * @param height   截图长度
     */
    public static void imageCut(String readPath, String outPath, int x, int y, int width, int height) {
        // 原始图像
        Mat image = Imgcodecs.imread(readPath);
        // 截取的区域
        Rect rect = new Rect(x, y, width, height);
        // Mat sub = new Mat(image,rect);
        Mat sub = image.submat(rect);
        Mat mat = new Mat();
        Size size = new Size(width, height);
        // 人脸进行截图并保存
        Imgproc.resize(sub, mat, size);
        Imgcodecs.imwrite(outPath, mat);
    }
}

2.报错分析

错误信息
Exception in thread “main” java.lang.UnsatisfiedLinkError: no opencv_java460 in java.library.path
在这里插入图片描述
需要把安装目录下面的dll文件扔到jdk的bin目录下面

在这里插入图片描述
再次运行,正常执行
后面继续再研究

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值