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






Thursday, 17 September 2015

How to activate scott schema in Oracle Express Edition


SCOTT is a database username in ORACLE  database software.
In Scott Schema we have some default tables like emp, dept, salgrade, bonus..
these tables are so well designed that every functionalities supported by Oracle works on these tables. so for the SQL practitioner  these tables are really helpful . But now this scott schema is not available in the current versions of oracle( oracle 9i onwards) in newer version we have hr and other schemas  for practice . but for the old practitioner  we feel easy while practicing on these default tables .
so if you want to enable the scott schema in your version of Oracle you can follow the below simple steps

step1) login to oracle as the System DBA
    sql> conn system/manager ;
then search the file SCOTT.sql in your oracle installation directory then copy the path of the SCOTT.sql and enter into the sql prompt
SQL> @%ORACLE_HOME%\RDBMS\ADMIN\SCOTT.sql;
 (in my system "@C:\oraclexe\app\oracle\product\10.2.0\server\RDBMS\ADMIN\scott.sql"
so i will do like
SQL>@C:\oraclexe\app\oracle\product\10.2.0\server\RDBMS\ADMIN\scott.sql )
SQL> connect scott/tiger
Connected.

easy final steps
 sql> conn system/manager ;
connected.
SQL>@C:\oraclexe\app\oracle\product\10.2.0\server\RDBMS\ADMIN\scott.sql )
SQL> connect scott/tiger
Connected.

so now you can use the SCOTT schema and can have the default tables of SCOTT schema.



hope this tutorial is helpful to you
thanks




Wednesday, 16 September 2015

LazyInitializationException in Hibernate

This is a pretty inconvenient situation occurs while using Hibernate ORM for developing Data Access Layer logic  in our java application .

Though Lazy loading is considered as one of the greatest advantage given by hibernate while considering the performance still we got situations like the LazyInitializationExceptions.
As lazy loading reduces hits to the database by the principle like "prepare food only when you are hungry , but don't prepare food even though you are not hungry presently . so when you prepare food even though you are not hungry then the food may get waste"

Like wise if you don't require the data urgently then don't go to the data base directly . only go to the database when you need to access the data. this way hibernate reduces unnecessary hits to the database.

Now ,We can consider a situation like

suppose an object employee is being loaded using load() method and also the lazy attribute is configured as true
  Employee employee=(Employee)session.load(Employee.class,101);
        session.close();
   int no=employee.getEmployeeNo();

here getter method getEmployee()  is used to get the employeeNo from the employee object but before that the session is closed then we have no Connection to the database  and as we know when an object is loaded using the load() method with the lazy attribute equal to true then hibernate returns the proxy class object of Employee instead of real Employee class object from database and the proxy is having only the id assigned to it.
 (proxy class> ---$$--javaassist_0 class)
so when we try to access the other properties by calling getter method on the object  and before that if the session is closed then hibernate couldn't load the object from the database and it throws
LazyInitializationException .


hope the article is helpful to you
in case any query feel free to comment below..



Monday, 7 September 2015