在学习人脸识别项目的过程中写好了一个程序,但是一直报 undefined reference to `cv::Mat::Mat()'的错误,请问应该如何解决
1.这是想要编译的可执行文件,face_recognition.cpp
include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/mat.hpp>
#include <vector>
#include <string>
using namespace std;
using namespace cv;
class HarrFaceRecognition
{
public:
HarrFaceRecognition()
{
img=imread("test.jpg",IMREAD_GRAYSCALE);
result = imread("test.jpg");
harr_xml="haarcascade_frontalface_default.xml";
}
void GetFace()
{
//创建检测器
CascadeClassifier object;
//加载采样文件
object.load(harr_xml);
//检测人脸
object.detectMultiScale(img,faces,1.1,3);
}
void Show()
{
for(int i=0;i<faces.size();i++)
{
//人脸矩形框的左上角坐标
int x=faces[i].tl().x;
int y=faces[i].tl().y;
//人脸矩形框的宽度和高度
int width=faces[i].width;
int height=faces[i].height;
rectangle(result,Rect(x,y,width,height),Scalar(255,0,0));
}
imshow("result",result);
waitKey(600);
}
protected:
Mat img;//原图---->灰度加载
Mat result;//显示
string harr_xml;
vector<Rect> faces;
};
int main()
{
HarrFaceRecognition* p= new HarrFaceRecognition;
p->GetFace();
p->Show();
return 0;
}
2.launch.json文件
{
"configurations": [
{
"name": "C/C++: cpp 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [
"`pkg-config","--libs","--cflags","opencv4`",
],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file", //要和 task.json的label字段一致
"miDebuggerPath": "/usr/bin/gdb",
//下面这行命令可以去除Vscode中编译后出现 [1]+ Done.... 的提示
"miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /usr/bin/gdb -q --interpreter=mi"
}
],
"version": "2.0.0"
}
3.task.json文件
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
//"command": "/usr/bin/cpp", //不能用这命名, 否则你可能会i报错 gdb failed with message 'file format not recognized'
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out",
"`pkg-config","--libs","--cflags","opencv4`" //这是添加第三方库代码,要添加什么库直接继续加就行,不要忘记了 ` 符号
//如下面再加个 gazebo11 第三方库(你得保证你安装的第三方库中含有.pc文件)
// "`pkg-config","--libs","--cflags","opencv4,gazebo11`"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
4.c_cpp_properties.json文件
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/x86_64-linux-gnu/sys",
"/usr/local/include/opencv2", //记得把opencv4的include路径包含,按我上述方法来安装opencv的话一般就是这个路径
"/usr/include/x86_64-linux-gnu/sys" //这是装了cuda的路径,如果无cuda请去除
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
然后这个文件夹里面是没有CMakeLists.txt文件的,不知道是不是没有这个文件的原因,还是说要找到mat库文件的路径(不知道具体怎么操作),希望有人能帮忙解答一下。