-1

I'm using OpenCV for a object detection project. I'm trying to read frames from a stored video file using VideoCapture, but in OpenCV Java there is no current implementation. I followed instructions in this post: open video file with opencv java, to edit the source files of OpenCV Java to allow this functionality. The problem is I don't know how to recompile the files? - since I just added the downloaded opencv jar file into my eclipse project originally.

Community
  • 1
  • 1
user3019612
  • 259
  • 2
  • 7
  • 15
  • 1
    google "building opencv" and you should get your answer. – Bull Nov 21 '13 at 23:39
  • the missing VideoCapture constructor was fixed in 2.4.7. you won't have to recompile yourself – berak Nov 22 '13 at 08:42
  • Hi berak. I have OpenCV 2.4.7, but the VideoCapture(String filename) constructor is still missing. – user3019612 Nov 22 '13 at 12:59
  • I just successfully used VideoCapture(String fn) yesterday in OpenCV 3.0. It's been moved to the Videoio package I believe. No recompile necessary. You do need to make sure one of the directories with native libs is in your system path though to get it to work (I think its the ffmpeg libs). – medloh Oct 19 '15 at 22:35

1 Answers1

-1

You should probably try JavaCV, an OpenCV wrapper for Java.

This post shows what you need to download/install to get things working on your system, but I'm sure you can find more updated posts around the Web.

One of the demos I present during my OpenCV mini-courses contains a source code that uses JavaCV to load a video file and display it on a window:

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.FrameGrabber;

public class OpenCV_tut4 
{
    public static void main(String[] args) 
    {
        FrameGrabber grabber = new OpenCVFrameGrabber("demo.avi");
        if (grabber == null)
        {
            System.out.println("!!! Failed OpenCVFrameGrabber");
            return;
        }

        cvNamedWindow("video_demo");

        try            
        {
            grabber.start();            // initialize video capture
            IplImage frame = null;  

            while (true)
            {
                frame = grabber.grab(); // capture a single frame               
                if (frame == null)
                {
                    System.out.println("!!! Failed grab");
                    break;
                }

                cvShowImage("video_demo", frame);
                int key = cvWaitKey(33);
                if (key == 27)          // ESC was pressed, abort!
                    break;
            }

        }
        catch (Exception e) 
        {    
            System.out.println("!!! An exception occurred");
        }
    }
}
Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Thanks. I'll look into it. I should also mention the program will be on Android. Is JavaCV going to be fairly similar to OpenCV4Android? – user3019612 Nov 22 '13 at 13:09
  • I don't know, but I know that the JavaCV interface is very very similar to the C API of OpenCV. – karlphillip Nov 22 '13 at 16:31
  • Thanks. Also, I'm hoping you can help with this: I get the exception running your code with the following error message: !!! An exception occurred warning: Error opening file (../../modules/highgui/src/cap_ffmpeg_impl.hpp:529) – user3019612 Nov 22 '13 at 20:20
  • I have latest version of javacv (0.6) and opencv 2.4.6. Do I have to use a specific version of ffmpeg? Because that was an error I had with using opencv 2.4.7 - link error (which was solved by using opencv 2.4.6). I downloaded 'javacv-0.6-cppjars' which had an ffmpeg 2.0.1 jar (which I've added to the build path) - but hasn't solved the problem. – user3019612 Nov 22 '13 at 20:25
  • OpenCV 2.4.7 requires the latest FFmpeg available. In the middle of 2013 I gave a lecture on JavaCV and the softwares installed were: **ffmpeg-­‐20121020-­‐git-­‐04bf2e7-­‐win32-­‐shared.7z**, **OpenCV-­‐2.4.5.exe**, **javacv-­‐0.5-­‐bin.zip**, **jdk-­‐7u25-­‐nb-­‐7_3_1-­‐windows-­‐i586.exe** (optional) and **[vcredist_x86.exe](http://www.microsoft.com/download/en/details.aspx?id=5555)**. – karlphillip Nov 22 '13 at 23:01