级联分类器-opencv使用

最简单的使用

级联分类器使用方法特别简单,调入一个文件即可,也可以做训练,具体训练方法参考其他,这里是如何使用

侦测前脸

使用训练好的文件haarcascade_frontalface_alt.xml,注意不要单独拷贝这个文件,opencv 还需要etc下面的其他文件

#include 
#include 
#include 
#include 
#include 
#include "opencv2/objdetect.hpp"
#include 
#include 
#include 
#include 
using std::cout;
using std::cerr;
using std::vector;
using std::string;
using namespace cv;
// Detect and draw detected object boxes on image
//
void detectAndDraw(
	cv::Mat& img,                               // input image
	CascadeClassifier* classifier,  // preloaded classifier
	double scale = 1.3) {                       // resize image by ...
												// Just some pretty colors to draw with
												//
	enum { BLUE, AQUA, CYAN, GREEN };
	static cv::Scalar colors[] = {
		cv::Scalar(0, 0, 255),
		cv::Scalar(0, 128, 255),
		cv::Scalar(0, 255, 255),
		cv::Scalar(0, 255, 0)
	};
	// Image preparation:
	//
	cv::Mat gray(img.size(), CV_8UC1);
	cv::Mat small_img(cv::Size(cvRound(img.cols / scale),
		cvRound(img.rows / scale)), CV_8UC1);
	cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
	cv::resize(gray, small_img, small_img.size(), 0.0, 0.0, cv::INTER_LINEAR);
	cv::equalizeHist(small_img, small_img);
	// Detect objects if any
	//
	vector<cv::Rect> objects;
	classifier->detectMultiScale(
		small_img,                  // input image
		objects,                    // place for the results
		1.1,                        // scale factor
		3,                          // minimum number of neighbors
		0,   // (old format cascades only)
		cv::Size(30, 30));          // throw away detections smaller than this

									// Loop through to found objects and draw boxes around them
									//
	int i = 0;
	for (vector<cv::Rect>::iterator r = objects.begin();
		r != objects.end(); r++, ++i) {
		cv::Rect r_ = (*r);
		r_.x *= scale;
		r_.y *= scale;
		r_.width *= scale;
		r_.height *= scale;
		cv::rectangle(img, r_, colors[i % 4]);
	}
}

const char* params_1
= "{ help           | false | Sample app for loading googlenet model }"
"{ proto          | bvlc_googlenet.prototxt | model configuration }"
"{ model          | bvlc_googlenet.caffemodel | model weights }"
"{ label          | synset_words.txt | names of ILSVRC2012 classes }"
"{ image          | faces.png | path to image file }"
"{ opencl         | false | enable OpenCL }"
;
int main(int argc, char** argv) {
	
	CV_TRACE_FUNCTION();
	CommandLineParser parser(argc, argv, params_1);
	String file_path = parser.getPathToApplication() + "/";
	string image_file_name =   "girl2.jpg"; // parser.get("image");// "oldboy.jpg";// string(argv[1]);
	cv::Mat img = cv::imread(image_file_name, 1);
	//String cascade_file_name = "haarcascade_frontalface_alt.xml"; //string(argv[2]);
	CascadeClassifier face_cascade;
	face_cascade.load("D:/opencv4lib/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml");
	//cv::Ptr cascade(new cv::CascadeClassifier(cascade_file_name));
	detectAndDraw(img, &face_cascade);
	cv::imshow("Result", img);
	cv::waitKey(0);

	return 0;
}

结果

级联分类器-opencv使用_第1张图片

其他

我们可以使用分类器训练其他,比如狗脸,猫脸,甚至一个车牌等等,具体训练方法请查阅其他文章

你可能感兴趣的:(opencv和AI,c++,opencv,级联分类器)