OpenCV2代码链接错误

时间:2017-12-30 11:15:19

标签: c++ opencv linkage

我尝试在Ubuntu 16.04上用C ++中的openCV编译这个简单演示用于HOG特征检测 Original Code

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main (int argc, const char * argv[])
{
    VideoCapture cap(CV_CAP_ANY);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);    
    if (!cap.isOpened())
        return -1;

    Mat img;
    HOGDescriptor hog;
    hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());

    namedWindow("video capture", CV_WINDOW_AUTOSIZE);
    while (true)
    {
        cap >> img;
        if (!img.data)
            continue;

        vector<Rect> found, found_filtered;
        hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);

        size_t i, j;
        for (i=0; i<found.size(); i++)
        {
            Rect r = found[i];
            for (j=0; j<found.size(); j++)
                if (j!=i && (r & found[j])==r)
                    break;
            if (j==found.size())
                found_filtered.push_back(r);
        }
        for (i=0; i<found_filtered.size(); i++)
        {
        Rect r = found_filtered[i];
            r.x += cvRound(r.width*0.1);
        r.width = cvRound(r.width*0.8);
        r.y += cvRound(r.height*0.06);
        r.height = cvRound(r.height*0.9);
        rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 2);
    }
        imshow("video capture", img);
        if (waitKey(20) >= 0)
            break;
    }
    return 0;
}

当我尝试使用

编译代码时
g++ -lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo  -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab hog.cpp

我收到以下错误

/tmp/cctllC37.o: In function `main':
hog.cpp:(.text+0x38): undefined reference to `cv::VideoCapture::VideoCapture(int)'
hog.cpp:(.text+0x65): undefined reference to `cv::VideoCapture::set(int, double)'
hog.cpp:(.text+0x92): undefined reference to `cv::VideoCapture::set(int, double)'
hog.cpp:(.text+0xa1): undefined reference to `cv::VideoCapture::isOpened() const'
hog.cpp:(.text+0xdc): undefined reference to `cv::HOGDescriptor::getDefaultPeopleDetector()'
hog.cpp:(.text+0x10e): undefined reference to `cv::HOGDescriptor::setSVMDetector(cv::_InputArray const&)'
hog.cpp:(.text+0x15b): undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
hog.cpp:(.text+0x17e): undefined reference to `cv::VideoCapture::operator>>(cv::Mat&)'
hog.cpp:(.text+0x23b): undefined reference to `cv::HOGDescriptor::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, cv::Size_<int>, cv::Size_<int>, double, double, bool) const'
hog.cpp:(.text+0x51e): undefined reference to `cv::rectangle(cv::Mat&, cv::Point_<int>, cv::Point_<int>, cv::Scalar_<double> const&, int, int, int)'
hog.cpp:(.text+0x545): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
hog.cpp:(.text+0x588): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
hog.cpp:(.text+0x5b0): undefined reference to `cv::waitKey(int)'
hog.cpp:(.text+0x637): undefined reference to `cv::VideoCapture::~VideoCapture()'
hog.cpp:(.text+0x71f): undefined reference to `cv::VideoCapture::~VideoCapture()'
/tmp/cctllC37.o: In function `cv::Mat::~Mat()':
hog.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cctllC37.o: In function `cv::Mat::release()':
hog.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to `cv::Mat::deallocate()'
/tmp/cctllC37.o: In function `cv::HOGDescriptor::HOGDescriptor()':
hog.cpp:(.text._ZN2cv13HOGDescriptorC2Ev[_ZN2cv13HOGDescriptorC5Ev]+0xd): undefined reference to `vtable for cv::HOGDescriptor'
/tmp/cctllC37.o: In function `cv::HOGDescriptor::~HOGDescriptor()':
hog.cpp:(.text._ZN2cv13HOGDescriptorD2Ev[_ZN2cv13HOGDescriptorD5Ev]+0xd): undefined reference to `vtable for cv::HOGDescriptor'
/tmp/cctllC37.o: In function `cv::_InputArray::_InputArray<float>(std::vector<float, std::allocator<float> > const&)':
hog.cpp:(.text._ZN2cv11_InputArrayC2IfEERKSt6vectorIT_SaIS3_EE[_ZN2cv11_InputArrayC5IfEERKSt6vectorIT_SaIS3_EE]+0x11): undefined reference to `vtable for cv::_InputArray'
collect2: error: ld returned 1 exit status

我尝试使用不同的编译器标志,但似乎没有任何效果。 我已经安装了我在默认的Ubuntu repro中找到的所有openCV包。

2 个答案:

答案 0 :(得分:0)

尝试使用&#34; -Lpath&#34;编译器标志告诉链接器安装库的位置,如果它们不是通常的/ usr / lib等...

确保所有库都存在于系统中。

答案 1 :(得分:0)

您的链接错误意味着编译器无法找到OpenCV的标头。所以你应该告诉编译器你的OpenCV安装在哪里。如:

g++ -std=c++11 test.cpp \
-I/home/xxx/Programs/OpenCV/shared_world/include/opencv \
-I/home/xxx/Programs/OpenCV/shared_world/include \
-L/home/xxx/Programs/OpenCV/shared_world/lib -lopencv_world 

顺便说一句,我自己编译并安装了OpenCV并安装了opencv.pc和环境设置。

编译并运行OpenCV程序时,我只使用这一行:

g++ -std=c++11 test.cpp `pkg-config --cflags --libs opencv`  -o main && ./main