2013-07-26 40 views
4

我正在嘗試編寫一些使用openCV函數的代碼。我一開始就採取了一些示例代碼提供的文檔中:「在此範圍內未聲明的函數」編譯openCV代碼時出錯

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

using namespace cv; 
using namespace std; 

int main(int argc, char** argv) 
{ 
    if(argc != 2) 
    { 
    cout <<" Usage: display_image ImageToLoadAndDisplay" << endl; 
    return -1; 
    } 

    Mat image; 
    image = imread(argv[1]); // Read the file 

    if(! image.data)        // Check for invalid input 
    { 
     cout << "Could not open or find the image" << std::endl ; 
     return -1; 
    } 

    namedWindow("Display window", CV_WINDOW_AUTOSIZE);// Create a window for display. 
    imshow("Display window", image);     // Show our image inside it. 

    waitKey(0);           // Wait for a keystroke in the window 
    return 0; 
} 

當我嘗試建立它在Eclipse的CDT,我得到這個:

**** Build of configuration Debug for project openCV1 **** 

make all 
Building target: openCV1 
Invoking: Cross G++ Linker 
g++ -L/usr/local/lib -o "openCV1" ./src/displayImage.o 
./src/displayImage.o: In function `main': 
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:25: undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)' 
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:33: undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)' 
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:34: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)' 
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:34: undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)' 
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:36: undefined reference to `cv::waitKey(int)' 
./src/displayImage.o: In function `~Mat': 
/usr/local/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)' 
./src/displayImage.o: In function `cv::Mat::operator=(cv::Mat const&)': 
/usr/local/include/opencv2/core/mat.hpp:298: undefined reference to `cv::Mat::copySize(cv::Mat const&)' 
./src/displayImage.o: In function `cv::Mat::release()': 
/usr/local/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()' 
collect2: ld returned 1 exit status 
make: *** [openCV1] Error 1 

**** Build Finished **** 

相同的代碼,當我建立與g ++(g++ -o displayImageInput displayImageInput.cpp pkg-config opencv --cflags --libs)起作用。

dispImgSobel.cpp: In function ‘int main(int, char**)’: 
dispImgSobel.cpp:34:27: error: ‘CV_BGR2GRAY’ was not declared in this scope 
dispImgSobel.cpp:34:38: error: ‘cvtColor’ was not declared in this scope 

我需要兩樣東西幫助,如何獲得它:

然後我改變了代碼,使圖像灰度,

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

using namespace cv; 
using namespace std; 

int main(int argc, char** argv) 
{ 
    if(argc != 2) 
    { 
    cout <<" Usage: display_image ImageToLoadAndDisplay" << endl; 
    return -1; 
    } 

    Mat image; 
    image = imread(argv[1]); // Read the file 

    if(! image.data)        // Check for invalid input 
    { 
     cout << "Could not open or find the image" << std::endl ; 
     return -1; 
    } 

    Mat grey; 
    cvtColor(image, grey, CV_BGR2GRAY); 

    namedWindow("image", CV_WINDOW_AUTOSIZE); 
    imshow("image", grey); 
    waitKey();           // Wait for a keystroke in the window 
    return 0; 
} 

它與G ++構建時給出了以下錯誤在Eclipse中工作,第二,如何解決這個範圍錯誤,爲這個和未來的用例。

回答

7
  • 第一個err是一個鏈接器問題。你沒有鏈接到opencv_core.a和opencv_highgui.a

    經驗法則:對於你包含的每個模塊頭,你需要鏈接相應的庫。

  • 第二個是一個編譯器的問題,你忘了標題,#include <opencv2/imgproc/imgproc.hpp>

    和OFC。需要鏈接opencv_imgproc

+0

謝謝!我已將庫路徑添加到-L,但沒有意識到我必須明確添加到庫! – jackStinger

2

確保pkg-config opencv --cflags --libs的輸出中的所有內容都位於鏈接器的搜索路徑中。

還有一個pkg-config add-on for Eclipse這將允許您直接放在上面的確切字符串,而不是手動添加輸出的每個項目。