Java Code Examples for org.opencv.highgui.VideoCapture

The following are top voted examples for showing how to use org.opencv.highgui.VideoCapture. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: raspoid   File: TestOpenCVInstallation.java   Source Code and License 7 votes vote down vote up
/**
 * Command-line interface.
 * 
 * <p>This example tests the OpenCV installation
 * and takes a picture with this OpenCV tool.</p>
 * 
 * @param args unused here.
 */
public static void main(String[] args) {
    // Load the native library.
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Tools.log(Core.NATIVE_LIBRARY_NAME);
    VideoCapture camera = new VideoCapture(0);
    Tools.sleepMilliseconds(1000);
    
    if(!camera.isOpened())
        Tools.log("Video capturing hasn't been correctly initialized.");
    else
        Tools.log("The camera has been correctly initialized.");
    
    Mat frame = new Mat();
    camera.read(frame);
    Highgui.imwrite("capture.jpg", frame);
}
 
Example 2
Project: opencv-fun   File: FaceDetectionTest.java   Source Code and License 7 votes vote down vote up
public static void loop (CascadeClassifier classifier, Mat mat, ImgWindow window, VideoCapture video) {
	video.read(mat);
	System.out.println(mat);
	if (!mat.empty()) {
		MatOfRect rects = new MatOfRect();
		long start = System.nanoTime();
		classifier.detectMultiScale(mat, rects);
		System.out.println((System.nanoTime()-start)/1000000000.0);
		window.setImage(mat);
		Graphics2D g = window.begin();
		g.setColor(Color.RED);
		for(Rect r: rects.toArray()) {
			g.drawRect(r.x, r.y, r.width, r.height);
		}
		window.end();
	}
}
 
Example 3
Project: ShootOFF   File: SarxosCaptureCamera.java   Source Code and License 6 votes vote down vote up
public SarxosCaptureCamera(final String cameraName) {
	final List<Webcam> webcams = Webcam.getWebcams();
	int cameraIndex = -1;

	for (int i = 0; i < webcams.size(); i++) {
		if (webcams.get(i).getName().equals(cameraName)) {
			cameraIndex = i;
			break;
		}
	}

	if (cameraIndex < 0) throw new IllegalArgumentException("Camera not found: " + cameraName);

	camera = new VideoCapture();
	this.cameraIndex = cameraIndex;

}
 
Example 4
Project: HadoopCV   File: VideoRecordReader.java   Source Code and License 6 votes vote down vote up
@Override
public void initialize(InputSplit split, TaskAttemptContext context)
		throws IOException, InterruptedException {
	FileSplit sp = (FileSplit)split;
    Path file = sp.getPath(); 
    Configuration job = context.getConfiguration();
    String fpath = System.getProperty("java.io.tmpdir")+new Date().getTime()+"-"+sp.getStart()+"-"+sp.getLength()+".avi";
    FSDataInputStream fs = null;
    FileOutputStream osw = null;
    LOG.info("<><><>"+sp.toString()+"-"+sp.getStart()+"-"+sp.getLength());
    try {
	    FileSystem fileSystem = file.getFileSystem(job);
	    fs = fileSystem.open(file);
	    fs.seek(sp.getStart());
    	osw = new FileOutputStream(fpath);  
    	IOUtils.copyBytes(fs,osw,sp.getLength(),false);
    	osw.flush();
	} catch (IOException e) {
		e.printStackTrace();
	}finally {
		IOUtils.closeStream(fs);
		IOUtils.closeStream(osw);
	}
    camera = new VideoCapture(fpath);
	
}
 
Example 5
Project: hadoop-video-ocr   File: VideoProcessing.java   Source Code and License 6 votes vote down vote up
private static File[] getFrames(VideoCapture video, int frameFrequency) {
    int frameNumber = 1;
    Mat frame = new Mat();
    File videoFramesDir = Files.createTempDir();
    while(video.read(frame)){
        if(frameNumber % frameFrequency == 0) {
            String res = videoFramesDir.getAbsolutePath() + "/" +
                    String.format("%08d", frameNumber / frameFrequency) + ".jpg";
            Highgui.imwrite(res, frame);
        }
        frameNumber ++;
    }
    File [] frames = videoFramesDir.listFiles();
    Arrays.sort(frames);
    return frames;
}
 
Example 6
Project: oculusPrime   File: StereoTesting.java   Source Code and License 6 votes vote down vote up
private Mat[] captureImages() {
    VideoCapture capture1 = new VideoCapture(1);
    VideoCapture capture0 = new VideoCapture(0);

    capture1.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, Stereo.xres);
    capture1.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, Stereo.yres);
    capture0.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, Stereo.xres);
    capture0.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, Stereo.yres);

    Util.delay(1000);

    Mat left = new Mat();
    capture1.grab(); // discard 1st frame
    capture1.read(left);

    Mat right = new Mat();
    capture0.grab(); // discard 1st frame
    capture0.read(right);

    return new Mat[]{left, right};
}
 
Example 7
Project: Face-detection-and-recognition-desktop-application   File: Camera.java   Source Code and License 5 votes vote down vote up
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        webSource = new VideoCapture(0); // video capture from default cam
        myThread = new DaemonThread(); //create object of threat class
        Thread t = new Thread(myThread);
        t.setDaemon(true);
        myThread.runnable = true;
        t.start();                 //start thrad
        jButton1.setEnabled(false);  // deactivate start button
        jButton2.setEnabled(true);  //  activate stop button


    }
 
Example 8
Project: ProyectoPacientes   File: JDIngresarPaciente.java   Source Code and License 5 votes vote down vote up
private void btnCamaraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCamaraActionPerformed
    // TODO add your handling code here:
   webSource =new VideoCapture(0);
        myThread = new DaemonThread();
        Thread t = new Thread(myThread);
        t.setDaemon(true);
        myThread.runnable = true;
        t.start();
 btnCamara.setEnabled(false);  //start button
        btnPausar.setEnabled(true); 
}
 
Example 9
Project: MightyMouse   File: FaceDetection.java   Source Code and License 5 votes vote down vote up
private void startButtonActionPerformed(ActionEvent evt) {

        webSource = new VideoCapture(0); 
        myThread = new DaemonThread(); 
        Thread t = new Thread(myThread);
        t.setDaemon(true);
        myThread.runnable = true;
        t.start();               
        startButton.setEnabled(false);  
        pauseButton.setEnabled(true); 


    }
 
Example 10
Project: cardboardAR-lib   File: ViewBase.java   Source Code and License 5 votes vote down vote up
public boolean openCamera() {
    Log.i(TAG, "openCamera");
    synchronized (this) {
     releaseCamera();
     mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
     if (!mCamera.isOpened()) {
         mCamera.release();
         mCamera = null;
         Log.e(TAG, "Failed to open native camera");
         return false;
     }
 }
    return true;
}
 
Example 11
Project: Aruco-Marker-Tracking-Android   File: ViewBase.java   Source Code and License 5 votes vote down vote up
public boolean openCamera() {
    Log.i(TAG, "openCamera");
    synchronized (this) {
     releaseCamera();
     mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
     if (!mCamera.isOpened()) {
         mCamera.release();
         mCamera = null;
         Log.e(TAG, "Failed to open native camera");
         return false;
     }
 }
    return true;
}
 
Example 12
Project: ASL-recognition   File: FaceDetection.java   Source Code and License 5 votes vote down vote up
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        webSource = new VideoCapture(0); // video capture from default cam
        myThread = new DaemonThread(); //create object of thread class
        Thread t = new Thread(myThread);
        t.setDaemon(true);
        myThread.runnable = true;
        t.start();                 //start thread
        jButton1.setEnabled(false);  // deactivate start button
        jButton2.setEnabled(true);  //  activate stop button


    }
 
Example 13
Project: DeadReckoning   File: PlayWorker.java   Source Code and License 5 votes vote down vote up
public PlayWorker(VideoCapture _camera, MainFrame _mainFrame) {
	// TODO Auto-generated constructor stub
	camera = _camera;
	panel = _mainFrame.videoPanel;

	estimatedTrack = _mainFrame.estimatedTrack;
	groundTruth = _mainFrame.groundTruth;
	mapView = _mainFrame.mapView;
	groundTruthMarker = _mainFrame.groundTruthMarker;
	estimatedTrackMarker = _mainFrame.estimatedTrackMarker;
}
 
Example 14
Project: yawls   File: Brightness.java   Source Code and License 5 votes vote down vote up
private Mat capture10thFrame(VideoCapture cap, Mat frame) {
// Grab and decode the 10th frame of the camera
for (int i = 0; i <= 10; i++) {
    cap.grab();
}
cap.retrieve(frame);

return frame;
   }
 
Example 15
Project: yawls   File: Brightness.java   Source Code and License 5 votes vote down vote up
private int captureAndCalculate() {
final Mat frame = new Mat();

// Initialize video capturing and set a small image size
final VideoCapture cap = new VideoCapture(0);
cap.set(3, 160);
cap.set(4, 120);

int meanValue = current;
face = true;

// Return current as meanValue if camera start fail
if (!cap.isOpened()) {
    return meanValue;
}

capture10thFrame(cap, frame);

// Calculate mean value of frame
meanValue = meanCalculation(frame);

// If the current value change in a extreme way
// set it again
if (control(meanValue)) {
    capture10thFrame(cap, frame);

    // Re-Calculate mean value of frame
    meanValue = meanCalculation(frame);
}

// Set true if someone's Face is detected
face = probability.detectFace(frame, meanValue);

// Release the camera for other programs
cap.release();

return meanValue;
   }
 
Example 16
Project: HadoopCV   File: VideoReaderTest.java   Source Code and License 5 votes vote down vote up
public static void main(String[] args) {
	
	System.out.println(System.getProperty("java.class.path"));
 System.out.println(System.getProperty("java.library.path"));
 
 
	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	VideoCapture camera = new VideoCapture("data/bike.avi");
	MatOfByte frame = new MatOfByte();
	int i = 0;
	
 
	while(true){
        if (camera.read(frame)){
            System.out.println("Frame Obtained");
            System.out.println("Captured Frame Width " +
            frame.width() + " Height " + frame.height());
            System.out.println(frame.dump());
            Highgui.imwrite("tmp\\image\\camera"+(i++)+".jpg", frame);
            //Highgui.imencode(ext, img, buf)
        }else{
        	break;
        }
    }
	camera.release();
	
}
 
Example 17
Project: java_handRecognition   File: HsvTest.java   Source Code and License 5 votes vote down vote up
public void setup() {
	size(1280, 720);
	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	frameRate(60);
	it = new ImageTranslater(this);
	cap = new VideoCapture();
	cap.open(0);
}
 
Example 18
Project: hadoop-video-ocr   File: VideoProcessing.java   Source Code and License 5 votes vote down vote up
public static File[] parseVideo(File inputFile, int frameFrequency)
        throws FileNotFoundException {
    String videoFilePath = inputFile.getAbsolutePath();
    VideoCapture video = new VideoCapture();

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    if(frameFrequency < 1) {
        throw new IllegalArgumentException("frameFreqNumber couldn't be less then 1");
    }
    video.open(videoFilePath);
    File[] frames = getFrames(video, frameFrequency);
    video.release();
    return frames;

}
 
Example 19
Project: FingerGun   File: Main.java   Source Code and License 5 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    
    Mat raw = new Mat();
    VideoCapture c = new VideoCapture(0);
    
    Scalar hsv_min = new Scalar(0,200,64,0);
    Scalar hsv_max = new Scalar(0,255,192,0);
    Scalar hsv_min2 = new Scalar(179,200,64,0);   
    Scalar hsv_max2 = new Scalar(179,255,192,0);
    
    while (!c.isOpened())
        Thread.sleep(5);
    
    c.read(raw);
    
    Mat hsv = new Mat();
    Imgproc.cvtColor(raw,hsv,Imgproc.COLOR_BGR2HSV);
    
    Mat thresh1 = new Mat();
    Mat thresh2 = new Mat();
    Mat thresh = new Mat();
    
    Core.inRange(hsv, hsv_min, hsv_max, thresh1);
    Core.inRange(hsv, hsv_min, hsv_max, thresh1);
    Core.inRange(hsv, hsv_min2, hsv_max2, thresh2);
    Core.bitwise_or(thresh1, thresh2, thresh);
    
    Highgui.imwrite("1.png", raw);
    Highgui.imwrite("2.png", hsv);
    Highgui.imwrite("3.png", thresh1);
    Highgui.imwrite("4.png", thresh2);
    Highgui.imwrite("5.png", thresh);
    
    c.release();
}
 
Example 20
Project: opencv-fun   File: CaptureVideo.java   Source Code and License 5 votes vote down vote up
public static void main (String[] args) {
	CVLoader.load();
	
	VideoCapture video = new VideoCapture(0);

	ImgWindow window = ImgWindow.newWindow();
	if (video.isOpened()) {
		Mat mat = new Mat();
		while (!window.closed) {
			loop(mat, window, video);
		}
	}
	video.release();
}
 
Example 21
Project: opencv-fun   File: MotionDetectionTest.java   Source Code and License 5 votes vote down vote up
public static void main (String[] args) {
	CVLoader.load();
	
	VideoCapture video = new VideoCapture(0);

	ImgWindow window = ImgWindow.newWindow();
	if (video.isOpened()) {
		Mat mat = new Mat();
		while (!window.closed) {
			loop(mat, window, video);
		}
	}
	video.release();
}
 
Example 22
Project: opencv-fun   File: MotionDetectionTest.java   Source Code and License 5 votes vote down vote up
public static void loop (Mat mat, ImgWindow window, VideoCapture video) {
	video.read(mat);
	if (!mat.empty()) {
		boolean result = detector.detect(mat);
		if(result) System.out.println("motion detected, " + System.nanoTime());
		window.setImage(detector.getMask());
	}
}
 
Example 23
Project: opencv-fun   File: Canny.java   Source Code and License 5 votes vote down vote up
public static void main (String[] args) {
	CVLoader.load();

	VideoCapture video = new VideoCapture(0);
	ImgWindow wnd = ImgWindow.newWindow();

	Mat img = new Mat();
	while (video.isOpened()) {
		video.read(img);
		loop(img, wnd);
	}
}
 
Example 24
Project: opencv-fun   File: FaceDetectionTest.java   Source Code and License 5 votes vote down vote up
public static void main (String[] args) {
	CVLoader.load();
	VideoCapture video = new VideoCapture(0);

	CascadeClassifier classifier = new CascadeClassifier("data/haarcascade_frontalface_alt.xml");
	ImgWindow window = ImgWindow.newWindow();
	if (video.isOpened()) {
	 	Mat mat = new Mat();
		while (!window.closed) {
			loop(classifier, mat, window, video);
		}
	}
	video.release();
}
 
Example 25
Project: car-counter   File: OpencvCarCounter.java   Source Code and License 5 votes vote down vote up
@Override
public Collection<DetectedVehicle> processVideo(Path video, DateTime startDateTime)
{
    CascadeClassifier carDetector = new CascadeClassifier("/Users/luke/working/car-counter/data/cars3.xml");


    VideoCapture videoCapture = new VideoCapture();
    videoCapture.open("/Users/luke/working/car-counter/data/video1.m4v");

    int index = 0;

    while (true)
    {
        if (!videoCapture.read(image))
        {
            break;
        }

        System.out.print(".");

        //processFrame();

        MatOfRect carDetections = new MatOfRect();
        carDetector.detectMultiScale(image, carDetections);

        System.out.println(String.format("Detected %s cars", carDetections.toArray().length));

        // Draw a bounding box around each hit
        for (Rect rect : carDetections.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));
        }

        String file = String.format("/Users/luke/working/car-counter/data/out/out-%03d.jpg", index++);
        org.opencv.highgui.Highgui.imwrite(file, image);
    }

    return null;
}
 
Example 26
Project: LuNaCV   File: AcquireSampleImages.java   Source Code and License 5 votes vote down vote up
public void run() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    camera = new VideoCapture("http://10.3.16.11/mjpg/video.mjpg");
    frame = new Mat();
    
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setLayout(new BoxLayout(window.getContentPane(), BoxLayout.Y_AXIS));
    
    CVMatPanel imagePanel = new CVMatPanel(320, 240);
    window.getContentPane().add(imagePanel);
    
    JButton saveButton = new JButton("Save");
    saveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            saveImage();
        }
    });
    window.getContentPane().add(saveButton);
    
    window.pack();
    window.setVisible(true);
    
    while (true) {
        if (camera.isOpened()) {
            if (camera.read(frame)) {
                imagePanel.showMat(frame);
            } else {
                System.err.println("Error: unable to read image from camera");
            }
        } else {
            System.err.println("Error: camera not open");
        }
    }
}
 
Example 27
Project: LuNaCV   File: LuNaCV.java   Source Code and License 5 votes vote down vote up
public void run() {
    // Load OpenCV native library
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    // Initialize NetworkTables
    if (!debug) {
        NetworkTable.setClientMode();
        NetworkTable.setIPAddress("10.3.16.2");
        table = NetworkTable.getTable("visionData");
    }

    // Setup the GUI
    setupGUI();

    if (debug) {
        processSampleImages();
        //while (true) {
        //    processSampleImage("sample_images/image1.jpg");
        //}
    } else {
        // Open the camera feed
        camera = new VideoCapture(kCameraAddress);

        // Process images from the camera
        processCameraFeed();
    }
}
 
Example 28
Project: maslab-2014-team17   File: RobotEye.java   Source Code and License 5 votes vote down vote up
/**
 * Given the camera number, sets up the camera for this
 * instance of RobotEye.  Note that the USB camera
 * is usually camera 2, but may be greater than 2.
 * 
 * @param n	The camera number.
 */
RobotEye(int n) {
	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	// Setup the camera
	camera = new VideoCapture();
	camera.open(n);

	if(DISPLAY) {
		setUpDisplay();
	}
}
 
Example 29
Project: oculusPrime   File: OpenCVUtils.java   Source Code and License 5 votes vote down vote up
public Mat getWebCamImg(VideoCapture capture) {
//    	VideoCapture capture =new VideoCapture(camnum); 
		capture.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 320);
		capture.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 240);
		Mat webcam_image = null;
		if (capture.isOpened()) {
			webcam_image = new Mat();
			capture.grab(); // discard 1st
			Util.delay(1000);
			capture.read(webcam_image);
		}
//    	capture.release();
		return webcam_image;
	}
 
Example 30
Project: TinyPlanetMaker   File: NativeCameraView.java   Source Code and License 4 votes vote down vote up
public NativeCameraFrame(VideoCapture capture) {
    mCapture = capture;
    mGray = new Mat();
    mRgba = new Mat();
}
 
Example 31
Project: android-things-drawbot   File: NativeCameraView.java   Source Code and License 4 votes vote down vote up
public NativeCameraFrame(VideoCapture capture) {
    mCapture = capture;
    mGray = new Mat();
    mRgba = new Mat();
}
 
Example 32
Project: tttclass   File: First.java   Source Code and License 4 votes vote down vote up
public static void main(String[] args) throws Exception {  
	VideoCapture v = new VideoCapture(1);
v.grab();
  }
 
Example 33
Project: Android-Crop-Receipt   File: NativeCameraView.java   Source Code and License 4 votes vote down vote up
public NativeCameraFrame(VideoCapture capture) {
    mCapture = capture;
    mGray = new Mat();
    mRgba = new Mat();
}
 
Example 34
Project: AndroidCameraSudokuSolver   File: NativeCameraView.java   Source Code and License 4 votes vote down vote up
public NativeCameraFrame(VideoCapture capture) {
    mCapture = capture;
    mGray = new Mat();
    mRgba = new Mat();
}
 
Example 35
Project: OpenCV_Android_Plus   File: NativeCameraView.java   Source Code and License 4 votes vote down vote up
public NativeCameraFrame(VideoCapture capture) {
    mCapture = capture;
    mGray = new Mat();
    mRgba = new Mat();
}
 
Example 36
Project: MemeVision   File: NativeCameraView.java   Source Code and License 4 votes vote down vote up
public NativeCameraFrame(VideoCapture capture) {
    mCapture = capture;
    mGray = new Mat();
    mRgba = new Mat();
}
 
Example 37
Project: RectangleDetection   File: NativeCameraView.java   Source Code and License 4 votes vote down vote up
public NativeCameraFrame(VideoCapture capture) {
    mCapture = capture;
    mGray = new Mat();
    mRgba = new Mat();
}
 
Example 38
Project: cardboardAR-lib   File: NativeCameraView.java   Source Code and License 4 votes vote down vote up
public NativeCameraFrame(VideoCapture capture) {
    mCapture = capture;
    mGray = new Mat();
    mRgba = new Mat();
}
 
Example 39
Project: Aruco-Marker-Tracking-Android   File: NativeCameraView.java   Source Code and License 4 votes vote down vote up
public NativeCameraFrame(VideoCapture capture) {
    mCapture = capture;
    mGray = new Mat();
    mRgba = new Mat();
}
 
Example 40
Project: ASL-recognition   File: WebcamPanel.java   Source Code and License 4 votes vote down vote up
public WebcamPanel(){
    cap = new VideoCapture();
    cap.open(0);
    timer.start();
}