Visp系列学习二:visp显示和visp_ros获取图像

显示

#include 
#include 

int main()
{
    vpImage<unsigned char>I(1060, 1040, 40);

    try{
#if defined(VISP_HAVE_X11)
//vpDisplayX d(I, vpDisplay::SCALE_5); 降采样显示图像
    vpDisplayX d(I); //默认是显示原始大小图像
#endif

    vpDisplay::setTitle(I,"My image");
    vpDisplay::display(I);
    vpDisplay::displayCircle(I, I.getHeight()/2, I.getWidth()/2 , 200, vpColor::red, true);
    //刷新与图像I显示关联的输出缓冲区。 此功能可以查看任何绘图的结果。
    vpDisplay::flush(I);
    std::cout << "A click to quit..." << std::endl;
    vpDisplay::getClick(I);
    }catch(const vpException &e){
        std::cout << "catch an exception:" << e.getMessage() << std::endl;
    }
}


下载visp_ros

参考:
http://wiki.ros.org/visp_ros
http://wiki.ros.org/visp_ros/Tutorials/Howto_install_visp_ros
http://docs.ros.org/hydro/api/visp_ros/html/tutorial-ros-grabber.html

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ~/catkin_ws
catkin_make

cd ~/catkin_ws/src
git clone https://github.com/lagadic/visp_ros.git 

cd ~/catkin_ws
sudo rosdep init
rosdep update
rosdep install --from-paths src --ignore-src --rosdistro $ROS_DISTRO

cd ~/catkin_ws
catkin_make -DCMAKE_BUILD_TYPE=Release --pkg visp_ros

cd ~/catkin_ws
catkin_make install
source ~/catkin_ws/install/setup.bash
source ~/catkin_ws/devel/setup.bash

使用kinectV2+visp_ros显示

在visp_ros下的tutorial/grabber/ros/tutorial-ros-grabber.cpp和CMakeLists.txt

中修改,修改后代码如下所示。

编译后执行

roslaunch kinect2_bridge kinect2_bridge.launch
./tutorial-ros-grabber

CMakeLists.txt

project(tutorial-ros-grabber)

cmake_minimum_required(VERSION 2.6)

find_package(visp_ros REQUIRED)
find_package(VISP REQUIRED)

include_directories(${VISP_INCLUDE_DIRS})
include_directories(${visp_ros_INCLUDE_DIRS})

add_executable(tutorial-ros-grabber tutorial-ros-grabber.cpp)
target_link_libraries(tutorial-ros-grabber ${visp_ros_LIBRARIES} ${VISP_LIBRARIES})

tutorial-ros-grabber.cpp

//! \example tutorial-ros-grabber.cpp
#include 
#include 
#include 


int main(int argc, const char** argv)
{
  try {
    bool opt_use_camera_info = false;
    for (int i=0; i<argc; i++) {
      if (std::string(argv[i]) == "--use-camera-info")
        opt_use_camera_info = true;
      else if (std::string(argv[i]) == "--help") {
        std::cout << "Usage: " << argv[0]
                  << " [--use-camera-info] [--help]"
                  << std::endl;
        return 0;
      }
    }
    std::cout << "Use camera info: " << ((opt_use_camera_info == true) ? "yes" : "no") << std::endl;

    //vpImage I; // Create a gray level image container
    vpImage<vpRGBa> I; // Create a color image container
    //! [Construction]
    vpROSGrabber g; // Create a grabber based on ROS
    //! [Construction]

    //! [Setting camera topic]
    g.setImageTopic("/kinect2/qhd/image_color");
    //! [Setting camera topic]
    //! [Setting camera info]
    if (opt_use_camera_info) {
      g.setCameraInfoTopic("/kinect2/qhd/camera_info");
      g.setRectify(true);
    }
    //! [Setting camera info]

    //! [Opening]
    g.open(I);
    //! [Opening]
    std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;

#ifdef VISP_HAVE_X11
    vpDisplayX d(I);
#else
    std::cout << "No image viewer is available..." << std::endl;
#endif

    while(1) {
      //! [Acquisition]
      g.acquire(I);
      //! [Acquisition]
      vpDisplay::display(I);
      vpDisplay::displayText(I, 20, 20, "A click to quit...", vpColor::red);
      vpDisplay::flush(I);
      if (vpDisplay::getClick(I, false))
        break;
    }
  }
  catch(vpException e) {
    std::cout << "Catch an exception: " << e << std::endl;
  }
}

你可能感兴趣的:(机器视觉)