gpt4 book ai didi

c++ - "Function not declared in this scope"编译openCV代码出错

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:19:33 24 4
gpt4 key购买 nike

我正在尝试编写一些使用 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.cpppkg-config opencv --cflags --libs)有效。

然后我更改了代码使图像变成灰度,

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

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 中工作,其次,如何解决这个范围错误,用于这个和 future 的用例。

最佳答案

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

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

  • 第二个是编译器问题,您忘记了 header ,#include <opencv2/imgproc/imgproc.hpp>

    而且,当然。需要链接opencv_imgproc

关于c++ - "Function not declared in this scope"编译openCV代码出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17878160/

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