簡體   English   中英

將opencv庫與g ++鏈接起來

[英]linking opencv libraries with g++

你好我正在嘗試編譯一個來自這個網站的c ++文件: http//opencv-code.com/tutorials/eye-detection-and-tracking/用於眼睛跟蹤。 但我對此有點新鮮,我並不真正理解庫是如何鏈接的。 我知道include頭的絕對路徑位於/ user / include / opencv2中。 如何在gcc命令行(ubuntu)中鏈接它? 我試過這個命令:

$ g++ -Wall eye-tracking.cpp -o eyeTracking

看來我還需要鏈接其他庫。 我嘗試將使用此命令找到的鏈接:

$ pkg-config --libs opencv

但在這里我再也不知道如何將輸出鏈接到我的命令。 我通過輸入以下命令嘗試了我的邏輯:

$g++ -Wall eye-tracking.cpp -I `pkg-config --libs opencv` -o eyeTracking

當然它不起作用,我真的不明白我在做什么:P

有人能解釋一下怎么樣?

在這里您可以找到整個文件代碼:

/**
* 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

pkg-config --libs <library>輸出pkg-config --libs <library>的鏈接參數。

pkg-config --cflags <library>輸出include參數和任何其他所需的編譯標志。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM