fatal error: opencv2/opencv_modules.hpp: No such file or directory #include "opencv2/opencv_modules.hpp"

fatal error: opencv2/opencv_modules.hpp: No such file or directory #include "opencv2/opencv_modules.hpp"

大家好,我正在尝试使用我从源代码构建的 opencv-c++ API(版本 4.4.0)。它安装在 /usr/local/ 中,我只是尝试使用以下代码加载和显示图像 -

#include <iostream>
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/core.hpp>
#include <opencv4/opencv2/imgcodecs.hpp>
#include <opencv4/opencv2/highgui.hpp>
#include <opencv4/opencv2/core/cuda.hpp>

using namespace cv;

int main()
{
    std::string image_path = "13.jpg";
    cv::Mat img = cv::imreadmulti(image_path, IMREAD_COLOR);
    if(img.empty())
    {
        std::cout<<"COULD NOT READ IMAGE"<<std::endl;
        return 1;
    }
    imshow("Display Window", img);
    return 0;
}

而且当我编译时它在编译过程中抛出以下错误-

In file included from /CLionProjects/opencvTest/main.cpp:2:
/usr/local/include/opencv4/opencv2/opencv.hpp:48:10: fatal error: opencv2/opencv_modules.hpp: No such file or directory
 #include "opencv2/opencv_modules.hpp"

我的Cmake如下-

cmake_minimum_required(VERSION 3.15)
project(opencvTest)

set(CMAKE_CXX_STANDARD 17)
include_directories("/usr/local/include/opencv4/opencv2/")
add_executable(opencvTest main.cpp)
target_link_libraries(opencvTest PUBLIC "/usr/local/lib/")

我不知道我在这里做错了什么。这可能是一个菜鸟问题,但我刚开始在 C++ 中使用 opencv

解决方案是 include_directories 到 /usr/local/opencv4 的路径,并且效果很好。

不过,我认为最好的方法是使用 find_package 函数。我将我的 Cmake 更新为以下内容,它负责构建期间的链接。

cmake_minimum_required(VERSION 3.15)
project(opencvTest)

set(CMAKE_CXX_STANDARD 17)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(opencvTest main.cpp)
target_link_libraries(opencvTest ${OpenCV_LIBS})

#include

应该是这样的。

相对地,header 搜索路径应该是: /usr/local/include/opencv4