gpt4 book ai didi

c++ - 在同一台计算机上运行多个版本的 OpenCV

转载 作者:IT老高 更新时间:2023-10-28 22:05:11 29 4
gpt4 key购买 nike

我的电脑正在运行 Ubuntu-16.04-LTS,并且已经安装了 OpenCV-2.4.13。但是,我想在不删除旧版本的情况下使用 OpenCV 较新版本的功能,例如 OpenCV-3.2.0。

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

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})

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

#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 version: 3.2.0

而不是

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

我得到了所有这些 undefined reference 错误:

CMakeFiles/cv_io.dir/io.cpp.o: In function main':
io.cpp:(.text+0x40): undefined reference to
cv::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 function
cv::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 function
cv::String::~String()': io.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x10): undefined reference to cv::String::deallocate()' CMakeFiles/cv_io.dir/io.cpp.o:
In function
cv::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 中的所有库。 .另外,我发现 a comment below this article提到类似于我正在经历的事情,但我不明白the page containing the solution它指的是。 我对 OpenCV 和 CMake 非常陌生,因此希望您能提供尽可能明确的说明。我一直坚持这一点,所以非常感谢任何帮助!非常感谢!

最佳答案

我有一个可用的 CMakelists.txt,其配置与您描述的几乎相同,只是我运行的是一台非常旧的 Ubuntu 12.04(它不是我自己的计算机)。

我相信你的问题来自这一行:

find_package(OpenCV REQUIRED)

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

这是我的 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(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

导出它来做到这一点

关于c++ - 在同一台计算机上运行多个版本的 OpenCV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45614230/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com