【问题标题】:Compiling a program for using a Basler Camera编译使用 Basler 相机的程序
【发布时间】:2019-10-03 09:42:06
【问题描述】:

我正在尝试获取一个使用 Basler 相机捕获图像的 C++ 程序。我拿到 来自制造商的代码,它应该“非常易于使用”,但是,链接它有 成为噩梦。我的 C++ 时代已经过去(最近只使用 Matlab),所以我可能会犯一些愚蠢的错误,但请赐教:

代码如下所示:

// Include files to use the PYLON API.
#include <pylon/PylonIncludes.h>
#ifdef PYLON_WIN_BUILD
#    include <pylon/PylonGUI.h>
#endif

// Namespace for using pylon objects.
using namespace Pylon;
using namespace GenApi;


#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/video/video.hpp>
//#include <opencv2/videoio.hpp>

using namespace cv;




// Namespace for using cout.
using namespace std;

#include <stdio.h>
#include "kbhit.h"

#include <sys/stat.h>  // for checking the file size
#include <sstream>

// Number of images to be grabbed.
//static const uint32_t c_countOfImagesToGrab = 100;




int main(int argc, char* argv[])
{
    // The exit code of the sample application.
    int exitCode = 0;

    // Automagically call PylonInitialize and PylonTerminate to ensure the pylon runtime system
    // is initialized during the lifetime of this object.
    Pylon::PylonAutoInitTerm autoInitTerm;
    VideoWriter cvVideoCreator;
    struct stat statbuf;
    string filenameBase = "/opt/PylonTestAVI";
    uint filecounter = 1;
    string filename = "";

    ... (and so on)

我正在尝试使用以下代码对其进行编译:

g++ GrabV3.cpp -I/opt/pylon5 -I/opt/pylon5/include -I/usr/local/include -I/usr/include/ -L/opt/pylon5/lib64 

这里/opt/pylon5是Basler自己的库,/usr/local/include是opencv4文件夹的链接。

我收到一页长的错误消息列表 - 开头看起来像这样 GrabV3.cpp:(.text+0x2a5): 未定义引用 cv::VideoWriter::VideoWriter()' GrabV3.cpp:(.text+0x32a): undefined reference toPylon::CTlFactory::GetInstance()' GrabV3.cpp:(.text+0x346): 未定义引用Pylon::CDeviceInfo::CDeviceInfo()' GrabV3.cpp:(.text+0x370): undefined reference toPylon::CInstantCamera::CInstantCamera(Pylon::IPylonDevice, Pylon::ECleanup)' GrabV3.cpp:(.text+0x38e): 未定义引用Pylon::CInstantCamera::Open()' GrabV3.cpp:(.text+0x39d): undefined reference toPylon::CInstantCamera::GetNodeMap()' GrabV3.cpp:(.text+0x3cb): 未定义引用GenICam_3_1_Basler_pylon::gcstring::gcstring(char const*)' GrabV3.cpp:(.text+0x41b): undefined reference toGenICam_3_1_Basler_pylon::gcstring::~gcstring()' GrabV3.cpp:(.text+0x465): 未定义引用GenICam_3_1_Basler_pylon::gcstring::gcstring(char const*)' GrabV3.cpp:(.text+0x488): undefined reference toGenICam_3_1_Basler_pylon::gcstring::~gcstring()' GrabV3.cpp:(.text+0x4af): 未定义引用GenICam_3_1_Basler_pylon::gcstring::gcstring(char const*)' GrabV3.cpp:(.text+0x4ff): undefined reference toGenICam_3_1_Basler_pylon::gcstring::~gcstring()' GrabV3.cpp:(.text+0x526): 未定义引用GenICam_3_1_Basler_pylon::gcstring::gcstring(char const*)' GrabV3.cpp:(.text+0x576): undefined reference toGenICam_3_1_Basler_pylon::gcstring::~gcstring()' GrabV3.cpp:(.text+0x745): undefined reference to `cv::VideoWriter::open(cv::String const&, int, double, cv::Size_, bool)'*

显然没有任何效果,从 cv:VideoWriter() 函数开始,它是标准 OpenCV 的一部分(我使用本教程安装:https://cv-tricks.com/installation/opencv-4-1-ubuntu18-04/)。

所以我在这里迷路了 - 已经花了大约一天的时间试图让它工作。有人可以帮忙吗?

【问题讨论】:

  • 请尝试g++ 而不是gcc。 (缺少对std::basic_string 的引用让我认为std c++ lib。没有链接。g++ 默认情况下应该这样做。)
  • 再次查看您的命令,我坚信有/缺少 lib。这会导致左侧未定义的引用错误(链接错误)。详细说明cmd。 args.:-I(大写 i)用于包含路径(用于编译器)。 -L 用于库路径(用于链接器)。 -l (small ell) 用于图书馆。我在您的编译命令中没有看到任何 -l 参数。我相信这就是你所缺少的。
  • 正如@Scheff 提到的,您需要链接到正确的库。正如我所看到的,您只链接到这个 pylon 库,但您也使用了 opencv,并且您还获得了一个未定义的参考(我相信单独安装不足以让链接器找到正确的库)。
  • 所以,这个命令会告诉你pkg-config 知道的关于pkg-config --list-all 的所有包,你应该能够找到 OpenCVPylon 在那里,比如 opencv4。然后您可以使用pkg-config --libs --cflags opencv4 获取相应的库,并将其放入您的编译命令中$(...)。 Pylon 也是如此。
  • 好的,更详细地查看您的代码...您代码中的包含文件本身对于 OpenCV4 是错误的。它们都变了,现在整个 OpenCV 只有一个包含文件,而不是 core.hpphighgui.hppvideo.hpp 我认为是 #include &lt;opencv2/opencv.hpp&gt;

标签: c++ opencv


【解决方案1】:

好的,所以 Mark 的建议解决了链接问题。但是,由于该软件仍未编译,而且我们仍处于“编译使用 Basler 相机的程序”主题中,我将继续处理范围问题:

g++ $(pkg-config --cflags --libs opencv4) GrabV3.cpp -I/opt/pylon5/include -I/opt/pylon5/include/pylon -L/opt/pylon5/lib64 - L/opt/pylon5/include/pylon

现在给出以下错误:

 GrabV3.cpp: In function ‘int main(int, char**)’:
  GrabV3.cpp:84:32: error: ‘CV_FOURCC’ was not declared in this scope
  cvVideoCreator.open(filename,CV_FOURCC('D','I','V','X'),20,FrameSize,true);

在“等等”之后(见我的第一篇文章),代码继续如下:

 try
{
    // Create an instant camera object with the camera device found first.
    CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());

    camera.Open();

    INodeMap& nodeMap = camera.GetNodeMap();
    CEnumerationPtr TestImageSelector = nodeMap.GetNode("TestImageSelector");
    TestImageSelector->FromString("Testimage4");

    CIntegerPtr Width = nodeMap.GetNode("Width");
    CIntegerPtr Height = nodeMap.GetNode("Height");
    Size FrameSize = Size(Width->GetValue(),Height->GetValue());
    stringstream s ;
    s<<"_" ;
    s<< filecounter;
    filename = filenameBase;
    filename += s.str() + ".avi";

    //cvVideoCreator.open("``/opt/PylonTest_DIVX.avi",CV_FOURCC('M','P','4','2'),20,FrameSize,true);
    cvVideoCreator.open(filename,CV_FOURCC('D','I','V','X'),20,FrameSize,true);
    //cvVideoCreator.open("/opt/PylonTest.avi",CV_FOURCC('M','J','P','G'),20,FrameSize,true);


    // Print the model name of the camera.
    cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;

    // The parameter MaxNumBuffer can be used to control the count of buffers
    // allocated for grabbing. The default value of this parameter is 10.
    camera.MaxNumBuffer = 5;
    CImageFormatConverter fc;
    fc.OutputPixelFormat = PixelType_BGR8packed;
    CPylonImage image;

    // Start the grabbing of c_countOfImagesToGrab images.
    // The camera device is parameterized with a default configuration which
    // sets up free-running continuous acquisition.
    camera.StartGrabbing();

    // This smart pointer will receive the grab result data.
    CGrabResultPtr ptrGrabResult;

CV_FOURCC('D','I','V','X') 是定义在 /usr/include/opencv2/videoio/videoio_c.h,现在超出了某些人的范围 原因。

我试图通过包含

来暴力链接它
#include "/usr/include/opencv2/videoio/videoio_c.h"

但这导致链接完全搞砸了。 感谢@Mark 让我走到这一步;从这里怎么走?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多