我正在使用带有cmake的opencv3.0.0开发版本。我按照本教程安装了opencv http://docs.opencv.org/trunk/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation
之后,我使用以下 cmake 文件来编译一个简单的面部检测器:
cmake_minimum_required(VERSION 2.8)
project( face-detect )
find_package( OpenCV 3.0.0 EXACT REQUIRED )
add_executable( face-detect face-detect.cpp )
target_link_libraries( face-detect ${OpenCV_LIBS} )
Makefile 已成功生成,但是当我编译代码时,命名空间 cv 下缺少类:
face-detect.cpp:15: error: 'CommandLineParser' is not a member of 'cv'
cv::CommandLineParser parser(argc, argv, keys);
^
face-detect.cpp:23: error: invalid initialization of reference of type 'cv::InputArray {aka const cv::_InputArray&}' from expression of type 'const char*'
std::cout << cv::format("Error: cannot load cascade file!\n");
但是它确实找到了 cv::CascadeClassifier。如何解决这个问题?
这是人脸检测的代码,我从互联网上获取的:
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
const char* keys =
{
"{i|input| |The source image}"
"{o|outdir| |The output directory}"
};
int main(int argc, const char** argv)
{
cv::CommandLineParser parser(argc, argv, keys);
std::string infile = parser.get<std::string>("input");
std::string outdir = parser.get<std::string>("outdir");
std::string cascade_file = "haarcascade_frontalface_alt.xml";
cv::CascadeClassifier cascade;
if (cascade_file.empty() || !cascade.load(cascade_file))
{
std::cout << cv::format("Error: cannot load cascade file!\n");
return -1;
}
cv::Mat src = cv::imread(infile);
if (src.empty())
{
std::cout << cv::format("Error: cannot load source image!\n");
return -1;
}
cv::Mat gray;
cv::cvtColor(src, gray, CV_BGR2GRAY);
cv::equalizeHist(gray, gray);
std::vector<cv::Rect> faces;
cascade.detectMultiScale(gray, faces, 1.2, 3);
std::cout << cv::format("0, %s (%dx%d)\n", infile.c_str(), src.cols, src.rows);
cv::Mat src_copy = src.clone();
for (int i = 0; i < faces.size(); i++)
{
std::string outfile(cv::format("%s/face-%d.jpg", outdir.c_str(), i+1));
cv::Rect r = faces[i];
cv::rectangle(src, r, CV_RGB(0,255,0), 2);
cv::imwrite(outfile, src_copy(r));
cv::imwrite(infile, src);
std::cout << cv::format("%d, %s (%dx%d)\n", i+1, outfile.c_str(), r.width, r.height);
}
return 0;
}