有没有办法修复编译器找不到特定的 openCV 变量/函数?

Posted

技术标签:

【中文标题】有没有办法修复编译器找不到特定的 openCV 变量/函数?【英文标题】:Is there a way to fix the compiler not finding specific openCV variables/functions? 【发布时间】:2019-05-03 04:40:12 【问题描述】:

经过长时间的麻烦,我刚刚安装了 openCV 3.2.0。我尝试编写一个程序来加载图像,将其转换为灰度,写入它然后显示两者。问题是每当我尝试运行时

g++ main.cpp -o 输出`pkg-config --cflags --libs opencv`

我得到以下内容

main.cpp: In function ‘int main()’:
main.cpp:14:27: error: ‘COLOR_BGR2GRAY’ was not declared in this scope
   cvtColor(image, gImage, COLOR_BGR2GRAY);
                           ^~~~~~~~~~~~~~
main.cpp:14:27: note: suggested alternative: ‘CV_BGR2GRAY’
   cvtColor(image, gImage, COLOR_BGR2GRAY);
                           ^~~~~~~~~~~~~~
                           CV_BGR2GRAY
main.cpp:14:3: error: ‘cvtColor’ was not declared in this scope
   cvtColor(image, gImage, COLOR_BGR2GRAY);
   ^~~~~~~~
main.cpp:14:3: note: suggested alternative: ‘cvCvtColor’
   cvtColor(image, gImage, COLOR_BGR2GRAY);
   ^~~~~~~~
   cvCvtColor

我正在阅读 openCV 教程,并且正在阅读我的版本 3.2.0,它正在使用我尝试使用的名称。

这是我的代码:

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

using namespace cv;

int main()

  Mat image = imread("test.jpeg", 1);
  Mat gImage;
  cvtColor(image, gImage, COLOR_BGR2GRAY);

  imwrite("test_gray.jpeg", gImage);

  namedWindow("Display image", WINDOW_AUTOSIZE);
  imshow("Display image", image);

  namedWindow("gray", WINDOW_AUTOSIZE);
  imshow("gray", gImage);

  waitKey(0);
  return 0;

我试着跑了

pkg-config --modversion opencv

检查我是否安装了正确版本的 opencv 并按预期返回“3.2.0”。 如果这有任何相关性,我也正在运行 Mint 19.1(我是初学者,所以我不太了解高级用户可能建议/做的事情)

【问题讨论】:

pkg-config --cflags --libs 不应该在滴答声中吗? @WhozCraig 是的,我用记号写了它,但忘记转义了,现在编辑它 我认为您缺少包含 #include &lt;opencv2/imgproc.hpp&gt;,您也可以使用包含大部分 #include &lt;opencv2/opencv.hpp&gt; 的 opencv 标头 @api55 是的,我只需要 imgproc.hpp,谢谢!我使用了他们在我的版本的官方文档/教程中包含的相同内容,我想知道为什么他们认为它当时有效。奇怪的。无论如何,再次感谢。 我认为它从版本 3.4 -> 4.0 或 3.2 -> 3.4 不是 100% 确定的,但对于 3.2 来说就是这样。 【参考方案1】:
#include <opencv2/imgproc.hpp>

是我所需要的。没有写在官方教程的代码中,所以我不知道我需要它。

【讨论】:

以上是关于有没有办法修复编译器找不到特定的 openCV 变量/函数?的主要内容,如果未能解决你的问题,请参考以下文章

MinGW C++ 编译器在系统路径中找不到 OpenCV

Ubuntu找不到OpenCV里的xml文件

IntelliJ 找不到特定的方法

OpenCV - 编译器找不到 features2d.hpp 的 drawKeypoints?

无法编译第一个 opencv 程序,'/usr/bin/ld: 找不到 -lcv'

魔改Cmake系列:编译caffe的时候找不到OpenCV