CMake does not find OpenCV

2019-05-26 16:52发布

问题:

I seem to have an issue with my CMakeLists and opencv. I am currently using opencv version 2.4 which is confirmed by

pkg-config --modversion opencv

2.4.8

and

dpkg -l libopencv*

which outputs http://pastebin.com/ZJEfLjDX

But for some reason when i cmake a simple program such as

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>\n");
        return -1;
    }

    Mat image;
    image = imread( argv[1], 1 );

    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);

    waitKey(0);

    return 0;
}

Will cmake not continue due to this error.

CMake Error at /opt/ros/indigo/share/OpenCV-3.1.0-dev/OpenCVModules.cmake:183 (message):
  The imported target "opencv_core" references the file

     "/opt/ros/indigo/lib/libopencv_core3.so.3.1.0"

  but this file does not exist.  Possible reasons include:

  * The file was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and contained

     "/opt/ros/indigo/share/OpenCV-3.1.0-dev/OpenCVModules.cmake"

  but not all the files it references.

Call Stack (most recent call first):
  /opt/ros/indigo/share/OpenCV-3.1.0-dev/OpenCVConfig.cmake:86 (include)
  CMakeLists.txt:3 (find_package)


-- Configuring incomplete, errors occurred!

with this CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

The project has no ros dependency, and I have no idea why it is looking for OpenCVConfig.cmake inside ros.

A simple locate shows that the correct version is available other places

locate OpenCVConfig.cmake

/home/k/opencv/opencv-2.4.8/build/OpenCVConfig.cmake
/home/k/opencv/opencv-2.4.8/build/unix-install/OpenCVConfig.cmake
/home/k/opencv/opencv-2.4.8/cmake/OpenCVConfig.cmake
/home/k/opencv/opencv-2.4.8/cmake/templates/OpenCVConfig.cmake.in
/opt/ros/indigo/share/OpenCV-3.1.0-dev/OpenCVConfig.cmake
/usr/local/share/OpenCV/OpenCVConfig.cmake
/usr/share/OpenCV/OpenCVConfig.cmake

but why does it choose to use the one from ros, which I am not sure is even an option if I don't have opencv3.1.0-dev installed?

I know that a solution to this problem would be SET the path. But why can't find_package make use of the 2.4.8 version? I even tried to change the filename of /opt/ros/indigo/share/OpenCV-3.1.0-dev/OpenCVConfig.cmake to /opt/ros/indigo/share/OpenCV-3.1.0-dev/OpenCVConfig.cmake.old, which caused the same error?... Why is cmake not able to find opencv by simple using find_package( OpenCV REQUIRED ), and how do I make it so that it possible to find using this.

标签: opencv cmake