gpt4 book ai didi

c++ - Opencv2 示例程序不工作

转载 作者:太空宇宙 更新时间:2023-11-04 09:03:19 25 4
gpt4 key购买 nike

我正在尝试让 opencv2 代码在我的 Ubuntu 12.04 上运行。似乎库已正确安装。

这是我尝试开始工作的示例代码

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

using namespace cv;

int main(int argc, char ** argv)
{
// Read image and write to matrix - here converted to grayimage by "0"
Mat myimage = imread ( argv[1], 0 );

// Show it in a window
imshow ( "MyWindowTitle", myimage);

// Wait for a keypress ( for x [ms], 0 means infinity )
waitKey(0);
}

我将这段代码编译为:

g++ -o sample `pkg-config --cflags --libs opencv` sample.cpp

但是我得到以下编译器错误:

/tmp/cccMPtyu.o: In function `main':
imshow.cpp:(.text+0x59): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
imshow.cpp:(.text+0x92): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
imshow.cpp:(.text+0xc9): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
imshow.cpp:(.text+0xeb): undefined reference to `cv::waitKey(int)'
/tmp/cccMPtyu.o: In function `cv::Mat::~Mat()':
imshow.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x2b): undefined reference to `cv::fastFree(void*)'
/tmp/cccMPtyu.o: In function `cv::Mat::release()':
imshow.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x3b): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status

可能的原因是什么?我该如何纠正它?

还有谁能推荐一个专门针对 opencv2 的好教程。我以前用 opencv1 编程过,但似乎有很多变化。

最佳答案

添加库路径

export LD_LIBRARY_PATH=$LD_LIBARARY_PATH:/usr/local/lib

然后使用命令

g++ -o sample sample.cpp `pkg-config --cflags --libs opencv` 

关于c++ - Opencv2 示例程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16029625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com