OpenCV函数cvConvexHull2由点集序列或数组创建凸边形

本网站用的阿里云ECS,推荐大家用。自己搞个学习研究也不错

<

div id=”content”>有图有真相:

OpenCV函数cvConvexHull2由点集序列或数组创建凸边形

Objective-C中@property的所有属性详解 http://www.linuxidc.com/Linux/2014-03/97744.htm

Objective-C 和 Core Foundation 对象相互转换的内存管理总结 http://www.linuxidc.com/Linux/2014-03/97626.htm

使用 Objective-C 一年后我对它的看法 http://www.linuxidc.com/Linux/2013-12/94309.htm

10个Objective-C基础面试题,iOS面试必备 http://www.linuxidc.com/Linux/2013-07/87393.htm

Objective-C适用C数学函数 http://www.linuxidc.com/Linux/2013-06/86215.htm

以下是点集序列或数组创建凸多边形的代码

#include
#include
#include

#pragma comment(lib, “cv.lib”)
#pragma comment(lib, “cxcore.lib”)
#pragma comment(lib, “highgui.lib”)

#define ARRAY 0

int main()
{
 IplImage* img = cvCreateImage (cvSize(500, 500), 8, 3);
 cvNamedWindow (“hull”, 1);

#if ! ARRAY
 CvMemStorage* storage = cvCreateMemStorage (0);
#endif
 int i;
 int contour = rand() % 100 + 1;
 int hullcontour;
 CvPoint pt0;
#if ! ARRAY
 CvSeq* ptseq = cvCreateSeq (CV_SEQ_KIND_GENERIC | CV_32SC2, sizeof(CvContour), sizeof(CvPoint), storage);
 CvSeq* hull;
 for (i = 0; i < contour; i++)
 {
  pt0.x = rand() % (img->width / 2) + img->width / 4;
  pt0.y = rand() % (img->height / 2) + img->height / 4;
  cvSeqPush (ptseq, &pt0);
 }
 hull = cvConvexHull2 (ptseq, 0, CV_CLOCKWISE, 0);
 /hull = cvConvexHull2 (ptseq, 0, CV_CLOCKWISE, 1);/
 hullcontour = hull->total;
#else
 CvPoint* points = (CvPoint)malloc(contour * sizeof(points[0]));
 int
hull = (int)malloc(contour * sizeof(hull[0]));
 CvMat point_mat = cvMat (1, contour, CV_32SC2, points);
 CvMat hull_mat = cvMat (1, contour, CV_32SC1, hull);
 for (i = 0; i < contour; i++)
 {
  pt0.x = rand() % (img->width / 2) + img->width / 4;
  pt0.y = rand() % (img->height / 2) + img->height / 4;
  points[i] = pt0;
 }
 cvConvexHull2 (&point_mat, &hull_mat, CV_CLOCKWISE, 0);
 hullcontour = hull_mat.cols;
#endif
 cvZero (img);
 for (i = 0; i < contour; i++)
 {
#if ! ARRAY
  pt0 = * CV_GET_SEQ_ELEM(CvPoint, ptseq, i);
  /
pt0 = ** CV_GET_SEQ_ELEM(CvPoint*, ptseq, i);/
#else
  pt0 = points[i];
#endif
  cvCircle (img, pt0, 2, CV_RGB(255, 0, 0), CV_FILLED);
 }
#if ! ARRAY
 pt0 = ** CV_GET_SEQ_ELEM(CvPoint
, hull, hullcontour – 1);
 /pt0 = * CV_GET_SEQ_ELEM(CvPoint, hull, hullcontour – 1);/
#else
 pt0 = points[hull[hullcontour – 1]];
#endif

 for (i = 0; i < hullcontour; i++)
 {
#if ! ARRAY
  CvPoint pt = **CV_GET_SEQ_ELEM(CvPoint*, hull, i);
  /CvPoint pt = *CV_GET_SEQ_ELEM(CvPoint, hull, i);/
#else
  CvPoint pt = points[hull[i]];
#endif
  cvLine (img, pt0, pt, CV_RGB(255, 0, 0));
  pt0 = pt;
 }

 cvShowImage (“hull”, img);

 cvWaitKey (0);

#if ! ARRAY 
 cvClearMemStorage (storage);
#else
 free (points);
 free (hull);
#endif

 return 0;
}

更夼/div>

未经允许不得转载:演道网 » OpenCV函数cvConvexHull2由点集序列或数组创建凸边形

赞 (0)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册