domingo, 3 de agosto de 2014

First Project with OpenCV: inRange Code for detect objects.

Well the following code let us, to remove all of elements of the pictures that are not of our interest using OpenCV and Java. For realize it, its necesary to use the inRange method (more information here). The first step is to convert the image in HSV format and them apply the inRange code.

 import org.opencv.core.Core;  
 import org.opencv.core.Mat;  
 import org.opencv.core.Scalar;  
 import org.opencv.highgui.Highgui;  
 import org.opencv.highgui.VideoCapture;  
 import org.opencv.imgproc.Imgproc;  
 import values.MyIntegers;  
 /**  
  * @author aldajo  
  */  
 public class InRange {  
   public static void main(String[] args) {  
     System.loadLibrary(Core.NATIVE_LIBRARY_NAME);  
     VideoCapture camera = new VideoCapture(1);  
     Mat framevideo = new Mat();  
     camera.open(1);  
     camera.read(framevideo);  
     Highgui.imwrite("image.png", framevideo);  
     Highgui.imwrite("imageInRange.png", preproc(framevideo));  
   }  
   public static Mat preproc(Mat frame) {  
     Mat preprosMat = new Mat();  
     Imgproc.cvtColor(frame, preprosMat, Imgproc.COLOR_RGB2HSV);  
     Core.inRange(frame, new Scalar(242, 228,103),  
         new Scalar(255, 255, 255), preprosMat);  
     return preprosMat;  
   }  
 }  

I used the above code and I got the following pictures:


Of course, you need change the values of the range for achieve it...

I have configured NetBeans for use the OpenCV library...

First Project with OpenCV: single code for web-cam capture.

Well, today I want to share you a simple code that let us to show a single frame from web-cam. The simple code uses OpenCV, and Java code... I have configured NetBeans for uses the OpenCV library.

 import org.opencv.core.Core;  
 import org.opencv.core.Mat;  
 import org.opencv.highgui.Highgui;  
 import org.opencv.highgui.VideoCapture;  
 /**  
  * @author aldajo  
  */  
 public class SingleCameraCapture {  
   public static void main(String[] args) {  
     System.loadLibrary(Core.NATIVE_LIBRARY_NAME);  
     VideoCapture camera = new VideoCapture(1);  
     Mat framevideo = new Mat();  
     camera.open(1);  
     camera.read(framevideo);  
     Highgui.imwrite("image.png", framevideo);  
   }  
 }  

The value in VideoCapture construct and camera.open method correspond to the number of the camera device in your computer. If you have only a web-cam, the value is 0.

I used the above code , and I have the following capture (my camera is a logitech c920):