获得包围对象的凸包

// testing the convex hull
std::vector<cv::Point> hull;
cv::convexHull(cv::Mat(contours[3]),hull);
C++:  void  convexHull (InputArray  points, OutputArray  hull, bool  clockwise=false, bool  returnPoints=true  )
Python:   cv2. convexHull (points [, hull [, clockwise [, returnPoints ] ] ] ) → hull
C:  CvSeq*  cvConvexHull2 (const CvArr*  input, void*  hull_storage=NULL, int  orientation=CV_CLOCKWISE, int return_points=0  )
Parameters:
  • points – Input 2D point set, stored in std::vector or Mat.
  • hull – Output convex hull. It is either an integer vector of indices or vector of points. In the first case, the hull elements are 0-based indices of the convex hull points in the original array (since the set of convex hull points is a subset of the original point set). In the second case, hull elements are the convex hull points themselves.
  • hull_storage – Output memory storage in the old API (cvConvexHull2 returns a sequence containing the convex hull points or their indices).
  • clockwise – Orientation flag. If it is true, the output convex hull is oriented clockwise. Otherwise, it is oriented counter-clockwise. The assumed coordinate system has its X axis pointing to the right, and its Y axis pointing upwards.
  • orientation – Convex hull orientation parameter in the old API, CV_CLOCKWISE orCV_COUNTERCLOCKWISE.
  • returnPoints – Operation flag. In case of a matrix, when the flag is true, the function returns convex hull points. Otherwise, it returns indices of the convex hull points. When the output array isstd::vector, the flag is ignored, and the output depends on the type of the vector:std::vector<int> implies returnPoints=truestd::vector<Point> impliesreturnPoints=false.

The functions find the convex hull of a 2D point set using the Sklansky’s algorithm [Sklansky82] that has O(N logN)complexity in the current implementation. See the OpenCV sample convexhull.cpp that demonstrates the usage of different function variants.

Note

  • An example using the convexHull functionality can be found at opencv_source_code/samples/cpp/convexhull.cpp 
  • 边界片段
  • 边界包围的区域凸壳对边界的鲁棒分解是一有力的工具。

    convexityDefects

    Finds the convexity defects of a contour. 凸形缺陷

    C++:  void  convexityDefects (InputArray  contour, InputArray  convexhull, OutputArray  convexityDefects )
    Python:   cv2. convexityDefects (contour, convexhull [, convexityDefects ] ) → convexityDefects
    C:  CvSeq*  cvConvexityDefects (const CvArr*  contour, const CvArr*  convexhull, CvMemStorage*  storage=NULL  )
    Parameters:
    • contour – Input contour.
    • convexhull – Convex hull obtained using convexHull() that should contain indices of the contour points that make the hull.
    • convexityDefects – The output vector of convexity defects. In C++ and the new Python/Java interface each convexity defect is represented as 4-element integer vector (a.k.a. cv::Vec4i):(start_index, end_index, farthest_pt_index, fixpt_depth), where indices are 0-based indices in the original contour of the convexity defect beginning, end and the farthest point, andfixpt_depth is fixed-point approximation (with 8 fractional bits) of the distance between the farthest contour point and the hull. That is, to get the floating-point value of the depth will befixpt_depth/256.0. In C interface convexity defect is represented by CvConvexityDefect structure - see below.
    • storage – Container for the output sequence of convexity defects. If it is NULL, the contour or hull (in that order) storage is used.

    The function finds all convexity defects of the input contour and returns a sequence of the CvConvexityDefect structures, where CvConvexityDetect is defined as:

  • struct CvConvexityDefect
    {
       CvPoint* start; // point of the contour where the defect begins
       CvPoint* end; // point of the contour where the defect ends
       CvPoint* depth_point; // the farthest from the convex hull point within the defect
       float depth; // distance between the farthest point and the convex hull
    };

    1. // 轮廓表示为凸多边形 
    2. vector<Point> hull; 
    3. convexHull(Mat(contours[3]), hull); 
    4. vector<Point>::const_iterator ith = hull.begin(); 
    5. while (ith != (hull.end() - 1)) 
    6.     line(result, *ith, *(ith + 1), Scalar(255), 2); 
    7.     ++ith; 
    8. line(result, *ith, *(hull.begin()), Scalar(255), 2); 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值