直接上代码
#include
#include
using namespace cv;
using namespace std;
String HarrFile = "D:\\OpenCV\\OpencvNewBuild\\install\\etc\\haarcascades\\haarcascade_frontalcatface_extended.xml";
String videoFile = "D:\\OpenCV\\videos\\cats.mp4";
CascadeClassifier faceHarr;
CascadeClassifier faceLbp;
size_t t;
int tt;
const char *INPUT = "input image";
const char *OUTPUT = "output image";
const char *CAMERA = "camera window";
int cnt = 0;
Mat img, dst, gray, frame;
VideoCapture cap;
int main(int, void*)
{
img = imread("D:\\OpenCV\\images\\6.jpg");
if (img.empty() || !faceHarr.load(HarrFile))
{
cout << "image loading failed..." << endl;
waitKey(2000);
return -1;
}
//namedWindow(INPUT, WINDOW_AUTOSIZE);
//namedWindow(OUTPUT, WINDOW_AUTOSIZE);
//namedWindow(CAMERA, WINDOW_AUTOSIZE);
//imshow(INPUT, img);
//cvtColor(img, gray, COLOR_RGB2GRAY);
imshow("gray image", gray);
//equalizeHist(gray, gray);
//vectorcat;
//faceHarr.detectMultiScale(gray, cat, 1.4, 3, 0, Size(30, 30));
//for (size_t t = 0; t < cat.size(); t++)
//{
// rectangle(img, cat[static_cast(t)], Scalar(0, 255, 0), 2, 8, 0);
//}
//imshow(OUTPUT, img);
cap.open(videoFile);
vectorcat;
while (cap.read(frame))
{
cvtColor(frame, gray, COLOR_RGB2GRAY);
equalizeHist(gray, gray);
cnt++;
if (cnt % 10 == 0)
{
cnt = 0;
faceHarr.detectMultiScale(gray, cat, 1.1, 3, 0, Size(30, 30));
for (t = 0; t < cat.size(); t++)
{
rectangle(frame, cat[t], Scalar(0, 0, 255), 2, 8, 0);
}
tt = t;
string s = to_string(tt);
putText(frame, "cat nums: " + s, Point(0, 50), FONT_HERSHEY_TRIPLEX, 1.0, Scalar(25, 128, 23), 2, 8);
imshow(CAMERA, frame);
}
char c = waitKey(40);
if (c == 27)
{
break;
}
}
//imshow("gray window", gray);
cap.release();
waitKey(0);
return 0;
}
没什么好说的,利用现有的级联分类器实现的
效果图如下