opencv视频取帧并进行人脸检测(Windows和Linux双版本)

Windows版本

环境win7+vs2015+opencv2.4.10

直接给代码:

#include "highgui.h"  
#include "objdetect/objdetect.hpp"
#include "imgproc/imgproc.hpp"

using namespace cv;

String face_cascade_name = "E:\\rszj\\software\\opencv\\sources\\data\\lbpcascades\\lbpcascade_frontalface.xml";
String eyes_cascade_name = "E:\\rszj\\software\\opencv\\sources\\data\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;


void detectAndDisplay(char* window_name, IplImage* frameSrc)
{
	Mat frame = Mat(frameSrc);
	std::vector faces;
	Mat frame_gray;

	cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
	equalizeHist(frame_gray, frame_gray);

	//-- Detect faces
	face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0, Size(80, 80));

	for (size_t i = 0; i < faces.size(); i++)
	{
		Mat faceROI = frame_gray(faces[i]);
		std::vector eyes;

		//-- In each face, detect eyes
		eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
		if (eyes.size() == 2)
		{
			//-- Draw the face
			Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
			ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 0), 2, 8, 0);

			for (size_t j = 0; j < eyes.size(); j++)
			{ //-- Draw the eyes
				Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
				int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
				circle(frame, eye_center, radius, Scalar(255, 0, 255), 3, 8, 0);
			}
		}
	}
	//-- Show what you got
	imshow(window_name, frame);
}


int loadCascade()
{
	if (!face_cascade.load(face_cascade_name))
	{
		printf("--(!)Error loading face cascade\n");
		return -1;
	};
	if (!eyes_cascade.load(eyes_cascade_name))
	{
		printf("--(!)Error loading eyes cascade\n");
		return -1;
	};
	return 0;
}

int main(int argc, char* argv[])
{

	if (loadCascade() != 0)
	{
		printf("Error loading cascade!!");
		return -1;
	}

	cvNamedWindow("avi");
	CvCapture* capture = cvCreateFileCapture("E:\\rszj\\codes\\c++workspace\\opencv_model\\Debug\\test4.mp4");

	IplImage* frame;

	while (1)
	{
		frame = cvQueryFrame(capture);
		if (!frame) break;

		//cvShowImage("avi", frame);
		detectAndDisplay("avi", frame);
		char c = cvWaitKey(50);

		if (c == 27)
			break;
	}
	cvReleaseCapture(&capture);

	cvDestroyWindow("avi");
	return 0;
}

Linux:

环境:Ubuntu14.04 + opencv2.4.9

#include "stdio.h"
#include "highgui.h"
#include "objdetect/objdetect.hpp"
#include "imgproc/imgproc.hpp"

using namespace cv;
//using namespace std;

string face_cascade_name = "/home/rszj/yanjie/codes/opencv/lbpcascade_frontalface.xml";
string eyes_cascade_name = "/home/rszj/yanjie/codes/opencv/haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;


void detectAndDisplay(string window_name, IplImage* frameSrc)
{
    Mat frame = Mat(frameSrc);
	std::vector faces;
	Mat frame_gray;

	cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
	equalizeHist(frame_gray, frame_gray);

	//-- Detect faces
	face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0, Size(80, 80));

	for (size_t i = 0; i < faces.size(); i++)
	{
		Mat faceROI = frame_gray(faces[i]);
		std::vector eyes;

		//-- In each face, detect eyes
		eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
		if (eyes.size() == 2)
		{
			//-- Draw the face
			Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
			ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 0), 2, 8, 0);

			for (size_t j = 0; j < eyes.size(); j++)
			{ //-- Draw the eyes
				Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
				int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
				circle(frame, eye_center, radius, Scalar(255, 0, 255), 3, 8, 0);
			}
		}
	}
	//-- Show what you got
	imshow(window_name, frame);
}


int loadCascade()
{
	if (!face_cascade.load(face_cascade_name))
	{
		printf("--(!)Error loading face cascade\n");
		return -1;
	};
	if (!eyes_cascade.load(eyes_cascade_name))
	{
		printf("--(!)Error loading eyes cascade\n");
		return -1;
	};
	return 0;
}

int main(int argc, char* argv[])
{

	if (loadCascade() != 0)
	{
		printf("Error loading cascade!!");
		return -1;
	}

	cvNamedWindow("avi");
	CvCapture* capture = cvCreateFileCapture("test4.mp4");

	IplImage* frame;

	while (1)
	{
		frame = cvQueryFrame(capture);
		if (!frame) break;

		//cvShowImage("avi", frame);
		detectAndDisplay("avi", frame);
		char c = cvWaitKey(20);

		if (c == 27)
			break;
	}
	cvReleaseCapture(&capture);

	cvDestroyWindow("avi");
	return 0;
}

Makefile:

CFLAGS = `pkg-config --cflags opencv` -I/usr/include/opencv2
LIBS = `pkg-config --libs opencv`

CC = g++

clean:
    rm -f *.o *~ test

main:main.cpp
    $(CC) $+ $(CFLAGS) $(LIBS) -o main.o  

编译用:make main



PS:这里Makefile要记住一点:

顺序一定要:$(CC) $+ $(CFLAGS) $(LIBS) -o main.o 而不能用:$(CC)  $(CFLAGS) $(LIBS) -o main.o $+  

不然会出现:“对‘cv::Mat::Mat(_IplImage const*, bool)’未定义的引用”之类的错误。

具体原因我也没搞懂,我测试了一下:

g++  -o main.o main.cpp `pkg-config --cflags opencv` -I/usr/include/opencv2 `pkg-config --libs opencv` #这个是OK的    

g++ `pkg-config --cflags opencv` -I/usr/include/opencv2 `pkg-config --libs opencv` -o main.o main.cpp  #这个就是不行的,反而按照规范这个才是正确写法吧


原因没搞懂,望有经验的同学予以指正。


你可能感兴趣的:(Linux,Windows)