cv rgb2gray python_Ros Kinetic 配置 OpenCV2和CV_bridge (Python, C++)

本篇介绍如何在Ros-kinetic环境下运用opencv2进行开发的配置,系统平台为64位Ubuntu16.04。

需要系统环境:

1.Ros kinetic版本,一般自带cv_bridge, 若没有可以通过apt下载

sudo apt-get install ros-kinetic-cv-bridge

2.OpenCV 2.4.9版本,一般来说cv_bridge依赖的OpenCV版本为2.4.8,亲测2.4.9可以用,安装可以参考https://blog.csdn.net/u013250416/article/details/78913126

2.2 事先安装下列软件

sudo apt-get install build-essential cmake libgtk2.0-dev pkg-config python-dev python-numpy libavcodec-dev libavformat-dev libswscale-dev

2.3 进入cmake并编译

cd cmake

cmake-D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..sudo make install

2.4 测试

新建C++代码test.cpp:

1 #include

2 #include

3 #include

4 #include

5 using namespacecv;6 using namespacestd;7 int main (int argc, char **argv)8 {9 Mat image, image_gray;10 image = imread(argv[1], CV_LOAD_IMAGE_COLOR );11 if (argc != 2 || !image.data) {12 cout << "No image data\n";13 return -1;14 }15

16 cvtColor(image, image_gray, CV_RGB2GRAY);17 namedWindow("image", CV_WINDOW_AUTOSIZE);18 namedWindow("image gray", CV_WINDOW_AUTOSIZE);19

20 imshow("image", image);21 imshow("image gray", image_gray);22

23 waitKey(0);24 return 0;25 }

新建makefile文件:

CC = g++# 可执行文件

TARGET=test

# C文件

SRCS= main.cpp# 目标文件

OBJS= $(SRCS:.cpp=.o)

# 库文件

DLIBS= -lopencv_core -lopencv_imgproc -lopencv_highgui

# 链接为可执行文件

$(TARGET):$(OBJS)

$(CC)-o $@ $^$(DLIBS)

clean:rm -rf $(TARGET) $(OBJS)

# 编译规则 $@代表目标文件 $

make,产生了两个文件:test文件和test.o文件 ,并测试:

./test lena.bmp

注:程序运行的过程中,常见错误error while loading shared libraries: libopencv_highgui.so.2.4: cannot open,解决方法:

1.用gedit打开/etc/ld.so.conf,注意要用sudo打开获得权限,不然无法修改,

如:sudo gedit /etc/ld.so.conf,在文件中加上一行 /usr/loacal/lib,/user/loacal是opencv安装路径 就是makefile中指定的安装路径

加了之后的变为

2.再运行sudo ldconfig,

修改bash.bashrc文件,sudo gedit /etc/bash.bashrc

在文件末尾加入:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig

export PKG_CONFIG_PATH

加后的图:

3.

source /etc/bash.bashrc

然后就可以正常运行了!

Python环境配置:

python下由于ros一般自带cv_bridge,系统同样自带cv2,处理起来比较简单,当摄像机采出的图像以rostopic发布时,可以用以下代码进行测试:

1 importrospy2 from sensor_msgs.msg importImage3 importcv24 from cv_bridge importCvBridge, CvBridgeError5

6 classImage_Receiver:7 def __init__(self):8 rospy.Subscriber('/camera/rgb/image_color', Image, callback=self.image_rgb_callback, queue_size=1000)9 self.cv_bridge =CvBridge()10 rospy.spin()11

12 defimage_rgb_callback(self, data):13 cv_image = self.cv_bridge.imgmsg_to_cv2(data, "bgr8")14 (rows, cols, channnels) =cv_image.shape15 if cols > 60 and rows > 60:16 cv2.circle(cv_image, (50, 50), 10, 255)17 cv2.imshow("Image Window", cv_image)18 cv2.waitKey(3)19

20 if __name__ == '__main__':21 rospy.init_node('rgb_image_get_node')22 Image_Receiver()

C++ QT环境配置:

由于本人开发环境为qt,则需要在qmake执行.pro文件时同时加载ros和opencv2的环境,在.pro文件中添加以下依赖:

# ROS Depends

INCLUDEPATH+= /opt/ros/kinetic/include \

DEPENDPATH+= /opt/ros/kinetic/include \

LIBS+= -L/opt/ros/kinetic/lib \-L/usr/local/lib \-lroscpp \-lrospack \-lpthread \-lrosconsole \-lrosconsole_log4cxx \-lrosconsole_backend_interface \-lxmlrpcpp \-lroscpp_serialization \-lrostime \-lcpp_common \-lroslib \-ltf \-lyaml-cpp\-lkdl_conversions \-lcv_bridge \-lboost_system

# OpenCV Depends

LIBS+= -lopencv_core \-lopencv_imgproc \-lopencv_highgui

编写测试代码如下:

#include #include#include

classImage_Receiver

{public:

Image_Receiver()

{

subsRgbImage= nh.subscribe<:image>("/camera/rgb/image_color", 1000, &Image_Receiver::image_rgb_callback, this);

ros::spin();

}~Image_Receiver()

{

}void image_rgb_callback(const sensor_msgs::Image::ConstPtr&data)

{

cv_bridge::CvImagePtr cv_ptr;try{

cv_ptr=cv_bridge::toCvCopy(data, sensor_msgs::image_encodings::BGR8);

}catch (cv_bridge::Exception&e) {

std::cout<< e.what() <<:endl>

}

receiveFlag= true;

}private:

sensor_msgs::Image::ConstPtr m_data;

ros::NodeHandle nh;

ros::Subscriber subsRgbImage;boolreceiveFlag;

};int main(int argc, char**argv)

{

ros::init(argc, argv,"image_receive_node");

Image_Receiver image_receiver_instance;return 0;

}

你可能感兴趣的:(cv,rgb2gray,python)