
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to detect faces in an image using Java OpenCV library?
The CascadeClassifier class of is used to load the classifier file and detects the desired objects in the image.
The detectMultiScale() of this class detects multiple objects of various sizes. This method accepts −
An object of the class Mat holding the input image.
An object of the class MatOfRect to store the detected faces.
To get the number of faces in the image −
Load the lbpcascade_frontalface.xml file using the CascadeClassifier class.
Invoke the detectMultiScale() method.
Convert the MatOfRect object to an array.
The length of the array is the number of faces in the image.
Example
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfRect; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import org.opencv.objdetect.CascadeClassifier; public class FaceDetection { public static void main (String[] args) { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); //Reading the Image from the file String file ="D:\Images\faces.jpg"; Mat src = Imgcodecs.imread(file); //Instantiating the CascadeClassifier String xmlFile = "lbpcascade_frontalface.xml"; CascadeClassifier classifier = new CascadeClassifier(xmlFile); //Detecting the face in the snap MatOfRect faceDetections = new MatOfRect(); classifier.detectMultiScale(src, faceDetections); System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); //Drawing boxes for (Rect rect : faceDetections.toArray()) { Imgproc.rectangle( src, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255), 3 ); } //Writing the image Imgcodecs.imwrite("D:\Images\face_Detection.jpg", src); System.out.println("Image Processed"); } }
Input
Output
No of faces detected: 3
- Related Articles
- How to detect cat faces in an image in OpenCV using Python?
- How to Detect the key points of an image using OpenCV Java library?
- How to write an image using Java OpenCV library?
- How to flip an image using Java OpenCV library?
- How to blur faces in an image using OpenCV Python?
- How to add noise to an image using Java OpenCV library?
- How to add text to an image using Java OpenCV library?
- How to add borders to an image using Java OpenCV library?
- How to draw markers on an image using Java OpenCV library?
- How to detect eyes in an image using OpenCV Python?
- How to find Image Contours using Java OpenCV library?
- How to draw Image Contours using Java OpenCV library?
- How to create a watermark on an image using Java OpenCV library?
- How to alter the contrast of an image using Java OpenCV library?
- How to alter the brightness of an image using Java OpenCV library?

Advertisements