Last week i was very excited about computer vision and the on going activity on this field of science. So i started looking at OpenCV,i was really amazed to see what it offer. Although i have just started playing with OpenCV,so i will show in this tutorial,how to get started with OpenCV in both Java and Python.
Now its just the beginner's tutorial,as i have no problem in admitting that currently i'm a novice or beginner in the computer vision field.Next time the example will be more realistic :)
Step 1: Setting up OpenCV
I have installed it on my Laptop having Ubuntu 14.04.Please follow the steps given here for installation.
After you have installed the OpenCV,set up your IDE. I have used Eclipse-IDE.
You can download the source-code from here.Hope you enjoy it!
Step 2: Coding Time with Java
HelloWorld->Lets start our first HelloWorld example with OpenCV to make sure,its working fine.
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class HelloWorld {
public static void main(String[] args) {
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
System.out.println("Hello OpenCV..!!");
Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
System.out.println("mat = " + mat.dump());
}
}
Output:
Hello OpenCV..!!
mat = [1, 0, 0;
0, 1, 0;
0, 0, 1]
OpenCV_Drawing-> Now we have done our HelloWorld working,its time to get little fancier.Lets draw a circle filled with green over a picture.
public class OpenCV_Drawing {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = Highgui.imread("/home/kuntal/Pictures/rock.jpeg");
PictureFrame.bufferedImageShow(mat, "Original");
Core.circle(mat, new Point(mat.width() * 0.5, mat.height() * 0.5), 40,
new Scalar(0, 255, 0), Core.FILLED);
Core.putText(mat, "Hello World!", new Point(30, 30), 100, 1,
new Scalar(0, 0, 0));
PictureFrame.bufferedImageShow(mat, "Drawing");
}
}
Output:
OpenCV_EdgeDetect-> We will use the same picture and detect the edge of this picture.
public class OpenCV_EdgeDetect {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = Highgui.imread("/home/kuntal/Pictures/rock.jpeg");
PictureFrame.bufferedImageShow(mat, "Original");
int kernelSize = 3;
Mat kernel = new Mat(kernelSize, kernelSize, CvType.CV_32F) {
{
put(0, 0, 0);
put(0, 1, -1);
put(0, 2, 0);
put(1, 0, -1);
put(1, 1, 4);
put(1, 2, -1);
put(2, 0, 0);
put(2, 1, -1);
put(2, 2, 0);
}
};
Imgproc.filter2D(mat, mat, -1, kernel);
PictureFrame.bufferedImageShow(mat, "Laplacian");
}
}
Output:
OpenCV_FaceDetect-> Now Face or Object detection is a very widely used technique in modern day application.You will see how easy it is to do this OpenCv,just few lines of code. Obviously you can tune your algorithm,but default work good.
public class OpenCV_FaceDetect {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier faceDetector = new CascadeClassifier(
"/home/kuntal/knowledge/software/opencv-2.4.10/data/lbpcascades/lbpcascade_frontalface.xml");
Mat mat = Highgui.imread("/home/kuntal/Pictures/rock.jpeg");
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(mat, faceDetections);
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(mat, new Point(rect.x, rect.y), new Point(rect.x
+ rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
PictureFrame.bufferedImageShow(mat, "faceDetection");
JOptionPane.showMessageDialog(null,
"Detected " + faceDetections.toArray().length + " faces");
}
}
Output:
Note: I have used Swing JFrame in PictureFrame class for showing picture.You can use any other.
Step 3: Coding time with Python
Testing OpenCv with python
python
then, import opencv:
import cv2
cv2.__version__
Output:
'2.4.10'
Reading Writing image(to gray Scale)
import cv2
grayImage = cv2.imread('/home/kuntal/Pictures/rock.jpeg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
cv2.imwrite('/home/kuntal/Pictures/rock_modified.jpeg', grayImage)
Output: (Original and Gray Scale)
Tracking Faces with Haar Cascades Classifier
For this example you need numpy and matplotlib installed in your system.
import numpy as np
import cv2
from matplotlib import pyplot as plt
face_cascade = cv2.CascadeClassifier('/home/kuntal/knowledge/software/opencv-2.4.10/data/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/home/kuntal/knowledge/software/opencv-2.4.10/data/haarcascades/haarcascade_eye.xml')
img = cv2.imread('/home/kuntal/Pictures/rock.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Hope you Enjoy It !!
Now its just the beginner's tutorial,as i have no problem in admitting that currently i'm a novice or beginner in the computer vision field.Next time the example will be more realistic :)
Step 1: Setting up OpenCV
I have installed it on my Laptop having Ubuntu 14.04.Please follow the steps given here for installation.
After you have installed the OpenCV,set up your IDE. I have used Eclipse-IDE.
- Create a Java Project
- Add External Lib and include the following jar: opencv-2410.jar
- Also add native library to opencv jar in our build-path,which is located in the path: /usr/local/share/OpenCV/java
You can download the source-code from here.Hope you enjoy it!
Step 2: Coding Time with Java
HelloWorld->Lets start our first HelloWorld example with OpenCV to make sure,its working fine.
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class HelloWorld {
public static void main(String[] args) {
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
System.out.println("Hello OpenCV..!!");
Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
System.out.println("mat = " + mat.dump());
}
}
Output:
Hello OpenCV..!!
mat = [1, 0, 0;
0, 1, 0;
0, 0, 1]
OpenCV_Drawing-> Now we have done our HelloWorld working,its time to get little fancier.Lets draw a circle filled with green over a picture.
public class OpenCV_Drawing {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = Highgui.imread("/home/kuntal/Pictures/rock.jpeg");
PictureFrame.bufferedImageShow(mat, "Original");
Core.circle(mat, new Point(mat.width() * 0.5, mat.height() * 0.5), 40,
new Scalar(0, 255, 0), Core.FILLED);
Core.putText(mat, "Hello World!", new Point(30, 30), 100, 1,
new Scalar(0, 0, 0));
PictureFrame.bufferedImageShow(mat, "Drawing");
}
}
Output:
OpenCV_EdgeDetect-> We will use the same picture and detect the edge of this picture.
public class OpenCV_EdgeDetect {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = Highgui.imread("/home/kuntal/Pictures/rock.jpeg");
PictureFrame.bufferedImageShow(mat, "Original");
int kernelSize = 3;
Mat kernel = new Mat(kernelSize, kernelSize, CvType.CV_32F) {
{
put(0, 0, 0);
put(0, 1, -1);
put(0, 2, 0);
put(1, 0, -1);
put(1, 1, 4);
put(1, 2, -1);
put(2, 0, 0);
put(2, 1, -1);
put(2, 2, 0);
}
};
Imgproc.filter2D(mat, mat, -1, kernel);
PictureFrame.bufferedImageShow(mat, "Laplacian");
}
}
Output:
OpenCV_FaceDetect-> Now Face or Object detection is a very widely used technique in modern day application.You will see how easy it is to do this OpenCv,just few lines of code. Obviously you can tune your algorithm,but default work good.
public class OpenCV_FaceDetect {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier faceDetector = new CascadeClassifier(
"/home/kuntal/knowledge/software/opencv-2.4.10/data/lbpcascades/lbpcascade_frontalface.xml");
Mat mat = Highgui.imread("/home/kuntal/Pictures/rock.jpeg");
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(mat, faceDetections);
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(mat, new Point(rect.x, rect.y), new Point(rect.x
+ rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
PictureFrame.bufferedImageShow(mat, "faceDetection");
JOptionPane.showMessageDialog(null,
"Detected " + faceDetections.toArray().length + " faces");
}
}
Output:
Note: I have used Swing JFrame in PictureFrame class for showing picture.You can use any other.
Step 3: Coding time with Python
Testing OpenCv with python
- Open terminal, then launch python interpeter:
python
then, import opencv:
import cv2
cv2.__version__
Output:
'2.4.10'
Reading Writing image(to gray Scale)
import cv2
grayImage = cv2.imread('/home/kuntal/Pictures/rock.jpeg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
cv2.imwrite('/home/kuntal/Pictures/rock_modified.jpeg', grayImage)
Output: (Original and Gray Scale)
Tracking Faces with Haar Cascades Classifier
For this example you need numpy and matplotlib installed in your system.
import numpy as np
import cv2
from matplotlib import pyplot as plt
face_cascade = cv2.CascadeClassifier('/home/kuntal/knowledge/software/opencv-2.4.10/data/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/home/kuntal/knowledge/software/opencv-2.4.10/data/haarcascades/haarcascade_eye.xml')
img = cv2.imread('/home/kuntal/Pictures/rock.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Hope you Enjoy It !!