Showing posts with label Camera Capture. Show all posts
Showing posts with label Camera Capture. Show all posts

Sunday, 10 January 2016

Capturing Image using Webcam in JavaCV

Camera Capture using JavaCV


The initial set will be same as it is done in this example

http://errorsexception.blogspot.in/2016/01/basic-imagetransformation-using-javacv.html

We can capture image from webcam using this simple example using java cv


The Code is : 

import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.*;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.opencv_core.CvPoint;
import org.bytedeco.javacpp.opencv_core.CvScalar;
import org.bytedeco.javacpp.opencv_core.CvSeq;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacpp.opencv_highgui.CvCapture;

import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
import static org.bytedeco.javacpp.opencv_calib3d.*;
import static org.bytedeco.javacpp.opencv_objdetect.*;
import static org.bytedeco.javacpp.opencv_highgui.*;
public class CameraCapture1 {

/**
* @param args
* @throws Exception 
* @throws org.bytedeco.javacv.FrameRecorder.Exception 
*/
public static void main(String[] args) throws Exception, org.bytedeco.javacv.FrameRecorder.Exception {
// TODO Auto-generated method stub

OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();
        IplImage grabbedImage = grabber.grab();
        IplImage imgGray=cvCreateImage(cvGetSize(grabbedImage),IPL_DEPTH_8U,1);
        IplImage imgHsv=cvCreateImage(cvGetSize(grabbedImage),IPL_DEPTH_8U,3);

        CanvasFrame canvasFrame = new CanvasFrame("Cam");
        CanvasFrame canvasFrame1=new CanvasFrame("GrayImage");
        CanvasFrame canvasFrame2=new CanvasFrame("HSV Image");
        canvasFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height());
        canvasFrame1.setCanvasSize(grabbedImage.width(), grabbedImage.height());
        canvasFrame2.setCanvasSize(grabbedImage.width(), grabbedImage.height());

      
        while (canvasFrame.isVisible() && (grabbedImage = grabber.grab()) != null) {
        cvCvtColor(grabbedImage,imgGray,CV_BGR2GRAY);
        cvCvtColor(grabbedImage,imgHsv,CV_BGR2HSV);
       
            canvasFrame.showImage(grabbedImage);
            canvasFrame1.showImage(imgGray);
            canvasFrame2.showImage(imgHsv);
            
           
        }
        
        grabber.stop();
        canvasFrame.dispose();
}


}

and the result is
 

sorry for the resolution it's because of bad light .


thank you 

Ashutosh