Showing posts with label OpenCV-JavaCV. Show all posts
Showing posts with label OpenCV-JavaCV. Show all posts

JavaCV Configuration in Windows

I had published a article on how to configure JavaCV on windows machine about 5 year back. Since then a lot of changes has been made to JavaCV:
  • The repository host Google Code stopped their services
  • JavaCV team moved to github with a different package name. They have replaced the "com.googlecode.javacv.cpp." package with "org.bytedeco.opencv." or "org.bytedeco.javacv"
  • They ( probably OpenCV too) moved some classes here and there.  eg:  the static method cvSaveImage is now under org.bytedeco.opencv.helper.opencv_imgcodecs.cvSaveImage package. It was on com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage  before.
  • Finally the good thing is the installation/setup steps has been easier than before.  This is because they have wrapped all libraries files (dll, so ) into the platform specific jar files and we don't need to install and configure the OpenCV binaries separately

Setup Steps:

1) Install the JDK on your system. 

You can choose between 3 options:
  • OpenJDK http://openjdk.java.net/install/ or
  • Sun JDK http://www.oracle.com/technetwork/java/javase/downloads/ or
  • IBM JDK http://www.ibm.com/developerworks/java/jdk/

2) Download the JavaCV binaries.

2.a) Manually:
2.b) Automatically - Using Maven (Recommended)
<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv-platform</artifactId>
    <version>1.5.3</version>
</dependency>

3) Project Setup:

3.a) Basic project using Eclipse/Netbeans,Intellij or other IDE
Extract the JavaCV binaries and add all the jars into your classpath.

3.b) Maven Project Setup:
If you want to use Maven you need to add the dependencies as in 2.b) in your pom.xml file. There is already a sample project available on GitHub. Download it and import into  your IDE. It has a sample code to capture images from webcam.

GitHub Sample Project URL: https://github.com/gtiwari333/JavaCV-Test-Capture-WebCam


Sample Code to Capture Images from WebCam:



Happy Coding ...

colored object tracking in java- javacv code

Code for this demo video - Color Based Image Segmentation to Track Path of Moving Object


Working Source Code :
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_core.cvCreateImage;
import static com.googlecode.javacv.cpp.opencv_core.cvFlip;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
import static com.googlecode.javacv.cpp.opencv_core.cvInRangeS;
import static com.googlecode.javacv.cpp.opencv_core.cvScalar;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_MEDIAN;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvEqualizeHist;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvGetCentralMoment;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvGetSpatialMoment;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvMoments;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth;
import static com.googlecode.javacv.cpp.opencv_highgui.*;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.VideoInputFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_imgproc.CvMoments;

public class ColoredObjectTrack implements Runnable {
    final int INTERVAL = 1000;// 1sec
    final int CAMERA_NUM = 0; // Default camera for this time

simple ball game by colored object motion tracking - image processing opencv javacv

DEMO VIDEO:  simple ball game by colored object motion tracking - using javacv opencv to detect the path of moving object. This is earliest version of the game.


Full Code :
Java Collision Detection and bounce

Colored object tracking in java- javacv code


You need to integrate the ideas from above links.
The full (integrated code will be uploaded shortly)

Object tracking in Java - detect position of colored spot in image

Red spot in image - position to be detected later
Object Tracking plays important role in Image Processing research projects. In this example, I am showing how we can detect the position [(x, y) coordinates ] of a colored spot in given image using JavaCV (Java wrapper for OpenCV ).



Input image :
This image has a red colored spot. And our objective is to track the position coordinate of the spot in image.The example below uses thresholding in HSV space and simple moment calculations given in OpenCV library.





You can use this code to track an object in a video sequence - say live web-cam capture video.


Detecting Position of a spot in Threshold image:
    static Dimension getCoordinates(IplImage thresholdImage) {
        int posX = 0;
        int posY = 0;

JavaCV: Image Thresholding HSV color space

JavaCV (OpenCv) example of image thresholding based on color in HSV-A Space - to detect red color spot on given image. Useful in object tracking.

Java Source Code:

//imports
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class ColorDetect {

JavaCV - Color based thresholding in image using OpenCV

JavaCV - Red color based thresholding (RGB-A space) in image using OpenCV : Full working java source code 


Note that the order of colors is BGR-A not RGB-A. 
Read it more from http://stackoverflow.com/questions/367449/bgr-color-space
//static imports
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
//non-static imports
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

JavaCV capture-save-flip-show live camera

NOTE: Updated code with configuration and example is available here:


--

JavaCV:  Capture/save/flip image and show live image on CanvasFrame from camera

JAVA CODE:
import static com.googlecode.javacv.cpp.opencv_core.cvFlip;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage; import com.googlecode.javacv.CanvasFrame; import com.googlecode.javacv.FrameGrabber; import com.googlecode.javacv.VideoInputFrameGrabber; import com.googlecode.javacv.cpp.opencv_core.IplImage; public class GrabberShow implements Runnable { //final int INTERVAL=1000;///you may use interval IplImage image; CanvasFrame canvas = new CanvasFrame("Web Cam"); public GrabberShow() { canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); } @Override public void run() { FrameGrabber grabber = new VideoInputFrameGrabber(0); // 1 for next camera int i=0; try { grabber.start(); IplImage img; while (true) { img = grabber.grab(); if (img != null) { cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise cvSaveImage((i++)+"-aa.jpg", img); // show image on window canvas.showImage(img); } //Thread.sleep(INTERVAL); } } catch (Exception e) { } }

public static void main(String[] args) { GrabberShow gs = new GrabberShow(); Thread th = new Thread(gs); th.start(); } }

Java Code : Capture Image from webcam using JavaCV

Java Code for capturing image from webcam- uses JavaCV (java wrapper for OpenCV) library
Working CODE:
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage; import static com.googlecode.javacv.cpp.opencv_highgui.*; public class CaptureImage { private static void captureFrame() { // 0-default camera, 1 - next...so on final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0); try { grabber.start(); IplImage img = grabber.grab(); if (img != null) { cvSaveImage("capture.jpg", img); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { captureFrame(); } }

JavaCV- Image load, smooth and save

Static Imports:
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;

Image Smoothing:
public static void smoothSave(String filename) throws Exception {
IplImage image = cvLoadImage(filename);
System.out.println(image.nSize());
if (image != null) {
cvSmooth(image, image, CV_BLUR, 3);
cvSaveImage("smoothed_" + filename, image);
cvReleaseImage(image);
}
}

OpenCV-JavaCV : eclipse project configuration windows 7

NOTE: A Easier and Simpler version of the installation step is available !!  Check the latest ( Jan , 2017) article.

---

Eclipse (windows 7) project setup for JNA wrapper of OpenCV : JavaCV - getting started.

OpenCV (Open Source Computer Vision Library) is library of programming functions for real time computer vision.  JavaCV provides wrappers to commonly used libraries for OpenCV and few others.

Download the Essentials :