linking opencv libraries with g++(用 g++ 链接 opencv 库)
问题描述
您好,我正在尝试编译取自该网站的 c++ 文件:http://opencv-code.com/tutorials/eye-detection-and-tracking/ 用于眼动追踪.但我对此有点陌生,我真的不明白图书馆是如何链接的.我知道包含头文件的绝对路径位于/user/include/opencv2 中.如何在 gcc 命令行(ubuntu)中链接它?我试过这个命令:
Hello I am trying to compile a c++ file taken from this website: http://opencv-code.com/tutorials/eye-detection-and-tracking/ for eye-tracking. But I am kinda new to this and I don't really understand how libraries are linked. I know the absolute path to the include headers is located in /user/include/opencv2. How can I link it in a gcc command line (ubuntu)? I tried this command:
$ g++ -Wall eye-tracking.cpp -o eyeTracking
看来我还需要链接其他库.我尝试使用此命令链接找到的那些:
It seems I also need to link other libraries. I tried linking those found with this command:
$ pkg-config --libs opencv
但在这里我不知道如何将输出链接到我的命令.我通过输入以下命令尝试了我的逻辑:
But here again I have no clue how to link the output to my command. I tried with my logic by entering the following commands:
$g++ -Wall eye-tracking.cpp -I `pkg-config --libs opencv` -o eyeTracking
当然不行,我真的不明白我在做什么:P
Of course it doesn't work, I don't really understand what I am doing :P
谁能给我解释一下?
这里你会找到整个文件的代码:
Here you will find the whole file code:
/**
* eye-tracking.cpp:
* Eye detection and tracking with OpenCV
*
* This program tries to detect and tracking the user's eye with webcam.
* At startup, the program performs face detection followed by eye detection
* using OpenCV's built-in Haar cascade classifier. If the user's eye detected
* successfully, an eye template is extracted. This template will be used in
* the subsequent template matching for tracking the eye.
*/
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
cv::CascadeClassifier face_cascade;
cv::CascadeClassifier eye_cascade;
/**
* Function to detect human face and the eyes from an image.
*
* @param im The source image
* @param tpl Will be filled with the eye template, if detection success.
* @param rect Will be filled with the bounding box of the eye
* @return zero=failed, nonzero=success
*/
int detectEye(cv::Mat& im, cv::Mat& tpl, cv::Rect& rect)
{
std::vector<cv::Rect> faces, eyes;
face_cascade.detectMultiScale(im, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30,30));
for (int i = 0; i < faces.size(); i++)
{
cv::Mat face = im(faces[i]);
eye_cascade.detectMultiScale(face, eyes, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(20,20));
if (eyes.size())
{
rect = eyes[0] + cv::Point(faces[i].x, faces[i].y);
tpl = im(rect);
}
}
return eyes.size();
}
/**
* Perform template matching to search the user's eye in the given image.
*
* @param im The source image
* @param tpl The eye template
* @param rect The eye bounding box, will be updated with the new location of the eye
*/
void trackEye(cv::Mat& im, cv::Mat& tpl, cv::Rect& rect)
{
cv::Size size(rect.width * 2, rect.height * 2);
cv::Rect window(rect + size - cv::Point(size.width/2, size.height/2));
window &= cv::Rect(0, 0, im.cols, im.rows);
cv::Mat dst(window.width - tpl.rows + 1, window.height - tpl.cols + 1, CV_32FC1);
cv::matchTemplate(im(window), tpl, dst, CV_TM_SQDIFF_NORMED);
double minval, maxval;
cv::Point minloc, maxloc;
cv::minMaxLoc(dst, &minval, &maxval, &minloc, &maxloc);
if (minval <= 0.2)
{
rect.x = window.x + minloc.x;
rect.y = window.y + minloc.y;
}
else
rect.x = rect.y = rect.width = rect.height = 0;
}
int main(int argc, char** argv)
{
// Load the cascade classifiers
// Make sure you point the XML files to the right path, or
// just copy the files from [OPENCV_DIR]/data/haarcascades directory
face_cascade.load("haarcascade_frontalface_alt2.xml");
eye_cascade.load("haarcascade_eye.xml");
// Open webcam
cv::VideoCapture cap(0);
// Check if everything is ok
if (face_cascade.empty() || eye_cascade.empty() || !cap.isOpened())
return 1;
// Set video to 320x240
cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
cv::Mat frame, eye_tpl;
cv::Rect eye_bb;
while (cv::waitKey(15) != 'q')
{
cap >> frame;
if (frame.empty())
break;
// Flip the frame horizontally, Windows users might need this
cv::flip(frame, frame, 1);
// Convert to grayscale and
// adjust the image contrast using histogram equalization
cv::Mat gray;
cv::cvtColor(frame, gray, CV_BGR2GRAY);
if (eye_bb.width == 0 && eye_bb.height == 0)
{
// Detection stage
// Try to detect the face and the eye of the user
detectEye(gray, eye_tpl, eye_bb);
}
else
{
// Tracking stage with template matching
trackEye(gray, eye_tpl, eye_bb);
// Draw bounding rectangle for the eye
cv::rectangle(frame, eye_bb, CV_RGB(0,255,0));
}
// Display video
cv::imshow("video", frame);
}
return 0;
}
推荐答案
如果您查看 pkg-config --libs --cflags opencv
的输出,您会看到它设置了为您链接并包含行.您不需要输入 -l
或 -I
.
If you look at the output of pkg-config --libs --cflags opencv
you'll see that it sets up the link and include lines for you. You don't need to put the -l
or -I
.
pkg-config --libs <library>
输出库的链接参数.
pkg-config --cflags <library>
输出包含参数和任何其他需要的编译标志.
pkg-config --cflags <library>
outputs the include arguments and any other needed compile flags.
这篇关于用 g++ 链接 opencv 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用 g++ 链接 opencv 库
基础教程推荐
- 设计字符串本地化的最佳方法 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04