2
    import org.opencv.core.Core;        
    import org.opencv.core.Mat;        
    import org.opencv.core.Size;        
    import org.opencv.highgui.Highgui;        
    import org.opencv.highgui.VideoCapture;        
    import org.opencv.imgproc.Imgproc;        

    public class Video        
    {        
        public static void main(String[] args)        
        {        
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);        

    VideoCapture cap = new VideoCapture(0);

    cap.open(1);

    if(!cap.isOpened())
    {
        System.out.println("No camera");
    }

    else
    {
        System.out.println("Yes. Camera");
    }

    Mat frame = new Mat();
    cap.retrieve(frame);

    Highgui.imwrite("me1.jpg", frame);
    Mat frameBlur = new Mat();

    Imgproc.blur(frame, frameBlur, new Size(5,5));
    Highgui.imwrite("me2-blurred.jpg", frameBlur);

    Imgproc.GaussianBlur(frame, frameBlur, new Size(25, 25), 20);
    Highgui.imwrite("me3-blurred.jpg", frameBlur);

    cap.release();
        }
    }

I have used this code to open my Camera device and capture 3 different frames and made some operations on it. But, I couldn't open a file like .avi/.mpg/.mp4 etc., using {n_open} method of VideoCapture. There is a method in the VideoCapture implementation here. But because its a private and native method, that method can't be accesses using VideoCapture's object.

Could some one help how to do that using pure OpenCV 2.4.6 and Java
(Please dont suggest solution using Processing libraries)

Vishnu
  • 159
  • 1
  • 3
  • 9
  • 1
    Have you tried to open video file with `VideoCapture cap = new VideoCapture("C:\\Somefile.avi");`? If this doesn't work, see here: http://stackoverflow.com/questions/8414947/why-cant-i-open-avi-video-in-opencv – Simon G. Sep 25 '13 at 05:40

2 Answers2

2

Take a look on OpenCV 2.4.8. The VideoCapture API has been extended of public VideoCapture(String filename) method. The question remains why this feature has been implemented so late.

If using of recent version of OpenCV isn't acceptable for you for some reason, you have several options:

  • Rebuild OpenCV by yourself with this method marked as public

  • HACKY ONE: Make your copy of VideoCapture class (or extend original one and play with reflection) with public VideoCapture(String) constructor. Then give support for native method private static native long n_VideoCapture(java.lang.String filename) by creating your own DLL using C++ OpenCV API. (Tested!)

lukk
  • 3,164
  • 1
  • 30
  • 36
  • The API has changed, but I still can't load any of my .avi, .mpeg (even tree.avi available in opencv, which does open while executing OpenCV c samples), in 2.4.7. Has any of you made it possible? Are you using some publicly available video? Kind regards, – dannyxyz22 Jan 19 '14 at 03:33
  • Actually, it didn't work when using x86_64 but when using x86 it did work. It probably has got to do with loading ffmpeg dlls, but I haven't found exactly what it was... – dannyxyz22 Jan 19 '14 at 04:19
  • That's right, in most of cases you have to deliver opencv_ffmpegXXX.dll. Take care of correct 32b/64b version and put it in some location from java library path (next to opencv_javaXXX.dll will be safe choice). – lukk Jan 19 '14 at 17:11
1

I run into the same problem and this worked for me:

  1. load the ffmpeg library

System.loadLibrary("opencv_ffmpeg300_64");

  1. Open the file:

    VideoCapture vC = new VideoCapture("res/video.mp4");

  2. Copy opencv_ffmpeg300_64.dll from opencv\build\x64\vc11\bin to opencv\build\java\x64

Please note that 64 and .dll may differ from an OS to another, those are for Windows x64

Oss
  • 4,232
  • 2
  • 20
  • 35
m0etaz
  • 953
  • 1
  • 8
  • 21