Java calls opencv to recognize pictures

foreword

opencv's github address
opencv official website

This article describes how to use java to call opencv

download opencv

The opencv download page downloads the latest installation package according to your computer operating system. I downloaded version 4.7.0 here.

(The opencv-470.jar package in version 4.7.0 is jdk11compiled using , so 需要安装 jdk11no error will be reported when testing later)

Please add a picture description

Run the file after downloading to start extracting the file

Please add a picture description

call opencv

1. Create a lib folder under the resource folder of idea.

2. Copy the opencv-470.jar opencv\build\javaand opencv\build\java\x64opencv_java470.dll dynamic libraries to the lib folder in the two directories under the opencv extraction file directory.

3. File of idea ——> Project Structure ——> Modules ——> Dependencies ——> plus sign, and depend on the opencv-470.jar in the lib directory.

insert image description here

insert image description here

3. Test

package com.hai.tang;

import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import static org.opencv.highgui.HighGui.imshow;
import static org.opencv.highgui.HighGui.waitKey;

public class MainServer {
    
    
    public static void main(String[] args) throws Exception {
    
    
        //加载opencv的动态库
        initOpenCv();
        //加载图片并描绘外形
        drawAllArea();
    }

    public static String filePath = "F:\\opencv\\aaa.jpeg";

    //加载动态库
    public static void initOpenCv() {
    
    
        URL url = ClassLoader.getSystemResource("lib/opencv_java470.dll");
        System.load(url.getPath());
    }

    public static Mat getImageMat(String path) throws Exception {
    
    
        Mat mat = Imgcodecs.imread(path);
        if (mat.empty()) {
    
    
            throw new Exception("image is empty");
        } else {
    
    
            return mat;
        }
    }

    //在图片上绘制图形外形
    private static void drawAllArea() throws Exception {
    
    
        Mat src = getImageMat(filePath);
        Mat gary = new Mat();
        Imgproc.cvtColor(src, gary, Imgproc.COLOR_BGR2GRAY);
        //图像边缘处理
        Mat edges = new Mat();
        Imgproc.Canny(gary, edges, 200, 500, 3, false);
        List<MatOfPoint> list = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();
        //发现轮廓
        Imgproc.findContours(edges, list, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_NONE);
        //画出轮廓
        Imgproc.drawContours(src, list, -1, new Scalar(255, 0, 0), Imgproc.LINE_4, Imgproc.LINE_4);
        imshow("drawAllArea", src);
        waitKey();
    }
}

4. Effect
insert image description here

In the initOpenCv method of the above code, System.load is used to load the dll dynamic link library. There are other ways besides using code to load.

for example:

(1) Configure the dll file path to the environment variable;
(2) Put the dll file in other system folders or jdk/jre folders;
(3) Add the dll file path to the JVM parameters;


reference:

Java calls OpenCV
JAVA calls C/C++ dynamic library development notes

Guess you like

Origin blog.csdn.net/qq_33697094/article/details/131251126