1. 程式人生 > >Java呼叫Opencv及處理並行化

Java呼叫Opencv及處理並行化

一、開發環境配置

IDE及開發環境的配置不做詳細描述,網上參考文件很多,參考連結1為一個示例

二、使用Java呼叫OpenCV java API進行圖片縮放

main.java檔案內容如下

import java.io.File;
import org.opencv.core.*;
import org.opencv.imgcodecs.*;
import org.opencv.imgproc.Imgproc;

public class main {
    static {System.loadLibrary (Core.NATIVE_LIBRARY_NAME);}

    public static void run_test_case (File infile, File outfile, int resize_width, int resize_height) {
        Mat out_img = new Mat();

        Size size = Imgcodecs.imsize (infile.getAbsolutePath());

        double w_ratio = (double) resize_width / size.width;
        double h_ratio = (double) resize_height / size.height;

        double ratio = (size.width * h_ratio > resize_width) ? h_ratio : w_ratio;

        resize_width = (int) (size.width * ratio + 0.5);
        resize_height = (int) (size.height * ratio + 0.5);

        Mat in_img = Imgcodecs.imread (infile.getAbsolutePath());
        Imgproc.resize (in_img, out_img, new Size (resize_width, resize_height), 0, 0, Imgproc.INTER_LANCZOS4);

        Imgcodecs.imwrite (outfile.getAbsolutePath(), out_img);

        System.out.println (out_img);
    }



    public static void main (String[] args) {
        if (args.length != 5) {
            System.out.println ("Usage:java main input_file|input_dir output_file|output_dir resize_width resize_height");
            return;
        }

        // Get parameters
        File infile = new File (args[0]);
        File outfile = new File (args[1]);
        int resize_width = Integer.parseInt (args[2]);
        int resize_height = Integer.parseInt (args[3]);;

        //time stamp
        long time_start, time_end, time_diff;

        //process
        if (infile.isFile()) {
            time_start = System.currentTimeMillis();

            run_test_case (infile, outfile, resize_width, resize_height);

            time_end = System.currentTimeMillis();
            time_diff = time_end - time_start;
            System.out.println ("time = " + time_diff / 1000.0 + "s\n");
        }
        else if (infile.isDirectory()) {
            if (outfile.exists() == false) {
                outfile.mkdirs();
            }

            String[] files = infile.list();

            time_start = System.currentTimeMillis();

            // omp parallel for threadNum(56)
            for (int i = 0; i < files.length; i++) {
                File in_file = new File (infile.getAbsolutePath() + "/" + files[i]);
                File out_file = new File (outfile.getAbsolutePath() + "/" + files[i]);
                run_test_case (in_file, out_file, resize_width, resize_height);
            }

            time_end = System.currentTimeMillis();
            time_diff = time_end - time_start;
            System.out.println ("time = " + time_diff / 1000.0 + "s\n");
        }


        // omp parallel
        {
           System.out.println("Thread #" + OMP4J_THREAD_NUM + "/" + OMP4J_NUM_THREADS);
        }
    }

}

並行化處理:

使用omp4j進行了for迴圈的並行化處理,也可以使用Jomp。

下載omp4j-1.2.jar,並在啟動指令碼中設定目錄引數


啟動指令碼如下:
#!/bin/bash -eu

OPENCV_JAR_DIR=/usr/local/share/OpenCV/java/opencv-310.jar
OMP4J='java -jar ./lib/omp4j-1.2.jar'

#compile
#javac -cp .:$OPENCV_JAR_DIR main.java
$OMP4J -cp .:$OPENCV_JAR_DIR main.java

#run
java -cp .:$OPENCV_JAR_DIR main input.jpg output.jpg 1080 1080


參考:

1.https://segmentfault.com/a/1190000000358809#articleHeader0

2.http://docs.opencv.org/java/3.1.0/

3.http://www.omp4j.org/home