Skip to content

Instantly share code, notes, and snippets.

@nikhil9
Created October 31, 2012 15:58
Show Gist options
  • Save nikhil9/3987878 to your computer and use it in GitHub Desktop.
Save nikhil9/3987878 to your computer and use it in GitHub Desktop.
Demo code for import and display video using Javacv
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.OpenCVFrameGrabber;
public class demo_video {
public static void main(String[] args) {
//Create canvas frame for displaying video.
CanvasFrame canvas = new CanvasFrame("VideoCanvas");
//Set Canvas frame to close on exit
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
//Declare FrameGrabber to import video from "video.mp4"
FrameGrabber grabber = new OpenCVFrameGrabber("video.mp4");
try {
//Start grabber to capture video
grabber.start();
//Declare img as IplImage
IplImage img;
while (true) {
//inser grabed video fram to IplImage img
img = grabber.grab();
//Set canvas size as per dimentions of video frame.
canvas.setCanvasSize(grabber.getImageWidth(), grabber.getImageHeight());
if (img != null) {
//Show video frame in canvas
canvas.showImage(img);
}
}
}
catch (Exception e) {
}
}
}
@padilla0
Copy link

padilla0 commented Nov 6, 2015

why IplImage img don't accept grabber.grab()

@shresthaankit7
Copy link

grabber.grab() returns a "Frame". Use OpenCVFrameConvertor to convert the obtained frame to Ipllmage.

Frame frame = grabber.grab();
OpenCvFrameConvertor convertor = new OpenCvFrameConvertor();
IplImage iplImage = convertor.convert(frame);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment