缺少类opencv

时间:2013-10-04 20:11:36

标签: c++ opencv

我正在使用opencv3.0.0 dev版本和cmake.I按照本教程安装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;
}

0 个答案:

没有答案