opencv轮廓提取原理与代码的实现

opencv轮廓提取原理与代码的实现,第1张

  在检测物体的轮廓时,我们通常会使用到opencv中的findcontour和drawcontour,比较常用而且效果不错。那么findcontour是基于什么原理来实现轮廓的提取呢?

  轮廓的提取与描述

  在目标识别中我们首先要把感兴趣的目标提取出来,而一般常见的步骤都是通过颜色或纹理提取出目标的前景图(一幅黑白图像,目标以白色显示在图像中),接下来我们要对前景图进行分析进一步地把目标提取出来,而这里常常用到的就是提取目标的轮廓。

  OpenCV里提取目标轮廓的函数是findContours,它的输入图像是一幅二值图像,输出的是每一个连通区域的轮廓点的集合:vector《vector《Point》》。外层vector的size代表了图像中轮廓的个数,里面vector的size代表了轮廓上点的个数。下面我们通过实例来看函数的用法。

  view plain copy print?

  int main()

  {

  using namespace cv;

  Mat image=imread(“。。/shape.png”);

  cvtColor(image,image,CV_BGR2GRAY);

  vector《vector《Point》》 contours;

  // find

  findContours(image,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);

  // draw

  Mat result(image.size(),CV_8U,Scalar(0));

  drawContours(result,contours,-1,Scalar(255),2);

  namedWindow(“contours”);

  imshow(“contours”,result);

  waitKey();

  return 0;

  }

  int main()

  {

  using namespace cv;

  Mat image=imread(“。。/shape.png”);

  cvtColor(image,image,CV_BGR2GRAY);

  vector《vector《Point》》 contours;

  // find

  findContours(image,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);

  // draw

  Mat result(image.size(),CV_8U,Scalar(0));

  drawContours(result,contours,-1,Scalar(255),2);

  namedWindow(“contours”);

  imshow(“contours”,result);

  waitKey();

  return 0;

  }

  

  上面程序中包含了2个函数,第一个是查找轮廓函数,它的第三个参数说明查找轮廓的类型,这里我们使用的是外轮廓,还可以查找所有轮廓,即

  包括一些孔洞的部分,像图像人物胳膊与腰间形成的轮廓。第4个参数说明了轮廓表示的方法,程序中的参数说明轮廓包括了所有点,也可以用其

  他参数让有点直线的地方,只保存直线起始与终点的位置点,具体参数用法可以参考手册里函数的介绍。

  第二个函数drawContours是一个画轮廓的函数,它的第3个参数程序里设置-1表示所有的轮廓都画,你也可以指定要画的轮廓的序号。

  提取到轮廓后,其实我们更关心的是如果把这些轮廓转换为可以利用的特征,也就是涉及到轮廓的描述问题,这时就有多种方法可以选择,比如矢

  量化为多边形、矩形、椭圆等。OpenCV里提供了一些这样的函数。

  [cpp] view plain copy print?

  // 轮廓表示为一个矩形

  Rect r = boundingRect(Mat(contours[0]));

  rectangle(result, r, Scalar(255), 2);

  // 轮廓表示为一个圆

  float radius;

  Point2f center;

  minEnclosingCircle(Mat(contours[1]), center, radius);

  circle(result, Point(center), staTIc_cast《int》(radius), Scalar(255), 2);

  // 轮廓表示为一个多边形

  vector《Point》 poly;

  approxPolyDP(Mat(contours[2]), poly, 5, true);

  vector《Point》::const_iterator itp = poly.begin();

  while (itp != (poly.end() - 1))

  {

  line(result, *itp, *(itp + 1), Scalar(255), 2);

  ++itp;

  }

  line(result, *itp, *(poly.begin()), Scalar(255), 2);

  // 轮廓表示为凸多边形

  vector《Point》 hull;

  convexHull(Mat(contours[3]), hull);

  vector《Point》::const_iterator ith = hull.begin();

  while (ith != (hull.end() - 1))

  {

  line(result, *ith, *(ith + 1), Scalar(255), 2);

  ++ith;

  }

  line(result, *ith, *(hull.begin()), Scalar(255), 2);

  // 轮廓表示为一个矩形

  Rect r = boundingRect(Mat(contours[0]));

  rectangle(result, r, Scalar(255), 2);

  // 轮廓表示为一个圆

  float radius;

  Point2f center;

  minEnclosingCircle(Mat(contours[1]), center, radius);

  circle(result, Point(center), staTIc_cast《int》(radius), Scalar(255), 2);

  // 轮廓表示为一个多边形

  vector《Point》 poly;

  approxPolyDP(Mat(contours[2]), poly, 5, true);

  vector《Point》::const_iterator itp = poly.begin();

  while (itp != (poly.end() - 1))

  {

  line(result, *itp, *(itp + 1), Scalar(255), 2);

  ++itp;

  }

  line(result, *itp, *(poly.begin()), Scalar(255), 2);

  // 轮廓表示为凸多边形

  vector《Point》 hull;

  convexHull(Mat(contours[3]), hull);

  vector《Point》::const_iterator ith = hull.begin();

  while (ith != (hull.end() - 1))

  {

  line(result, *ith, *(ith + 1), Scalar(255), 2);

  ++ith;

  }

  line(result, *ith, *(hull.begin()), Scalar(255), 2);

  程序中我们依次画了矩形、圆、多边形和凸多边形。最终效果如下:

  

  对连通区域的分析到此远远没有结束,我们可以进一步计算每一个连通区域的其他属性,比如:重心、中心矩等特征,这些内容以后有机会展开来写。

  以下几个函数可以尝试:minAreaRect:计算一个最小面积的外接矩形,contourArea可以计算轮廓内连通区域的面积;pointPolygenTest可以

  用来判断一个点是否在一个多边形内。mathShapes可以比较两个形状的相似性,相当有用的一个函数。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/dianzi/2717693.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-17
下一篇 2022-08-17

发表评论

登录后才能评论

评论列表(0条)

保存