Ros下跑opencv zbar 识别二维码Qr

xiaoxiao2021-02-28  12

环境 : ubuntu14.04+ros indigo 推荐大家使用robware这个软件,方便,不用做中间的一些复杂的操作~ 首先执行如下程序:

$ cd ~/catkin_ws/src 02.$ git clone https://github.com/bosch-ros-pkg/usb_cam.git 03.$ cd ~/catkin_ws 04.$ catkin_make

接下来需要在你的工作空间下 创建一个功能包,我这里创建的是zbar_opencv, 创建一个zbar_opencv.cpp

#include <ros/ros.h> #include <image_transport/image_transport.h> #include <cv_bridge/cv_bridge.h> #include <sensor_msgs/image_encodings.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <zbar.h> #include <iostream> #include <iomanip> using namespace std; using namespace cv; using namespace zbar; //static const std::string OPENCV_WINDOW="Image window"; void zbarscanner(cv_bridge::CvImagePtr cv_ptr) { // Create a zbar reader ImageScanner scanner; // Configure the reader scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1); // Capture an OpenCV frame cv::Mat frame,frame_grayscale; frame=cv_ptr->image; // Convert to grayscale cvtColor(frame, frame_grayscale, CV_BGR2GRAY); // Obtain image data int width = frame_grayscale.cols; int height = frame_grayscale.rows; uchar *raw = (uchar *)(frame_grayscale.data); // Wrap image data Image image(width, height, "Y800", raw, width * height); // Scan the image for barcodes // scanner.scan(image); // Extract results int counter = 0; for (Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol) { cout<< symbol->get_data() << endl; // Draw location of the symbols found if (symbol->get_location_size() == 4) { line(frame, Point(symbol->get_location_x(0), symbol->get_location_y(0)), Point(symbol->get_location_x(1), symbol->get_location_y(1)), Scalar(0, 255, 0), 2, 8, 0); line(frame, Point(symbol->get_location_x(1), symbol->get_location_y(1)), Point(symbol->get_location_x(2), symbol->get_location_y(2)), Scalar(0, 255, 0), 2, 8, 0); line(frame, Point(symbol->get_location_x(2), symbol->get_location_y(2)), Point(symbol->get_location_x(3), symbol->get_location_y(3)), Scalar(0, 255, 0), 2, 8, 0); line(frame, Point(symbol->get_location_x(3), symbol->get_location_y(3)), Point(symbol->get_location_x(0), symbol->get_location_y(0)), Scalar(0, 255, 0), 2, 8, 0); } counter++; } } class ImageConverter { ros::NodeHandle nh; image_transport::ImageTransport it; image_transport::Subscriber image_sub; image_transport::Publisher image_pub; public: ImageConverter():it(nh) { //使用image_transport订阅图像话题“in” 和 发布图像话题“out” image_sub=it.subscribe("/usb_cam/image_raw",1,&ImageConverter::imageCb,this); image_pub=it.advertise("zbar_opencv",1); } ~ImageConverter(){} //订阅回调函数 void imageCb(const sensor_msgs::ImageConstPtr& msg) { cv_bridge::CvImagePtr cv_ptr; try { //将ROS图像消息转化为适合Opencv的CvImage cv_ptr=cv_bridge::toCvCopy(msg,sensor_msgs::image_encodings::BGR8); } catch(cv_bridge::Exception& e) { ROS_ERROR("cv_bridge exception: %s",e.what()); return; } zbarscanner(cv_ptr); image_pub.publish(cv_ptr->toImageMsg()); } }; int main(int argc, char **argv) { ros::init(argc,argv,"zbar_opencv"); ImageConverter ic; ros::spin(); return 0; }

注意需要 添加一些依赖项: camke.list如下:

cmake_minimum_required(VERSION 2.8.3) project(zbar_opencv) set(CMAKE_MODULE_PATH ${ZBARCV_SOURCE_DIR}) find_package (OpenCV) find_package (ZBar0) find_package(catkin REQUIRED COMPONENTS cv_bridge image_transport roscpp sensor_msgs std_msgs ) find_package(PkgConfig) pkg_check_modules(PC_ZBAR QUIET zbar) set(ZBAR_DEFINITIONS ${PC_ZBAR_CFLAGS_OTHER}) find_library(ZBAR_LIBRARIES NAMES zbar HINTS ${PC_ZBAR_LIBDIR} ${PC_ZBAR_LIBRARY_DIRS} ) find_path(ZBAR_INCLUDE_DIR Decoder.h HINTS ${PC_ZBAR_INCLUDEDIR} ${PC_ZBAR_INCLUDE_DIRS} PATH_SUFFIXES zbar ) include(FindPackageHandleStandardArgs) find_package_handle_standard_args(ZBAR DEFAULT_MSG ZBAR_LIBRARIES ZBAR_INCLUDE_DIR) catkin_package( # INCLUDE_DIRS include # LIBRARIES zbar_opencv CATKIN_DEPENDS cv_bridge image_transport roscpp sensor_msgs std_msgs # DEPENDS system_lib ) include_directories( ${catkin_INCLUDE_DIRS} ) add_executable(zbar_opencv src/zbar_opencv.cpp) target_link_libraries(zbar_opencv ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} ${ZBAR_LIBRARIES} )

接下来编译就好了,运行如下:

roslaunch usb_cam usb_cam-test.launch rosrun zbar_opencv zbar_opencv

问题:有人编译不过去,说是出现zbar0找不到,这里可以进行如下更改: 首先对cmake.list里面的内容进行更改: 注释掉find_package (ZBar0),加#进行注释。 接着修改package.xml的内容: 最后拿着 二维码 就可以进行解析了 最好别直接copy我的运行指令,还是自己去敲键盘吧。

转载请注明原文地址: https://www.6miu.com/read-850102.html

最新回复(0)