Ask Your Question

Revision history [back]

Android Static Initialization

Hi guys, I'm writing a program that's using Histogram Comparison. I'm able to make it work but with OpenCV Manager. However, I need to use without it. I have look at the tutorial provided by the OpenCv regarding static initialization. But it doesn't work on me. I don't have the JNI part. I'm using the Java version of the OpenCV codes. Is there a way to implement this without having the JNI part? I mean, without the C++ codes? Please help me guys. I already have all the necessary .so files in my project libs.

Currently here's my code for initializing the libs:

   static
{
    whether(!OpenCVLoader.initDebug()) {
        Log.d("ERROR", "Unable to load OpenCV");
    } else {
        Log.d("SUCCESS", "OpenCV loaded");
    }
}


private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
     @Override
     public void onManagerConnected(int status) {
       switch (status) {
           case LoaderCallbackInterface.SUCCESS:
           { 

               } break;


           default:
           {
               super.onManagerConnected(status);
           } break;    
       }
     }

   };



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mOpenCVCallBack))
    {

    }
    else{  
    System.out.println(java.lang.Runtime.getRuntime().maxMemory()); }
    setContentView(R.layout.camera);
}

And here's how I'm implementing Histogram comparison:

@Override
public double StartMatch(Bitmap src, Bitmap tmp) {

    Mat src_base = new Mat();
    Mat src_test1 = new Mat();

    Utils.bitmapToMat(tmp, src_base);
    Utils.bitmapToMat(src, src_test1);

    Imgproc.cvtColor(src_base, src_base, Imgproc.COLOR_RGB2HSV);
    Imgproc.cvtColor(src_test1, src_test1, Imgproc.COLOR_RGB2HSV);

    int h_bins = 50; int s_bins = 60;
    MatOfInt histSize = new MatOfInt(h_bins, s_bins );

    MatOfInt channels = new MatOfInt(0,1);
    MatOfFloat ranges = new MatOfFloat(0, 180, 0, 255);

    Mat hist_base = new Mat();
    Mat hist_test1 = new Mat();

    ArrayList<Mat> hsv_base = new ArrayList<Mat>();
    hsv_base.add(src_base);

    ArrayList<Mat> hsv_test1 = new ArrayList<Mat>();
    hsv_test1.add(src_test1);


    Imgproc.calcHist(hsv_base, channels, new Mat(), hist_base, histSize, ranges, false);
    Core.normalize(hist_base, hist_base, 0, 1, Core.NORM_MINMAX, -1, new Mat());

    Imgproc.calcHist(hsv_test1, channels, new Mat(), hist_test1, histSize, ranges, false);
    Core.normalize(hist_test1, hist_test1, 0, 1, Core.NORM_MINMAX, -1, new Mat());

    double test1 = Imgproc.compareHist(hist_base, hist_test1, Imgproc.CV_COMP_CORREL);

    double ans = test1 * 100;
    return  ans;

    }
click to hide/show revision 2
retagged

updated 2015-02-18 17:18:29 -0600

berak gravatar image

Android Static Initialization

Hi guys, I'm writing a program that's using Histogram Comparison. I'm able to make it work but with OpenCV Manager. However, I need to use without it. I have look at the tutorial provided by the OpenCv regarding static initialization. But it doesn't work on me. I don't have the JNI part. I'm using the Java version of the OpenCV codes. Is there a way to implement this without having the JNI part? I mean, without the C++ codes? Please help me guys. I already have all the necessary .so files in my project libs.

Currently here's my code for initializing the libs:

   static
{
    whether(!OpenCVLoader.initDebug()) {
        Log.d("ERROR", "Unable to load OpenCV");
    } else {
        Log.d("SUCCESS", "OpenCV loaded");
    }
}


private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
     @Override
     public void onManagerConnected(int status) {
       switch (status) {
           case LoaderCallbackInterface.SUCCESS:
           { 

               } break;


           default:
           {
               super.onManagerConnected(status);
           } break;    
       }
     }

   };



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mOpenCVCallBack))
    {

    }
    else{  
    System.out.println(java.lang.Runtime.getRuntime().maxMemory()); }
    setContentView(R.layout.camera);
}

And here's how I'm implementing Histogram comparison:

@Override
public double StartMatch(Bitmap src, Bitmap tmp) {

    Mat src_base = new Mat();
    Mat src_test1 = new Mat();

    Utils.bitmapToMat(tmp, src_base);
    Utils.bitmapToMat(src, src_test1);

    Imgproc.cvtColor(src_base, src_base, Imgproc.COLOR_RGB2HSV);
    Imgproc.cvtColor(src_test1, src_test1, Imgproc.COLOR_RGB2HSV);

    int h_bins = 50; int s_bins = 60;
    MatOfInt histSize = new MatOfInt(h_bins, s_bins );

    MatOfInt channels = new MatOfInt(0,1);
    MatOfFloat ranges = new MatOfFloat(0, 180, 0, 255);

    Mat hist_base = new Mat();
    Mat hist_test1 = new Mat();

    ArrayList<Mat> hsv_base = new ArrayList<Mat>();
    hsv_base.add(src_base);

    ArrayList<Mat> hsv_test1 = new ArrayList<Mat>();
    hsv_test1.add(src_test1);


    Imgproc.calcHist(hsv_base, channels, new Mat(), hist_base, histSize, ranges, false);
    Core.normalize(hist_base, hist_base, 0, 1, Core.NORM_MINMAX, -1, new Mat());

    Imgproc.calcHist(hsv_test1, channels, new Mat(), hist_test1, histSize, ranges, false);
    Core.normalize(hist_test1, hist_test1, 0, 1, Core.NORM_MINMAX, -1, new Mat());

    double test1 = Imgproc.compareHist(hist_base, hist_test1, Imgproc.CV_COMP_CORREL);

    double ans = test1 * 100;
    return  ans;

    }