1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; int main() { // Load the pre-trained Haar Cascade classifier for face detection CascadeClassifier faceCascade; if (!faceCascade.load("haarcascade_frontalface_default.xml")) { cerr << "Error loading Haar Cascade classifier!" << endl; return -1; } // Open an image file Mat image = imread("input_image.jpg"); if (image.empty()) { cerr << "Error loading image!" << endl; return -1; } // Convert the image to grayscale Mat grayImage; cvtColor(image, grayImage, COLOR_BGR2GRAY); // Detect faces in the grayscale image vector<Rect> faces; faceCascade.detectMultiScale(grayImage, faces); // Draw rectangles around detected faces for (const auto& face : faces) { rectangle(image, face, Scalar(255, 0, 0), 2); // Blue rectangles } // Display the result imshow("Face Detection", image); waitKey(0); // Wait for a key press before closing the window return 0; } |
Explanation:
- OpenCV Library:
- This program uses the OpenCV library, which provides various computer vision tools. Make sure you have OpenCV installed and properly linked to your project.
- Loading Haar Cascade Classifier (
faceCascade
):- The
CascadeClassifier
class is used to load the pre-trained Haar Cascade classifier for face detection. The classifier XML file (haarcascade_frontalface_default.xml
) contains the model for detecting faces. - Ensure you have the Haar cascade XML file available in your working directory or provide the correct path.
- The
- Loading Image (
imread
):- The
imread
function loads the image from a file. Replace"input_image.jpg"
with the path to your image file. - Checks if the image is loaded correctly and prints an error message if not.
- The
- Grayscale Conversion (
cvtColor
):- Converts the image to grayscale because face detection works more effectively on grayscale images.
- Face Detection (
detectMultiScale
):- The
detectMultiScale
function detects faces in the grayscale image. It returns a vector of rectangles (Rect
) where faces are detected.
- The
- Drawing Rectangles (
rectangle
):- Draws rectangles around detected faces on the original image using the
rectangle
function. The rectangles are colored blue (Scalar(255, 0, 0)
), and the thickness is set to 2 pixels.
- Draws rectangles around detected faces on the original image using the
- Displaying the Result (
imshow
,waitKey
):- The
imshow
function displays the result with detected faces highlighted. - The
waitKey
function waits for a key press before closing the display window.
- The
Possible Enhancements:
- Real-Time Detection: Integrate with a webcam to perform real-time face detection.
- Multiple Cascade Classifiers: Use additional classifiers for detecting other features (e.g., eyes, smiles).
- Face Recognition: Extend the program to include face recognition capabilities.
- GUI Integration: Develop a graphical user interface (GUI) for easier interaction and visualization.