Home Convert Code of Open CV to Java CV and enable GPU.
Reply: 0

Convert Code of Open CV to Java CV and enable GPU.

user8907
1#
user8907 Published in April 26, 2018, 5:32 am

I have written code in Open CV which work fine, now I want to enable the GPU to Speed up the process. Currently it takes 1 sec to process 1 Image, I have also multi-threaded the code, not in the below snippet of the code, but still need more performance.

The main thing which take time in the below code is extracting the key points from the image and matching it with the object image.

I have done some research and found that I have to write code in C++ and access through JNI. Also some people suggested that we can enable GPU in javaCV, but didn’t find any code and example.

Below is the code snippet which I want to enable GPU.

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;

import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.features2d.DMatch;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.KeyPoint;
import org.opencv.highgui.Highgui;

/**
 * Created by Kinath on 8/6/2016.
 */

public class SURFDetector {

public static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");

public static void main(String[] args) {

    File lib = null;
    String os = System.getProperty("os.name");
    String bitness = System.getProperty("sun.arch.data.model");

    if (os.toUpperCase().contains("WINDOWS")) {
        if (bitness.endsWith("64")) {
            lib = new File("lib//" + System.mapLibraryName("opencv_java2410"));
        } else {
            lib = new File("libs//x86//" + System.mapLibraryName("opencv_java2410"));
        }
    }

    System.out.println(lib.getAbsolutePath());
    System.load(lib.getAbsolutePath());

    String bookObject = "images//object.jpg";
    File dir = new File("images//");

    if(!dir.isDirectory()) {
        System.err.println("Not a directory ");
        return;
    }

    Mat sceneImage;
    System.out.println("Started....");
    System.out.println("Loading images...");
    Mat objectImage = imread(bookObject);


    for(File image: dir.listFiles()) {

        System.err.println("Image path is "+image.getAbsolutePath());
        sceneImage = imread(image.getAbsolutePath());
        try {
            compare(sceneImage, objectImage, image);
        }catch(Exception e ) {
            e.printStackTrace();
        }

    }

}

public static void compare(Mat sceneImage, Mat objectImage , File path) {

    KeyPointVector objectKeyPoints = new KeyPointVector();
    FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SURF);
    System.out.println(sdf.format(System.currentTimeMillis())+ " Detecting key points...");
    featureDetector.detect(objectImage, objectKeyPoints);
    System.out.println(sdf.format(System.currentTimeMillis())+ " Detect");
    KeyPoint[] keypoints = objectKeyPoints.toArray();
    System.out.println(sdf.format(System.currentTimeMillis())+ " To Array");
    //System.out.println(keypoints);

    MatOfKeyPoint objectDescriptors = new MatOfKeyPoint();
    DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    System.out.println(sdf.format(System.currentTimeMillis())+ " Computing descriptors...");
    descriptorExtractor.compute(objectImage, objectKeyPoints, objectDescriptors);

    // Create the matrix for output image.
    Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar newKeypointColor = new Scalar(255, 0, 0);

    System.out.println(sdf.format(System.currentTimeMillis())+ " Drawing key points on object image...");
    Features2d.drawKeypoints(objectImage, objectKeyPoints, outputImage, newKeypointColor, 0);

    // Match object image with the scene image
    MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();
    MatOfKeyPoint sceneDescriptors = new MatOfKeyPoint();
    System.out.println(sdf.format(System.currentTimeMillis())+ " Detecting key points in background image...");
    featureDetector.detect(sceneImage, sceneKeyPoints);
    System.out.println(sdf.format(System.currentTimeMillis())+" Computing descriptors in background image...");
    descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors);

    Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar matchestColor = new Scalar(0, 255, 0);

     List<MatOfDMatch> matches = new LinkedList<MatOfDMatch>();
     DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
     System.out.println(sdf.format(System.currentTimeMillis())+ " Matching object and scene images...");
     descriptorMatcher.knnMatch(objectDescriptors, sceneDescriptors, matches, 2);

     System.out.println(sdf.format(System.currentTimeMillis())+" Calculating good match list...");
     LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();

     float nndrRatio = 0.7f;

     for (int i = 0; i < matches.size(); i++) {
         MatOfDMatch matofDMatch = matches.get(i);
         DMatch[] dmatcharray = matofDMatch.toArray();
         DMatch m1 = dmatcharray[0];
         DMatch m2 = dmatcharray[1];

         if (m1.distance <= m2.distance * nndrRatio) {
             goodMatchesList.addLast(m1);
         }
     }

     if (goodMatchesList.size() >= 7) {

         System.out.println(sdf.format(System.currentTimeMillis())+ " Object Found!!!");

         List<KeyPoint> objKeypointlist = objectKeyPoints.toList();
         List<KeyPoint> scnKeypointlist = sceneKeyPoints.toList();

         LinkedList<Point> objectPoints = new LinkedList<>();
         LinkedList<Point> scenePoints = new LinkedList<>();

         for (int i = 0; i < goodMatchesList.size(); i++) {
             objectPoints.addLast(objKeypointlist.get(goodMatchesList.get(i).queryIdx).pt);
             scenePoints.addLast(scnKeypointlist.get(goodMatchesList.get(i).trainIdx).pt);
         }

         MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();
         objMatOfPoint2f.fromList(objectPoints);
         MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();
         scnMatOfPoint2f.fromList(scenePoints);

         Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);

         Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2);
         Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2);

         obj_corners.put(0, 0, new double[]{0, 0});
         obj_corners.put(1, 0, new double[]{objectImage.cols(), 0});
         obj_corners.put(2, 0, new double[]{objectImage.cols(), objectImage.rows()});
         obj_corners.put(3, 0, new double[]{0, objectImage.rows()});

         System.out.println(sdf.format(System.currentTimeMillis())+ " Transforming object corners to scene corners...");
         Core.perspectiveTransform(obj_corners, scene_corners, homography);

         Mat img = Highgui.imread(path.getAbsolutePath(), Highgui.CV_LOAD_IMAGE_COLOR);

         Core.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)), new Scalar(0, 255, 0), 4);
         Core.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)), new Scalar(0, 255, 0), 4);
         Core.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)), new Scalar(0, 255, 0), 4);
         Core.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)), new Scalar(0, 255, 0), 4);

        System.out.println(sdf.format(System.currentTimeMillis())+ " Drawing matches image...");
         MatOfDMatch goodMatches = new MatOfDMatch();
         goodMatches.fromList(goodMatchesList);

         Features2d.drawMatches(objectImage, objectKeyPoints, sceneImage, sceneKeyPoints, goodMatches, matchoutput, matchestColor, newKeypointColor, new MatOfByte(), 2);

         Highgui.imwrite("output//outputImage.jpg", outputImage);
         Highgui.imwrite("output//matchoutput.jpg", matchoutput);
         Highgui.imwrite("output//"+goodMatchesList.size() + " - " +path.getName() + getExt(path.getName()) , img);
     } else {
         System.out.println("Object Not Found");
     }

     System.out.println("Ended....");
     System.out.println("......................................................");

     System.out.println("     ");
}

public static String getExt(String name){
    if(name.contains(".")) name = name.substring(name.lastIndexOf("."));
    return name;
 }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317384 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO