Ask Your Question

ggl's profile - activity

2019-02-12 14:37:52 -0600 received badge  Famous Question (source)
2016-07-04 06:29:12 -0600 received badge  Notable Question (source)
2015-06-04 03:07:52 -0600 received badge  Popular Question (source)
2013-07-05 15:45:23 -0600 commented answer OpenCV matchTemplate CUDA large images & templates

I just ran the code on a HP Elitebook with a Quadro 3000M running CUDA 5.0. Initiation time is now only 2040 ms. It's probably driver related as well.

2013-07-03 06:50:19 -0600 commented answer OpenCV matchTemplate CUDA large images & templates

Thanks for this clarification. I understand the multiple run requirement, which will indeed be the case for this match set-up as well (i.e. normally I will run at least 9 to 49 templates against the same image).

Your suggestion does not lead to any significant improvement, however. Initiation time stays around 26 seconds, even if I use smaller input images. I still do not understand whether this is specific to OpenCV gpu implementation. I am running a pure-CUDA phase correlation based on cuFFT, which gives similar results as matchTemplate, but does not seem to have an excessive initiation penalty. It runs in 1710 ms total (330 ms for the GPU part) with the same images as before.

2013-07-02 10:20:30 -0600 asked a question OpenCV matchTemplate CUDA large images & templates

Dear All,

I am interested in using template matching on large (satellite) images (at least 8192 by 8192 pixels), using templates from reference image sets that are typically 256 by 256 or 512 by 512 pixels in size. A normal use case is matching N by N templates against the image (N=5,7,9...).

I am using OpenCV 2.4.6 with CUDA 4.2. I managed to get the gpu version of matchTemplate going, but ran into the initiation timing issue. This causes the gpu version to be slower than the cpu version, when used in a single image/single template match. I have done careful timing analysis (see code below) and find that the code is spending 98% of the time on initiation. I know that this has to do with the JIT compilation of the CUDA related code, but the reference to check this further in the documentation on the nvcc compiler and the CUDA_DEVCODE_CACHE environment variable is leading nowhere to a solution (I set the environment variable, but nothing improves).

This should be a compile once, run often code case, so if someone got the code caching working correctly, I'd appreciate if that knowledge could be shared.

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/gpu/gpu.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/// Global Variables
Mat img; 
Mat templ; 
Mat result;

int match_method;

/** @function main 

  Stripped down version, without GUI functionality 

*/
int main( int argc, char** argv )
{
  /// Load image and template
  img = imread( argv[1], 1 );
  templ = imread( argv[2], 1 );

  match_method = atoi(argv[2]);

  int result_cols =  img.cols - templ.cols + 1;
  int result_rows = img.rows - templ.rows + 1;

  result.create( result_cols, result_rows, CV_32F);

  size_t t0 = clock();
  try
  {
    gpu::printCudaDeviceInfo(gpu::getDevice());
    gpu::resetDevice(); 
  }
  catch (const std::exception& e)
  {
    //no GPU, DLL not compiled with GPU
    printf("Exception thrown: %s\n", e.what());
    return 0;
  }

  size_t t1 = clock();
  printf("GPU initialize: %f ms\n", (double(t1 - t0)/CLOCKS_PER_SEC*1000.0));

  gpu::GpuMat d_src, d_templ, d_dst;

  d_templ.upload(templ);
  printf("GPU load templ: %f ms\n", (double(clock() - t1)/CLOCKS_PER_SEC*1000.0));
  d_src.upload(img);
  printf("GPU load img: %f ms\n", (double(clock() - t1)/CLOCKS_PER_SEC*1000.0));
  //d_templ.upload(templ);
  //printf("GPU load templ: %f ms\n", (double(clock() - t1)/CLOCKS_PER_SEC*1000.0));
  d_dst.upload(result);
  printf("GPU load result: %f ms\n", (double(clock() - t1)/CLOCKS_PER_SEC*1000.0));

  /// Do the Matching
  size_t t2 = clock();

  printf("GPU memory set-up: %f ms\n", (double(t2 - t1)/CLOCKS_PER_SEC*1000.0));

  gpu::matchTemplate( d_src, d_templ, d_dst, match_method );

  size_t t3 = clock();
  printf("GPU template match: %f ms\n", (double(t3 - t2)/CLOCKS_PER_SEC*1000.0));

  /// Localizing the best match with minMaxLoc
  double minVal; double maxVal; Point minLoc; Point maxLoc;
  Point matchLoc;

  gpu::minMaxLoc( d_dst, &minVal, &maxVal, &minLoc, &maxLoc);

  size_t t4 = clock();
  printf("GPU minMaxLoc: %f ms\n", (double(t4 - t3)/CLOCKS_PER_SEC*1000.0));

  /// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the ...
(more)
2013-07-02 04:29:32 -0600 commented answer Unable to build documentation

Even after installing sphinx, the doc build will not work after a new cmake. There is a problem in cmake/OpenCVDetectPython.cmake. The line

if(SPHINX_OUTPUT MATCHES "^Sphinx v([0-9][^ \n]*)")

does not work correctly. Only if I force SPHINX_VERSION to 1.2 (my version) the documentation will built (both HTML and PDF).

The problem is related to executing sphinx-build, which returns a multi-line response, which includes the Sphinx version as the second line. I guess the MATCHES statement does not handle that correctly (I am not a cmake expert, though).

I am using the git procedure to build from source (2.4).

2013-06-13 10:46:18 -0600 answered a question JNI UnsatisfiedLinkError when running ant sample on Ubuntu

After a bit of extra work, I got the groovy problem solved as well! For some reason the System.loadLibrary(CORE.NATIVE_LIBRARY_NAME) does not work well with Groovy.

I wrote the following (somewhat trivial) Java class:

import org.opencv.core.Core;
public class LibLoader {
  public static void load() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  }
}

Compile with:

javac -cp ../build/bin/opencv-245.jar:. LibLoader.java
In the groovy script use the static class as follows:
LibLoader.load()
I still have to pre-compile first with groovyc and then run the compiled java class. Here is the DetectFaceDemo as a groovy script:
/*
 * Detects faces in an image, draws boxes around them, and writes the results
 * to "faceDetection.png".
 */

LibLoader.load()

println("\nRunning DetectFaceDemo")

// Create a face detector from the cascade file in the resources directory.
// Note: I got rid of the getClass.getResource.getPath construction (lazy)
def faceDetector = new CascadeClassifier("resources/lbpcascade_frontalface.xml")
def image = Highgui.imread("resources/AverageMaleFace.jpg")

// Detect faces in the image.
// MatOfRect is a special container class for Rect.
def faceDetections = new MatOfRect()
faceDetector.detectMultiScale(image, faceDetections)

println(String.format("Detected %s faces", faceDetections.toArray().length))

// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
  Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x
      + rect.width, rect.y + rect.height), new Scalar(0, 255, 0))
}

// Save the visualized detection.
def filename = "faceDetection.png"
println(String.format("Writing %s", filename))
Highgui.imwrite(filename, image)

Compile and run with:

groovyc -cp ../build/bin/opencv-245.jar:. DetectFace.groovy
java -cp ../build/bin/opencv-245.jar:/usr/local/groovy/embeddable/groovy-all-2.1.4.jar:. -Djava.library.path=../build/lib DetectFace

Results:

Running DetectFaceDemo
Detected 1 faces
Writing faceDetection.png

I reckon I will get groovy to work without the need for compilation first (there seems to be a problem with java.library.path).

What's neat about groovy is the simplicity of the code. I used to work with Java Advanced Imaging, but now that OpenCV has gpu-enabled routines, I want to go this road.

2013-06-13 05:47:34 -0600 received badge  Editor (source)
2013-06-13 05:45:06 -0600 answered a question JNI UnsatisfiedLinkError when running ant sample on Ubuntu

I get this error ONLY when I try to run as a groovy script, but not when I use the java classes. I use OpenCV 2.4.5 (installed via git as suggested in the intro to Java development OpenCV 2.4.5 document). Instead of using the ant build, I simply compile and then run SimpleSample.java which works fine, as in:

javac -cp ../build/bin/opencv-245.jar:. SimpleSample.java
java -cp ../build/bin/opencv-245.jar:. -Djava.library.path=../build/lib SimpleSample

My goal is to run this in groovy. I have groovy-fied SimpleSample.java as follows:

import org.opencv.core.Core
import org.opencv.core.Mat
import org.opencv.core.CvType
import org.opencv.core.Scalar

System.loadLibrary(Core.NATIVE_LIBRARY_NAME)
println("Welcome to OpenCV " + Core.VERSION)
def m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0)) 
println("OpenCV Mat: " + m)
...

I compile this with groovyc and then run it with java, which throws the exception.

groovyc -cp ../build/bin/opencv-245.jar:. TestGroovy.groovy
java -cp ../build/bin/opencv-245.jar:/usr/local/groovy/embeddable/groovy-all-2.1.4.jar:. -Djava.library.path=../build/lib TestGroovy

Welcome to OpenCV 2.4.5.0
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat(IIIDDDD)J