Opencv realizes multi-image reading and display, sprintf_s function, static_cast

The code is connected to the last face detection file name is file0001~4000, use%04d in scanf

#include<opencv2/objdetect/objdetect.hpp> 
#include<opencv2/highgui/highgui.hpp> 
#include<opencv2/imgproc/imgproc.hpp> 
#include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
using namespace cv;
using namespace std;
//Face detection class 
CascadeClassifier faceCascade;
int main()
{
 faceCascade.load("haarcascade_frontalface_alt2.xml"); //Load the classifier, pay attention to the file path 
  int num =4000;
 char fileName[50];
 
 for (int j = 1; j<= num; j++)
 {
  sprintf_s(fileName, "D:\\123\\files\\file%04d.jpg", j);
  Mat img = imread(fileName);
  Mat imgGray;
  vector<Rect> faces;
  if (img.empty())
  {
   return 1;
  }
  if (img.channels() == 3)
  {
   cvtColor(img, imgGray, CV_RGB2GRAY);
  }
  else
  {
   imgGray = img;
  }
  faceCascade.detectMultiScale(imgGray, faces, 1.2, 6, 0, Size(0, 0)); //Detect faces 
  if (faces.size() > 0)
  {
   for (int i = 0; i < faces.size(); i++)
   {
    rectangle(img, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height),
     Scalar(0, 255, 0), 1, 8);    //框出人脸位置 
   }
  }
  imshow("FacesOfPrettyGirl", img);
  waitKey(0);
  
 }
 waitKey(0);
 return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325807135&siteId=291194637