在同一台计算机上运行多个版本的OpenCV [英] Running multiple versions of OpenCV on the same computer

查看:330
本文介绍了在同一台计算机上运行多个版本的OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计算机正在运行Ubuntu-16.04-LTS,并且已经安装了OpenCV-2.4.13.但是,我想使用OpenCV的较新版本的功能,例如OpenCV-3.2.0,而不会删除较旧的版本.

My computer is running Ubuntu-16.04-LTS and OpenCV-2.4.13 is already installed on it. However, I would like to use the functionalities of newer versions of OpenCV, such as OpenCV-3.2.0 without removing the older version.

到目前为止,我已经下载了OpenCV-3.2.0并进行了编译和安装.我正在使用CMake来编译OpenCV,所以我将CMakeLists.txt文件更改为:

So far I have downloaded OpenCV-3.2.0 and compiled and installed it. I am uing CMake to compile OpenCV, so I changed my CMakeLists.txt file to:

cmake_minimum_required (VERSION 3.2)

project(io)

find_package(OpenCV REQUIRED)

include_directories("/home/ubuntu/opencv-3.2.0/include") # directory of OpenCV-3.2.0
link_directories("/home/ubuntu/opencv-3.2.0/lib") # directory of OpenCV-3.2.0

add_executable(cv_io io.cpp)

target_link_libraries(cv_io ${OpenCV_LIBS})

现在,当我运行这段小代码时,

Now, when I run this little piece of code,

#include <iostream>
#include "opencv2/core/version.hpp"

int main(int argc, char ** argv)
{
  std::cout << "OpenCV version: "
            << CV_MAJOR_VERSION << "." 
            << CV_MINOR_VERSION << "."
            << CV_SUBMINOR_VERSION
            << std::endl;
  return 0;
}

我知道

OpenCV版本:3.2.0

OpenCV version: 3.2.0

代替

OpenCV版本2.4.13

OpenCV version 2.4.13

因此,一切似乎都井井有条,除非我开始运行一些实际的OpenCV功能,例如:

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>

int main()
{
    cv::Mat img = cv::imread("ferrari.jpg");

    cv::Mat dst;
    cv::Sobel(img, dst, CV_32F, 1, 1);

    cv::imwrite("ferrari_sobel.png", dst);

    cv::VideoCapture input(0);
}

我得到所有这些未定义的参考错误:

I get all these undefined reference errors:

CMakeFiles/cv_io.dir/io.cpp.o:在函数main': io.cpp:(.text+0x40): undefined reference to cv :: imread(cv :: String const& ;, int)'io.cpp :(.text + 0xd4):未定义的引用 cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)' CMakeFiles/cv_io.dir/io.cpp.o: In function cv :: String :: String(char const *)': io.cpp :(.text._ZN2cv6StringC2EPKc [_ZN2cv6StringC5EPKc] + 0x40): 对cv::String::allocate(unsigned long)' CMakeFiles/cv_io.dir/io.cpp.o: In function cv :: String ::〜String()'的未定义引用: io.cpp :(.text._ZN2cv6StringD2Ev [_ZN2cv6StringD5Ev] + 0x10):未定义 引用cv::String::deallocate()' CMakeFiles/cv_io.dir/io.cpp.o: In function cv :: String :: operator =(cv :: String const&)': io.cpp :(.text. ZN2cv6StringaSERKS0 [ ZN2cv6StringaSERKS0 ] + 0x2c): 未定义对`cv :: String :: deallocate()'的引用collect2:错误:ld 返回1个退出状态CMakeFiles/cv_io.dir/build.make:121:的配方 目标'cv_io'失败make 2 : * [cv_io]错误1 CMakeFiles/Makefile2:67:目标'CMakeFiles/cv_io.dir/all'的配方 失败的make 1 :* [CMakeFiles/cv_io.dir/all]错误2 Makefile:83: 目标全部"的配方失败:*** [全部]错误2

CMakeFiles/cv_io.dir/io.cpp.o: In function main': io.cpp:(.text+0x40): undefined reference tocv::imread(cv::String const&, int)' io.cpp:(.text+0xd4): undefined reference to cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)' CMakeFiles/cv_io.dir/io.cpp.o: In functioncv::String::String(char const*)': io.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x40): undefined reference to cv::String::allocate(unsigned long)' CMakeFiles/cv_io.dir/io.cpp.o: In functioncv::String::~String()': io.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x10): undefined reference to cv::String::deallocate()' CMakeFiles/cv_io.dir/io.cpp.o: In functioncv::String::operator=(cv::String const&)': io.cpp:(.text.ZN2cv6StringaSERKS0[ZN2cv6StringaSERKS0]+0x2c): undefined reference to `cv::String::deallocate()' collect2: error: ld returned 1 exit status CMakeFiles/cv_io.dir/build.make:121: recipe for target 'cv_io' failed make2: * [cv_io] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/cv_io.dir/all' failed make1: * [CMakeFiles/cv_io.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2

有人知道如何解决这个问题吗?我认为问题是我仍然没有正确链接CMakeLists.txt中的所有库.此外,我发现本文下方的评论提到了与我所遇到的类似的内容,但我不理解

Does anyone know how to solve this problem? I think the problem is that I'm still not linking all the libraries properly in the CMakeLists.txt. Also, I found a comment below this article mentioning something similar to what I'm experiencing, but I do not understand the page containing the solution it is referring to. I'm very new to OpenCV and CMake, so hopefully you can provide me instructions that are as explicit as possible. I've been stuck on this for forever, so any help is highly appreciated! Thank you very much!

推荐答案

除了运行的是令人生畏的旧版Ubuntu 12.04(它不是我自己的计算机)之外,我使用的CMakelists.txt几乎与您所描述的配置相同.

I have a working CMakelists.txt for almost the same configuration as you describe except that I am running a dauntingly old Ubuntu 12.04 (its not my own computer).

我相信您的问题来自此行:

I believe your problem comes from this line:

find_package(OpenCV REQUIRED)

这使您可以访问发行版的OpenCV 2.4.然后,您将链接到手动安装的3.2.x版本.因此,一旦您使用的函数的接口在两个版本之间发生了变化,就会出现问题.我认为您的第一段代码是偶然运行的.

Which gives you access to your distribution's OpenCV 2.4. Then you are linking against the manually installed 3.2.x version. So problems arise as soon as the interface of a function you use has changed between the two version. Your first piece of code run by chance I think.

这是我的CMakeList.txt:

cmake_minimum_required(VERSION 2.8)
project(demo)

find_package(OpenCV 3.2 REQUIRED PATHS "/path/to/OCV3.2/install/dir/")

include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})

如果您不想将OpenCV 3.2安装的硬编码路径提交到存储库中,则可以通过将find_package行更改为以下内容来完善此CMakeList.txt:

If you do not want to commit to your repository the hardcoded path to your install of OpenCV 3.2 you can refine this CMakeList.txt by changing the find_package line to:

if(DEFINED ENV{OPENCV_INSTALL_DIR})
    find_package(OpenCV 3.2 REQUIRED PATHS $ENV{OPENCV_INSTALL_DIR})
else()
    message("OPENCV_INSTALL_DIR not set, searching in default location(s)")
    find_package(OpenCV 3.2 REQUIRED)
endif(DEFINED ENV{OPENCV_INSTALL_DIR})

然后,您只需要在运行cmake之前定义变量OPENCV_INSTALL_DIR.我是通过从.bashrc

Then you just have to define the variable OPENCV_INSTALL_DIR before running cmake. I do that by exporting it from my .bashrc

这篇关于在同一台计算机上运行多个版本的OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆