开发环境:Ubuntu 18.04 LTS + ROS Melodic + ViSP 3.3.1
文章内容主要参考ViSP官方教学文档:https://visp-doc.inria.fr/doxygen/visp-daily/tutorial_mainpage.html

  本文主要介绍了如何使用ViSP实现连通区域的跟踪,具体而言就是以鼠标点击的位置作为起始点搜索最近的连通区域,然后根据当前连通区域的信息,在一幅图像中搜索与之匹配的其他连通区域,如果在连续的图像序列中分别寻找匹配的连通区域的话就可以实现视频中连通区域的跟踪了。本文主要参考了 blob中的 tutorial-blob-auto-tracker.cpp 例程。首先要获取这个例程文件并编译它

svn export https://github.com/lagadic/visp.git/trunk/tutorial/tracking/blob
cd blob
mkdir build
cd build 
cmake .. -DCMAKE_BUILD_TYPE=Release -DVISP_DIR=$VISP_WS/visp-build
make 

  执行例程,查看效果

./tutorial--blob-auto-tracker

在这里插入图片描述

  根据实验发现这个连通区域的搜索机制是依赖于尺寸信息的,而不是形状,如下图所示
在这里插入图片描述
  即使我选择的是圆形的连通区域,但是尺寸接近的正方形连通区域也会被检测出来,而尺寸不同的正方形连通区域则不会被检测出来。

  下面介绍一下代码实现过程

#include <visp3/blob/vpDot2.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpImageIo.h>

int main()
{
  try {
    bool learn = false; 
    //如果设置为true表示需要通过鼠标点击获取连通区域信息,默认为false则根据既定参数寻找连通区域
    vpImage<unsigned char> I; // Create a gray level image container

    vpImageIo::read(I, "./target.pgm");

#if defined(VISP_HAVE_X11)
    vpDisplayX d(I, 0, 0, "Camera view");
#elif defined(VISP_HAVE_GDI)
    vpDisplayGDI d(I, 0, 0, "Camera view");
#elif defined(VISP_HAVE_OPENCV)
    vpDisplayOpenCV d(I, 0, 0, "Camera view");
#else
    std::cout << "No image viewer is available..." << std::endl;
#endif
    vpDisplay::display(I);
    vpDisplay::flush(I);

    //! [Construction]
    vpDot2 blob;
    //! [Construction]
    //! [Learn]
    if (learn) {
    //如果learn参数为true则根据鼠标点击的位置来搜索连通区域
      // Learn the characteristics of the blob to auto detect
      blob.setGraphics(true);//显示连通区域轮廓
      blob.setGraphicsThickness(1);//轮廓边缘线条的粗细
      blob.initTracking(I);//根据鼠标点击的位置自动搜索连通区域
      blob.track(I);//更新联通区域的信息
      std::cout << "Blob characteristics: " << std::endl;
      std::cout << " width : " << blob.getWidth() << std::endl;//连通区域的宽度
      std::cout << " height: " << blob.getHeight() << std::endl;//连通区域的高度
#if VISP_VERSION_INT > VP_VERSION_INT(2, 7, 0)
      std::cout << " area: " << blob.getArea() << std::endl;//连通区域的面积
#endif
      std::cout << " gray level min: " << blob.getGrayLevelMin() << std::endl;//连通区域最小灰度值
      std::cout << " gray level max: " << blob.getGrayLevelMax() << std::endl;//连通区域最大灰度值
      std::cout << " grayLevelPrecision: " << blob.getGrayLevelPrecision() << std::endl;//连通区域灰度级精度
      std::cout << " sizePrecision: " << blob.getSizePrecision() << std::endl;//连通区域尺寸精度
      std::cout << " ellipsoidShapePrecision: " << blob.getEllipsoidShapePrecision() << std::endl;//连通区域椭圆度精度
    }
    //! [Learn]
    //! [Setting]
    else {
    //如果learn参数为false,则根据下面的设定好的参数来搜索连通区域
      // Set blob characteristics for the auto detection
      blob.setWidth(50);
      blob.setHeight(50);
#if VISP_VERSION_INT > VP_VERSION_INT(2, 7, 0)
      blob.setArea(1700);
#endif
      blob.setGrayLevelMin(0);
      blob.setGrayLevelMax(30);
      blob.setGrayLevelPrecision(0.8);
      blob.setSizePrecision(0.65);
      blob.setEllipsoidShapePrecision(0.65);
    }
    //! [Setting]

    //! [Search]
    std::list<vpDot2> blob_list;
    blob.searchDotsInArea(I, 0, 0, I.getWidth(), I.getHeight(), blob_list);
    //从图像I中搜索与blob相匹配的连通区域,第二和三个参数表示搜索区域的左上角顶点坐标
    //第四和五个参数表示搜索区域的面积,第六个参数表示用于储存连通区域的列表
    //! [Search]

    //! [Add learned dot]
    if (learn) {
      // The blob that is tracked by initTracking() is not in the list of auto
      // detected blobs We add it:
      //如果最初的联通区域模板是通过鼠标点击自动搜索得到的则需要手动添加到列表中
      blob_list.push_back(blob);
    }
    //! [Add learned dot]
    std::cout << "Number of auto detected blob: " << blob_list.size() << std::endl;
    std::cout << "A click to exit..." << std::endl;

    while (1) {
      vpDisplay::display(I);

      //! [Display]
      for (std::list<vpDot2>::iterator it = blob_list.begin(); it != blob_list.end(); ++it) {
        (*it).setGraphics(true);
        (*it).setGraphicsThickness(3);
        (*it).track(I);
      }
      //! [Display]

      vpDisplay::flush(I);

      if (vpDisplay::getClick(I, false))
        break;

      vpTime::wait(40);
    }
  } catch (const vpException &e) {
    std::cout << "Catch an exception: " << e << std::endl;
  }
}

如果大家对于深度学习与计算机视觉领域感兴趣,希望获得更多的知识分享与最新的论文解读,欢迎关注我的个人公众号“深视”。在这里插入图片描述