“未在此范围内声明的功能”编译openCV代码时出错 [英] "Function not declared in this scope" Error in compiling openCV code

查看:1279
本文介绍了“未在此范围内声明的功能”编译openCV代码时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些使用openCV函数的代码。我首先介绍了文档中提供的一些示例代码:

I'm trying to write some code which uses openCV functions. I started by taking some of the example code available in the documentation:

#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中构建它时,我明白了:

When I try to build it in Eclipse-CDT, I get this:

**** 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)工作。

The same code, when I build with g++ (g++ -o displayImageInput displayImageInput.cpppkg-config opencv --cflags --libs) works.

然后我将代码更改为mak图像灰度,

I then changed the code to make the image greyscale,

#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 ++构建时出现以下错误:

It gave the following error when building with g++:

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

我需要两个方面的帮助,如何让它在Eclipse中运行,其次,如何解决此范围错误,以及将来的用例。

I need help with two things, How to get it working in Eclipse, and second, how to resolve this scope error, for this and future use cases.

推荐答案


  • 第一个错误是链接器问题。你没有链接到opencv_core.a和opencv_highgui.a

    • the first err is a linker problem. you did not link against opencv_core.a and opencv_highgui.a

      经验法则:对于你包含的每个模块头,你需要链接相应的库。

      rule of thumb: for every module header you include, you'll need to link the appropriate library.

      第二个是编译器问题,你忘了一个标题, #include< opencv2 / imgproc / imgproc.hpp>

      the 2nd is a compiler problem, you forgot a header, #include <opencv2/imgproc/imgproc.hpp>

      和ofc。需要链接opencv_imgproc

      and, ofc. need to link opencv_imgproc

      这篇关于“未在此范围内声明的功能”编译openCV代码时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆