Install ROS-melodic
# Setup your sources.list
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
# Set up your keys
sudo apt install curl
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
# Installation
sudo apt update
sudo apt install ros-melodic-desktop-full
# Environment setup
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc
# Dependencies for building packages
sudo apt install python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential
sudo apt install python-rosdep
sudo rosdep init
rosdep update
ROS安装(博主github):
https://github.com/TwilightZrui/Linux-Ubuntu/blob/main/ROS-Melodic.md
查看opencv的安装
opencv查看已有安装
pkg-config --modversion opencv
查找opencv的安装路径
sudo find / -iname "*opencv*"
C++测试代码
#include "opencv2/core.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main()
{
cout << "Opencv" << CV_VERSION << endl;
return 0;
}
CMakeLists:
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
# Define project name
project(cv_hw)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
# Declare the executable target built from your sources
add_executable(cv_hw1 cv_hw1.cpp)
# Link your application with OpenCV libraries
target_link_libraries(cv_hw1 ${OpenCV_LIBS})
一个报错:
(CVclass) twilight@pc: ~/project/class/cv2021/cv_hw1$ cmake .
CMake Deprecation Warning at CMakeLists.txt:2 (cmake_minimum_required):
Compatibility with CMake < 2.8.12 will be removed from a future version of
CMake.
Update the VERSION argument <min> value or use a ...<max> suffix to tell
CMake that the project does not need compatibility with older versions.
CMake Error at CMakeLists.txt:10 (find_package):
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
See also "/home/twilight/project/class/cv2021/cv_hw1/CMakeFiles/CMakeOutput.log".
问题定位:
Cmakelists中没有添加
include_directories(${OpenCV_INCLUDE_DIRS})
因此系统找不到OpenCV路径。
当然,前提是在bashrc中添加ROS路径,以证明是使用ROS中自带的opencv3.2.0
source /opt/ros/melodic/setup.bash