1.Python环境
----python pip
sudo apt install python3-pip
sudo pip3 install --upgrade pip
--python Opencv
OpenCV3在Python中包名称是cv2,而不是cv3?
实际上,”cv2”中的”2”并不表示OpenCV的版本号。
OpenCV是基于C/C++的,”cv”和”cv2”表示的是底层C API和C++API的区别,”cv2”表示使用的是C++API
opencv-contrib-python 与 opencv-python 有何不同
一个是基本装opencv,带contrib的是高配版,带一些收费或者专利的算
pip3 -V
pip -V
pip3 install opencv-python -i https://mirrors.aliyun.com/pypi/simple/
python
import cv2
print(cv2.__version__)
2.C++ opencv环境
在Ubuntu14.04和16.04下一般运行 sudo apt-get install libopencv-dev
Ubuntu18.04中 sudo apt-get install libopencv-dev
-- 查看版本 pkg-config --modversion opencv
#必须指定到包含 .cmake的上一层
set(OpenCV_DIR /usr/share/OpenCV)
find_package(OpenCV REQUIRED)
3.Ros运行
01.启动 roscore;
roscore
02.编译Mes
流程:
按照固定格式创建 msg 文件
编辑配置文件
编译生成可以被 Python 或 C++ 调用的中间文件
编译后的中间文件查看
C++ 需要调用的中间文件(.../工作空间/devel/include/包名/xxx.h)
Python 需要调用的中间文件(.../工作空间/devel/lib/python3/dist-packages/包名/msg)
cd ~/code/work_project/src
catkin_create_pkg vision roscpp std_msgs cv_bridge sensor_msgs
cd ~/code/work_project
catkin_make
03.启动发布节点;
cd ~/Data
rosbag play -l Evaluation.bag
04.启动订阅节点
mkdir vision/scripts
cd ~/code/work_project/vision/scripts
touch HelloImg.py
chmod +x HelloImg.py
cd ..
vim CamkeList.txt
cd ~/code/work_project
catkin_make
source ./devel/setup.bash
rosrun vision view_sub
05.启动查看 rviz是ros自带的一个图形化工具,可以方便的对ros的程序进行图形化操作
cd ~/code/work_project
rviz
06.杀死ros进程
killall -9 roscore
killall -9 rosmaster
4.Ros步骤
##只播放感兴趣的 topic ,则用命令
rosbag play ~/Data/Evaluation.bag --topic /usb_cam/image_raw
rosbag play -l Evaluation.bag
rosbag info ~/Data/Evaluation.bag
rosbag play ~/Data/Evaluation.bag --topic /usb_cam/image_raw
空格键可以恢复/暂停播放
rostopic info /usb_cam/image_raw
rostopic type /usb_cam/image_raw
rostopic echo /usb_cam/image_raw
rosmsg show sensor_msgs/Image
topic
/usb_cam/image_raw msgs : sensor_msgs/Image
common_msgs: sensor_msgs 消息的定义和使用
http://docs.ros.org/en/api/sensor_msgs/html/msg/Image.htm
sensor_msgs/CameraInfo
sensor_msgs/CompressedImage
sensor_msgs/Image
sensor_msgs/Imu
rostopic info /usb_cam/image_raw
Type: sensor_msgs/Image
Publishers:
* /play_123 (http://Ubuntu-111:39395/)
Subscribers: None
header:
seq: 46699
stamp:
secs: 1626943378
nsecs: 834539851
frame_id: "usb_cam"
height: 240
320
encoding: "rgb8"
is_bigendian: 0
step: 960
data:
5.ROS使用Opencv
ROS以其自己的sensor_msgs/Image消息格式发布图像
CvBridge是一个ROS库,提供ROS和OpenCV之间的接口。 可以在vision_opencvv堆栈的cv_bridge软件包中找到CvBridge
使用CvBridge将ROS图像转换为OpenCV cv::Matt格式
将OpenCV图像转换为ROS格式,以便通过ROS发布图像消息
:~/code/work_project$ locate OpenCVConfig.cmake
/usr/share/OpenCV/OpenCVConfig.cmake
set(Opencv_DIR /usr/share/OpenCV)
find_package( Opencv NAMES OpenCV REQUIRED)
6.错误处理
----ModuleNotFoundError: No module named ‘skbuild‘
sudo apt install cmake
pip install scikit-build
法
pip3 uninstall opencv-python
1.image_sub = rospy.Subscriber('camera/rgb/image_raw', Image, imaghe_callback)
ouldn't find executable named ImgVision.py below /home//code/work_project/src/vision
[rosrun] Found the following, but they're either not files,
[rosrun] or not executable:
[rosrun] /home/traveler/code/work_project/src/vision/scripts/ImgVision.py
traveler@Ubuntu-069:~/code/work_project$ chmod +x /home/traveler/code/work_project/src/vision/scripts/ImgVision.py
注意c++的代码是不需要加后缀的
rosrun vision grayImage
rosrun vision ImgVision.py
2.错误类型
TypeError: unbound method onlyCellValue() must be called with dictionary instance as first argument (got str instance instead)
CvBridge.bridge.imgmsg_to_cv2
后经在网上查看,发现时由于调用其他类时,未在后面添加括号,添加括号后,运行正常 CvBridge().bridge.imgmsg_to_cv2
Ros命名
Valid Names
A valid name has the following characteristics:
First character is an alpha character ([a-z|A-Z]), tilde (~) or forward slash (/)
Subsequent characters can be alphanumeric ([0-9|a-z|A-Z]), underscores (_), or forward slashes (/)
Exception: base names (described below) cannot have forward slashes (/) or tildes (~) in them.
参考:
ROS机器人编程实践----琐碎知识点 https://www.cnblogs.com/dingyc/p/10676756.html