Zbar+ROS+opencv二维码识别与定位研究(二)
程序员文章站
2022-05-29 09:23:11
...
本文是上篇博文的进阶,Zbar+ROS+opencv二维码识别与定位研究(一)
1.首先下载Zbar的源代码源代码链接
在ubuntu下,用命令行的方式在官网下载
$wget http://downloads.sourceforge.net/zbar/0.10/zbar-0.10.tar.bz2
2.安装相关包
$sudo apt install python-gtk2 python-gtk2-dev libmagickwand-dev imagemagick autoconf libv4l-dev
3.设置和编译
$sudo ln -s /usr/include/libv4l1-videodev.h /usr/include/linux/videodev.h
$tar xf zbar-0.10.tar.bz2
$cd zbar-0.10
$sed -i 's|linux/videodev.h|libv4l1-videodev.h|g' zbar/video/v4l1.c include/config.h.in configure.ac configure
$./configure --prefix=/usr --without-gtk --without-python
$make
$sudo make install
注意:如果make的时候报错:
在./configure上增加一行
$export CFLAGS=""
4.测试一下
$zbarimg 111.jpeg//111.jpeg为测试二维码
显示结果:
[email protected]:~/图片$ zbarimg 111.jpeg
QR-Code:http://weixin.qq.com/r/d0iJkerEhrf5ra7y9x1l
scanned 1 barcode symbols from 1 images in 0.06 seconds
1)工程环境:QT+C++
2)编译方法:CMake
3)用到的opencv和zbar库
CMakeList.txt文件:
cmake_minimum_required(VERSION 2.8.3)
project(zbar_opencv)
set(CMAKE_MODULE_PATH ${ZBARCV_SOURCE_DIR})
find_package (OpenCV)
find_package(catkin REQUIRED COMPONENTS
cv_bridge##ros的数据转化成opencv数据,再用opencv处理
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
)
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_executable(zbar_opencv src/zbar_opencv.cpp)
target_link_libraries(zbar_opencv
${catkin_LIBRARIES}
${OpenCV_LIBRARIES}
# ${Zbar_LIBRARIES}
/usr/lib/libzbar.so##最重要的添加编译用的共享库
)
1)在主程序zbar_opencv.cpp里面自定义类,实现将usb摄像头采集回来的/usb_cam/image_raw,通过cv_ptr=cv_bridge::toCvCopy(msg,sensor_msgs::image_encodings::BGR8);使用image_transport订阅图像话题“in” 和 发布图像话题“out” /camera/rgb/image_raw
2)转换完了以后,用opencv的方法,调用gradient.cpp,查看具体实现在开头链接
3)最后用定位好的图像传入zbarscanner()里面进行扫描识别
zbar_opencv.cpp如下:
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” /camera/rgb/image_raw
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;
}
//梯度运算
cv_ptr=gradient(cv_ptr);
//水平投影法
// projection(cv_ptr);
zbarscanner(cv_ptr);
// printf("OK1\n");
image_pub.publish(cv_ptr->toImageMsg());
}
};