17

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++

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Atharva Dubey
  • 832
  • 1
  • 8
  • 25
  • 2
    You need `/usr/local/include/opencv4` to be in your include directories not `/usr/local/include/opencv4/opencv2` – drescherjm Aug 17 '20 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. – john Aug 17 '20 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 – Atharva Dubey Aug 17 '20 at 17:38
  • 5
    What worked for me was: sudo cp -r /usr/local/include/opencv4/opencv2 /usr/include/ – Owl Apr 12 '21 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",` – EE_G_RLP Mar 21 '23 at 14:43

2 Answers2

16

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})
Atharva Dubey
  • 832
  • 1
  • 8
  • 25
1

#include <opencv2/opencv.hpp>

it should look like that.

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

Evx
  • 21
  • 1