【opencv】运行opencv3.4中的demo--facial_features.cpp
1.运行opencv3.4中的demo--facial_features.cpp
2.详解opencv中的CommandLineParser类
facial_features.cpp是一个检测人脸、眼睛、鼻子、嘴巴的cpp-demo,涉及到命令行操作,用的是opencv中的CommandLineParser类。facial_features.cpp在..\opencv3_4\opencv\sources\samples\cpp\facial_features.cpp 目录下。
1.运行opencv3.4中的demo--facial_features.cpp
【操作方法】
1.配置属性
配置属性==>调试==>命令参数:hebe2.jpg haarcascade_frontalface_alt.xml -eyes=haarcascade_eye.xml -noses=haarcascade_mcs_nose.xml -mouth=haarcascade_mcs_mouth.xml
注意:
1.每个命令参数之间用空格隔开;
2.上述路径实际是参数的路径,而不只是参数名称。例如,上述第一个参数 hebe2.jpg(实际上我将该图片文件放置在工程文件目录下了,所以可以省略路径)
如果不会设置相对文件路径,就直接将文件绝对路径写下来即可。
例如,hebe2.jpg的存放位置为"d:\opencv3_4\data\hebe2.jpg",则可以将上述第一个参数写为d:\opencv3_4\data\hebe2.jpg
同理,第二个参数的绝对路径为:"d:\opencv3_4\xml\haarcascade_frontalface_alt.xml" ,则可将第二个参数写为:d:\opencv3_4\xml\haarcascade_frontalface_alt.xml
用绝对路径的配置方法为:
配置属性==>调试==>命令参数:d:\opencv3_4\data\hebe2.jpg d:\opencv3_4\xml\haarcascade_frontalface_alt.xml -eyes=d:\opencv3_4\xml\haarcascade_eye.xml -noses=d:\opencv3_4\xml\haarcascade_mcs_nose.xml -mouth=d:\opencv3_4\xml\haarcascade_mcs_mouth.xml
运行程序的参数打印输出的结果如下:
2.修改程序
将源程序中的利用命令行获取路径的方式修改一下:
把这两行替换掉: input_image_path = parser.get<string>(0);
face_cascade_path = parser.get<string>(1);
替换为:input_image_path = argv[1];
face_cascade_path = argv[2];
//input_image_path = parser.get<string>(0);
//face_cascade_path = parser.get<string>(1);
input_image_path = argv[1];
face_cascade_path = argv[2];
因为,若运行原程序中的获取路径方式,会报错如下:(在没搞清楚原因的情况下,改为上述方式,可使得程序正常运行。)
3.运行结果:
从结果看,face、eyes、nose、mouth都检测出来了。
【程序代码+注释】
//facial_features.cpp: 定义控制台应用程序的入口点。
#include "stdafx.h"
/*
* Author: Samyak Datta (datta[dot]samyak[at]gmail.com)
*
* A program to detect facial feature points using
* Haarcascade classifiers for face, eyes, nose and mouth
*
*/
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
using namespace cv;
// Functions for facial feature detection
static void help();
static void detectFaces(Mat&, vector<Rect_<int> >&, string);
static void detectEyes(Mat&, vector