[英]Opencv2 Sample Program not working
我試圖在我的Ubuntu 12.04上使用opencv2代碼。 好像庫已正確安裝。
這是我要開始工作的示例代碼
#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`
看起來pkg-config
程序假定opencv
libs
的庫路徑位於默認路徑中,而事實並非如此。
嘗試將-L
選項添加到gcc
行。 ( -L{path to opencv libs}
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.