18

Hello all I am trying to use opencv-c++ API (version 4.4.0) which I have built from source. It is installed in /usr/local/ and I was simply trying to load and display an image using the following code -

#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;
}

And when I compile it throws the following error during compilation -

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"

My Cmake is as follows -

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/")

I do not know what am I doing wrong here.. This might be a noob question, But I ahev just started using opencv in C++

5
  • 2
    You need /usr/local/include/opencv4 to be in your include directories not /usr/local/include/opencv4/opencv2 Commented Aug 17, 2020 at 17:13
  • 1
    Taking a guess, your path is incorrect. try this include_directories("/usr/local/include/"). You shouldn't have opencv4/opencv2 in both your include path and in your include directives. The include directive starts where the include path stops. Commented Aug 17, 2020 at 17:14
  • @john the error I think is actually generated in opencv4/opencv2/opencv.hpp where it is not able to find "opencv_modules" even though they are in the same directory. I tried your suggestion as well and it did not work, however I do not think it would actually make a difference.. I may be wrong about it though Commented Aug 17, 2020 at 17:38
  • 5
    What worked for me was: sudo cp -r /usr/local/include/opencv4/opencv2 /usr/include/ Commented Apr 12, 2021 at 16:12
  • 1
    @Owl your suggestions works. I got a second suggestion, who helped me too. Add in the tasks.json the following at "args:" "-I/usr/local/include/opencv4", Commented Mar 21, 2023 at 14:43

2 Answers 2

18

The solution is to just include_directories path till /usr/local/opencv4 and it works perfectly.

However, the best way I believe is to use the find_package function. I updated my Cmake to the following and it takes care of linking during build.

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})
Sign up to request clarification or add additional context in comments.

3 Comments

This is indeed a better and more proper CMake solution.
Thanks for this answer! It is exactly what I have been looking for for hours...
Thanks! In my case, "include_directories(${OpenCV_INCLUDE_DIRS})" did worked!
1

#include <opencv2/opencv.hpp>

it should look like that.

relatively, the header search paths should be: /usr/local/include/opencv4

1 Comment

Please, use proper code formatting options available in StackOverflow editor when publishing an answer or a question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.