In the introductory set on Image Processing, BufferedImage class of Java was used for processing images the applications of BufferedImage class is limited to some operations only, i.e, we can modify the R, G, B values of given input image and produce the modified image. For complex image processing such as face/object detection OpenCV library is used which we will use in this article.
At first we need to setup OpenCV for Java, we recommend to use eclipse for the same since it is easy to use and setup.
Now lets understand some of the methods required for face detection.
- CascadeClassifier() : This class is used to load the trained cascaded set of faces which we will be using to detect face for any input image.
- Imcodecs.imread()/Imcodecs.imwrite() : These methods are used to read and write images as Mat objects which are rendered by OpenCV.
- Imgproc.rectangle() : Used to generate rectangle box outlining faces detected, it takes four arguments – input_image, top_left_point, bottom_right_point, color_of_border.
Explanation of Code:
- It loads the native OpenCV library to use Java API. An instance of CascadeClassifier is created, passing it the name of the file from which the classifier is loaded.
- Next, detectMultiScale method is used on the classifier passing it the given image and MatOfRect object.
- MatOfRect is responsible to do face detections after processing.
- The process is iterated for doing all the face detections and mark the image with rectangles and at the end image is saved as output.png file.
The output of the program is shown below. This is my pic before and after face detection.
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Image Processing in Python (Scaling, Rotating, Shifting and Edge Detection)
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)
- Image Processing in Java | Set 7 (Creating a random pixel image)
- Image Processing in Java | Set 8 (Creating mirror image)
- Image Processing in Java | Set 11 (Changing orientation of image)
- Image Processing in Java | Set 10 ( Watermarking an image )
- Image Processing In Java | Set 2 (Get and set Pixels)
- Image Processing in Java | Set 1 (Read and Write)
- Image Processing in Java | Set 14 ( Comparison of two images )
- Image Processing in Java | Set 12 ( Contrast Enhancement )
- Image Processing using OpenCV in Java | Set 14 ( Sharpness Enhancement )
- Image Processing using OpenCV in Java | Set 13 (Brightness Enhancement)
- OpenCV C++ Program for Face Detection