Sunday, 10 January 2016

Color Filtering Using JavaCV

In this example, we will be demonstrating how to filter a single or multiple color from webcam captured image using cvInRanges() function before that we need to convert the image to HSV format and then setting the minimum and maximum value for the range to filter a particular color.

the initial set up is same as this example
http://errorsexception.blogspot.in/2016/01/basic-imagetransformation-using-javacv.html


The code:


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 ColorFilter1 {

/**
* @param args
* @throws Exception 
*/
public static void main(String[] args) throws Exception {


IplImage  imghsv, imgbin;

imghsv = cvCreateImage(cvSize(640,480),8,3);
imgbin = cvCreateImage(cvSize(640,480),8,1);

OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();
        IplImage grabbedImage = grabber.grab();

        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());
        
        canvasFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height());


while (canvasFrame.isVisible() && (grabbedImage = grabber.grab()) != null) {
//if(img1 == null) break;

cvCvtColor(grabbedImage,imghsv,CV_BGR2HSV);
CvScalar minc = cvScalar(95,150,75,0), maxc = cvScalar(145,255,255,0);//for filtering Blue color
cvInRangeS(imghsv,minc,maxc,imgbin);

canvasFrame.showImage(grabbedImage);
canvasFrame1.showImage(imgbin);
canvasFrame2.showImage(imghsv);
char c = (char)cvWaitKey(15);
if(c == 'q') break; 

}

cvReleaseImage(imghsv);
cvReleaseImage(imgbin);
}

}


Happy Coding 
for any issue please let me know

Thank you,
Ashutosh

Video Recording from webcam using javacv

we can also record and save the video in any format from webcam using FFmpegFrameRecorder of javacv .

the initial set up can be done  before running the code  can be referenced from this example
http://errorsexception.blogspot.in/2016/01/basic-imagetransformation-using-javacv.html


The Code :

import org.bytedeco.javacpp.avutil;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.OpenCVFrameGrabber;

public class VideoRecordTest1 {
    public static void main(String[] args) throws Exception {
    OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();
        IplImage grabbedImage = grabber.grab();

        CanvasFrame canvasFrame = new CanvasFrame("Cam");
        canvasFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height());

        System.out.println("framerate = " + grabber.getFrameRate());
        grabber.setFrameRate(grabber.getFrameRate());
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("FILENAME",  grabber.getImageWidth(),grabber.getImageHeight());

        recorder.setVideoCodec(13);
        recorder.setFormat("mp4");
     
        recorder.setFrameRate(30);
        recorder.setVideoBitrate(10 * 1024 * 1024);

        recorder.start();
        while (canvasFrame.isVisible() && (grabbedImage = grabber.grab()) != null) {
            canvasFrame.showImage(grabbedImage);
            recorder.record(grabbedImage);
        }
        recorder.stop();
        grabber.stop();
        canvasFrame.dispose();
}
}



Happy Coding

For Any issue please let me know
thank you
Ashutosh

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


Basic ImageTransformation using JavaCV

Here we will do some basic image manipulation using javacv (a wrapper for opencv in java) and the javacpp( a java presset library to perform native operations). though JavaCV is not the official release from opencv.org .. Many Many Thanks to Samuel Audet for his enormous effort to to make it possible for us to use opencv in java.  (official site- http://bytedeco.org/)

In this example, we will convert an image into gray image and also into HSV image(Hue, saturation, value) format and display them in windows and later save the converted image. for that we will be using the function like cvCvtColor(), cvSaveImage() function  of javacv.

But before that, we must download the jars required for this example and must add it to the build path of the project folder in eclipse.

the jars required are

1) javacv_bin.jar
2)javaCpp_bin.jar
3)opencvjar for java
4) vcredist_x64.exe  (C++ redistibutable if it is a 64Bit window) .optional if already there

 To download all version of java CV please follow this link
1)http://central.maven.org/maven2/org/bytedeco/javacv/
2)To Download OpenCv please go the official website of opencv


we must take care that the javacv version must match with opencv version else version mismatch will occur and Unsatisfied Link error will be thrown
like for the opencv_2.4.9 the matched version is javacv_0.8 or javacv_0.7.



The code for this example is

Some times the Ctrl+Shift+o may not work to import the required classes so we have to manually copy the import .


import static org.bytedeco.javacpp.opencv_core.IPL_DEPTH_8U;
import static org.bytedeco.javacpp.opencv_core.cvCreateImage;
import static org.bytedeco.javacpp.opencv_core.cvGetSize;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvSaveImage;
import static org.bytedeco.javacpp.opencv_imgproc.cvCvtColor;
import static org.bytedeco.javacpp.opencv_core.*;
import org.bytedeco.javacpp.opencv_core.IplImage;

import static org.bytedeco.javacpp.opencv_highgui.*;
import static org.bytedeco.javacpp.opencv_imgproc.CV_BGR2GRAY;
import static org.bytedeco.javacpp.opencv_imgproc.CV_BGR2HSV;
public class BasicImgTransform1 {

public static void main(String[] args) {
IplImage img=cvLoadImage("C:/Users/ASD/Downloads/me.jpg");// ur  image file location
IplImage imgGray=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);// 1 represents                                                                                                                                                   single channel                                                                                                                                        & U is unsigned
IplImage imgHsv=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);

cvCvtColor(img,imgHsv,CV_BGR2HSV);
cvCvtColor(img,imgGray,CV_BGR2GRAY);

cvShowImage("Original",img);
cvShowImage("GrayImage",imgGray);
cvShowImage("HSVImage",imgHsv);
cvWaitKey();
cvSaveImage("D:\\Original.jpg",img);
cvSaveImage("D:\\GrayImage.jpg",imgGray);
cvSaveImage("D:\\HsvImage.jpg",imgHsv);

// now the below part is optional but it is always recommended to release the resources
cvReleaseImage(img);
cvReleaseImage(imgGray);
cvReleaseImage(imgHsv);
}

}


TO know more on this topic please refer the official Open CV Website.
now try this on your own and please let me know if you find any issue.

Happy Coding.

Thank you
Ashutosh