Ask Your Question

RolandC's profile - activity

2016-01-07 01:39:35 -0500 answered a question Distribute Opencv java project?

You need to distribute only the jar file and the libraries for the given operating system. There's no need for the users to have OpenCV "installed".

A possibility would be that you distribute a compressed / extractable package (tar, zip, jar, etc) with all the classes and libraries, including a shell command to start the application.

You can read about how to start the application here.

2015-12-12 02:48:35 -0500 commented question Connect to public IP camera in Java

Check this answer here: ... I quickly tried with appending "?x.mjpeg" to your url and it worked (x can be anything). Although I don't know why, so I'm just adding that as comment.

2015-12-05 00:59:38 -0500 commented question Can someone help me to translate this line of code in java?

@StevenPuttemans: What's worse? Someone not googling or someone saying google it yourself when the answer is just a few lines. Clearly @Raymond doesn't know how to program. As someone who likes to help others, I read the question only to see that comment by @LorenaGdL. A waste of time. Why does she write at all instead of simply ignoring the user? And regarding "no useful comments", what's her "she, please :P" and your "changed :D" then?

2015-12-04 01:44:21 -0500 commented question Can someone help me to translate this line of code in java?

@LorenaGdL: You are wasting everyones time by posting that as comment instead of providing the answer.

2015-12-04 01:42:32 -0500 answered a question Can someone help me to translate this line of code in java?
if (area >= 30 && Math.abs(1 - ((double)rect.width / (double)rect.height)) <= 0.2 && Math.abs(1 - (area / (Math.PI * Math.pow(radius, 2)))) <= 0.2)
2015-11-19 01:01:25 -0500 received badge  Necromancer (source)
2015-11-19 00:56:43 -0500 answered a question Running a Java Program Referencing OpenCV's Jar File

Assuming you have this folder structure:


with the Main class having the package declaration "application" and your command prompt being in the folder opencv you can start the app this way:

java -cp .;lib/opencv-300.jar -Djava.library.path=lib/x64 application.Main

The "." is needed for the main class, the jar is needed for the main class, the library is needed for the jar.

2015-09-22 09:50:41 -0500 commented answer Generating Java bindings with VideoWriter

Thanks. Is there an approximate release date or a nightly build?

2015-09-22 09:49:42 -0500 received badge  Scholar (source)
2015-09-22 00:42:37 -0500 received badge  Enthusiast
2015-09-21 02:55:57 -0500 asked a question Generating Java bindings with VideoWriter

As has been reported a year ago the VideoWriter class isn't generated for Java bindings. That's a very long time for not being fixed. It doesn't seem to be fixed in OpenCV 2.4.8 and it isn't fixed in OpenCV 3.

How can I fix this myself for OpenCV 3 used in Windows 7 x64? Or is there some kind of "nightly build" which has that problem fixed?

2015-09-20 10:31:20 -0500 answered a question OpenCV Run on Tomcat

My guess is that the OpenCV library isn't loaded. If you'd like to do it with loadLibrary you must specify a library path when you start the VM:


Also, the libary must be without the ".dll" extension.

In other words:

OpenCV's looks like this:

public static final String NATIVE_LIBRARY_NAME = getNativeLibraryName();
private static String getNativeLibraryName() { return "opencv_java300"; }

Then you load the library like this:

2015-09-20 10:28:00 -0500 answered a question How to get fps from video in java?

Here's a solution with OpenCV 3:

    VideoCapture videoCapture = new VideoCapture();"c:/movies/mymovie.mp4");
    double fps = videoCapture.get(Videoio.CAP_PROP_FPS);
    System.out.println( "fps: " + fps);
2015-09-20 10:22:49 -0500 answered a question opencv3 videoCapture doesn't work

You need to load the ffmpeg dll as well. You could create a folder structure


and add the jar to your build path.

Then in your application load the libraries e. g. like this:

// set the library path during runtime (or specify it at program start via -Djava.library.path=...)
try {

    System.setProperty("java.library.path", "lib/x64");

    Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
    fieldSysPath.set(null, null);

} catch (Exception ex) {
    throw new RuntimeException(ex);

// load the required libraries

The video is now playable with

VideoCapture videoCapture = new VideoCapture();"c:/movies/mymovie.mp4");
2015-09-20 10:12:40 -0500 answered a question VideoCapture can not open some files

What you are missing in your code is the loading of the ffmpeg dll. Just put the opencv_ffmpeg300_64.dll from the OpenCV installation into the java library folder, i. e. where the opencv_java300.dll resides and add this to your code:


Just tried it, works fine.

ps: Where did you get these videos, are they free to use? I just started with OpenCV and java yesterday and would need some samples.

2015-09-20 03:21:31 -0500 received badge  Supporter (source)
2015-09-20 01:24:08 -0500 answered a question Take frame after some millisecond

You could do it e. g. this way:


VideoCapture videoCapture = new VideoCapture();;


Mat mat = new Mat();
Mat prevMat = null;
Mat diffMat = new Mat();

while( true) {

    // get frame;

    // difference
    if( prevMat != null) {
        Core.absDiff(mat, prevMat, diffMat);

    // remember mat
    prevMat = mat.clone();

    // wait

The difference image is in diffMat.

2015-09-20 01:06:10 -0500 answered a question Error when lunching the runnable jar file

You are missing the opencv_java300.dll in your library path. You could either specify its location by using -Djava.library.path=... when you start the program or you could use this piece of code in your main method:

    System.setProperty("java.library.path", ".");

    Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
    fieldSysPath.setAccessible( true );
    fieldSysPath.set( null, null );

The problem with the build path in eclipse is that you need to use an absolute path for the native library location. Nobody wants that. With the code above you remap the library path to a relative path, so when you copy your project or rename it, you don't have to mess around with the build path settings. Your code would still work.

The "." means that in your created jar the dlls must be in the same folder as the jar. You could of course use a sub-folder, eg ./lib/x64. Then you need to have the libraries in that sub-folder in both eclipse and your jar's location.

2015-09-18 10:22:34 -0500 received badge  Editor (source)
2015-09-18 10:20:57 -0500 commented answer OpenCV Face detection works on PC but not on Laptop

Thank you, but that didn't work either, i. e. no errors shown. I even created a JAR and executed exactly the same code on both systems. It works on the PC, but not on the Laptop. It does however work with the haar cascades on both systems. A note: the getPath() doesn't work on windows, you have to use getPath().substring(1). Better yet would be to use Paths.get(...) to get a proper path. Anyway, problem isn't solved unfortunately.

2015-09-17 23:39:39 -0500 asked a question OpenCV Face detection works on PC but not on Laptop

I was trying out the face detection mechanism of OpenCV 3.0 using Java 8u40, i. e. the very simple HelloOpenCV example. Exactly the same code and libraries on PC and Laptop.


The face gets detected on my PC, but not on my laptop. Both are Win7 x64. I tried executing the code in a development environment and I created a JAR and executed it on both system. Same problem. The face detection doesn't work on the laptop. However, I found out that the haar cascades work on the laptop.


Does anyone how it could be possible that the face gets detected with exactly the same code on the one system and not on the other?

Is there some way to activate OpenCV logging in the Java version? There aren't any exceptions.


Code is the HelloOpenCV code from here:

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.objdetect.CascadeClassifier;
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
class DetectFaceDemo {
  public void run() {
    System.out.println("\nRunning DetectFaceDemo");
    // Create a face detector from the cascade file in the resources
    // directory.
    CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/lbpcascade_frontalface.xml").getPath());
    Mat image = Imgcodecs.imread(getClass().getResource("/lena.png").getPath());
    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);
    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
    // Draw a bounding box around each face.
    for (Rect rect : faceDetections.toArray()) {
        Imgproc.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.
    String filename = "faceDetection.png";
    System.out.println(String.format("Writing %s", filename));
    Imgcodecs.imwrite(filename, image);
public class HelloOpenCV {
  public static void main(String[] args) {
    System.out.println("Hello, OpenCV");
    // Load the native library.
    new DetectFaceDemo().run();

Thank you very much for the help!