Showing posts with label javacv. Show all posts
Showing posts with label javacv. Show all posts

HSV values of Image pixel using JavaCV


This post will explain how to get HSV color space values of Image pixel using JavaCV.
For JavaCV setup on Windows, follow link below
http://opencvlover.blogspot.in/2012/04/javacv-setup-with-eclipse-on-windows-7.html

If you want to access RGB color space values, then refer to link below
http://opencvlover.blogspot.in/2013/02/rgb-values-of-image-pixel-using-javacv.html

Pixel values in HSV color space are often required in Image processing. Following gist demonstrates how to access and print pixel values in HSVcolor space. We first convert image from RGB color space to HSV color space using 'cvCvtColor()''. We can call 'cvGet2D()' function to access image matrix and use it get pixel values at desired location.

This will print HSV values in console. This code comes really handy while using manual threshold.
You can fork me on github for complete project of this code.
https://github.com/nikhil9/getHSV

RGB values of Image pixel using JavaCV

This post will explain how to get RGB color space values of Image pixel using JavaCV.
For JavaCV setup on Windows, follow link below
http://opencvlover.blogspot.in/2012/04/javacv-setup-with-eclipse-on-windows-7.html

If you want to access HSV color space values, then refer to link below
http://opencvlover.blogspot.in/2013/02/hsv-values-of-image-pixel-using-javacv.html

Pixel values in RGB color space are often required in Image processing. Following gist demonstrates how to access and print pixel values in RGB color space. We can call 'cvGet2D()' function to access image matrix and use it get pixel values at desired location.


This will print RGB values in console. This code comes really handy while using manual threshold.
You can fork me on github for complete project of this code.
https://github.com/nikhil9/getRGB

Face Detection in JavaCV using haar classifier

OpenCV provides haar like feature detection algorithm which can be used for object detection. Wikipedia page http://en.wikipedia.org/wiki/Haar-like_features provides nice information about what are haar like feature.

OpenCV also provides haar training utility which can be used for training. It generates XML file from training samples which further can be used for fast object detection. Such XML file is provided with opencv package for face detection.

Gist below explains how to use haar classifier in JavaCV. Code loads classifier file haarcascade_frontalface_default.xml and uses cvHaarDetectObjects() to find faces in loaded image. More information about cvHaarDetectObjects() can be found at http://opencv.willowgarage.com/documentation/object_detection.html#haardetectobjects

Result of this code generates window which shows loaded picture and red rectangle around detected faces.


You can fork complete eclipse project at https://github.com/nikhil9/FaceDetection/

Hough Circle detection in Javacv

Opencv provides Hough circle Detection algorithm which can be used to detect circles. Some information about how algorithm works and its example using Opencv in cpp can be found in below link

We will see how to use cvHoughCircle using Javacv. first we will have to process image to get grayscale or binary image. Using cvSmooth() helps most of the time for good detection however it depending upon kind of object  and background more image processing may be required.

First we will load image and then convert it to grayscale. Then use cvSmooth() to smooth the edges. cvHoughCircle() is used to detect circles and are stored in CvSeq. cvGetSeqElem() is used to extract each circle. We have to use each element in CvPoint3D32f. Center of circle is obtained in CvPoint type using cvPointFrom32f(). Obtained center and radius is used to draw circle on input image using cvCircle.

Following code is a demonstration of all the above processes.


Input Image:

Output Image:

Accessing webcam using javacv

Configuration steps for javacv on Eclipse running on windows 7 can be found in the link below.
http://opencvlover.blogspot.in/2012/04/javacv-setup-with-eclipse-on-windows-7.html

Instructions for importing video in javacv can be found in link below
http://opencvlover.blogspot.in/2012/05/importing-video-using-javacv.html

Accessing webcam using javacv is pretty much similar to importing video. We just have to set null parameter  while specifying video path in deceleration of OpenCVFramegrabber. Also we have to flip output from webcam horizontally to get it like mirror.

Following is the code snippet which uses OpenCVFrameGrabber to capture from webcam.


Importing video using Javacv

Configuration steps for javacv on Eclipse running on windows 7 can be found in the link below.
http://opencvlover.blogspot.in/2012/04/javacv-setup-with-eclipse-on-windows-7.html

In this post we will see how to import video in javacv for processing. Opencv uses CvCapture function for importing video. Javacv uses FrameGrabber class for grabbing video. Framegrabber can be used with 2 methods for importing video OpenCVFrameGrabber and FFMpegFramegrabber.
Following is the code snippet which uses OpenCVFrameGrabber to capture video file "Video.mp4".


If openCVFrameGrabber shows blank output or some error then try using FFMpegGrabber instead.

JavaCV setup with Eclipse on Windows 7

Update: These instructions are also applicable for 2.4+ version of opencv and javacv.

I was looking for image processing library for Java. My search stopped at JavaCV. JavaCV provides wrappers for OpenCV. so you can directly use OpenCV functions in Java. JavaCV also provides hardware accelerated full screen Image display with CanvasFrame and GLCanvasFrame.

JavaCV provides jar files which can be used with OpenCV installation. Following steps can be followed to setup JavaCV with Eclipse on Windows 7.

1.Install Java Development Kit (JDK) from the link below. Make sure to install 32bit or 64 bit as per your processor.

http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u32-downloads-1594644.html

2. Install Microsoft Visual C++ redistributable package

For 32 bit: http://www.microsoft.com/download/en/details.aspx?id=5555
For 64 bit: http://www.microsoft.com/download/en/details.aspx?id=14632

3. Download OpenCV super-pack from Sourceforge and extract it in you C:\Opencv

http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.3.1/

4. Set OpenCV .dll in systems path so that JavaCV can access it.

Go to Control Panel > System Security > System > Advanced System Settings > Environment Variables
In System variable select path and click on Edit. Now add following address in Variable address. I have extracted opencv package in C:\ directory. If you have installed at different location please update address accordingly.

For OpenCV 2.4.2 and earlier version 

For 32 bit:
C:\opencv\build\x86\vc10\bin;C:\opencv\build\common\tbb\ia32\vc10\

For 64 bit:
C:\opencv\build\x64\vc10\bin;C:\opencv\build\common\tbb\intel64\vc10\


For OpenCV 2.4.3 and later version 

For 32 bit:
C:\opencv\build\x86\vc10\bin

For 64 bit:
C:\opencv\build\x64\vc10\bin



Once this is done, the most important step is to Restart Windows.

5. Download JavaCV- bin from link below and extract it.

http://code.google.com/p/javacv/downloads/list

6. Create New Java Project 'demo' in Eclipse. 

Once created, go to Project > Properties > Java Build Path > Libraries > Add External JARs .
Go to your JavaCV Extracted folder and add all jar files. It should look like the pic below



7. Now create demo.java in src folder of the project. 

Paste following code and run the project.Put your image in project folder. In my case it is 'img1.png'


Result will look like this