《cv中文参考手册-图像轮廓处理-结构分析与形状识别-外接轮廓》

  • Post author:
  • Post category:其他





minEnclosingTriangle



Finds a triangle of minimum area enclosinga 2D point set and returns its area.



C++:

double

minEnclosingTriangle

(InputArray

points

,OutputArray

triangle

)




Python:

cv2.

minEnclosingTriangle

(points[, triangle]) → retval,triangle



Parameters:


    • points


Input vector of 2D points with depth CV_32S or CV_32F, stored in:

      • std::vector<> or Mat (C++ interface)
      • Nx2 numpy array (Python interface)

    • triangle

      – Output vector of three 2D points defining the vertices of the triangle. The depth of the OutputArray must be CV_32F.


The function finds a triangle of minimumarea enclosing the given set of 2D points and returns its area. The output fora given 2D point set is shown in the image below. 2D points are depictedin

red

and the enclosing triangle in

yellow

.




minEnclosingCircle



对给定的

2D

点集,寻找最小面积的包围圆形


int cvMinEnclosingCircle( const CvArr*points, CvPoint2D32f* center, float* radius );



points

点序列或点集数组



center

输出参数:圆心



radius

输出参数:半径


函数

cvMinEnclosingCircle

对给定的

2D

点集迭代寻找最小面积的包围圆形。如果产生的圆包含所有点,返回非零。否则返回零(算法失败)。



Finds a circle of the minimum areaenclosing a 2D point set.C++: void minEnclosingCircle(InputArray points,Point2f& center, float& radius)Python: cv2.minEnclosingCircle(points) →center, radiusC: int cvMinEnclosingCircle(const CvArr* points, CvPoint2D32f*center, float* radius)Parameters: points –Input vector of 2D points, storedin:std::vector<> or Mat (C++ interface)CvSeq* or CvMat* (C interface)Nx2numpy array (Python interface)center – Output center of the circle.radius –Output radius of the circle.The function finds the minimal enclosing circle ofa 2D point set using an iterative algorithm. See the OpenCV sample minarea.cpp.


    1. // 轮廓表示为一个圆


    2. float

      radius;

    3. Point2f center;

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

    5. circle(result, Point(center),

      static_cast

      <

      int

      >(radius), Scalar(255), 2);






    6. 第一个参数points是所要求最小外结圆的点集数组或向量;


第二个参数

Point2f

类型的

center

是求得的最小外接圆的中心坐标;


第三个参数

float

类型的

radius

是求得的最小外接圆的半径;





对给定的

2D

点集,寻找最小面积的包围矩形



CvBox2D cvMinAreaRect2( const CvArr* points, CvMemStorage* storage=NULL );



points


点序列或点集数组



storage


可选的临时存储仓


函数

cvMinAreaRect2

通过建立凸外形并且旋转外形以寻找给定

2D

点集的最小面积的包围矩形

.



Picture. Minimal-area bounding rectanglefor contour



fitEllipse

拟合圆



fitLine

拟合直线


1.0


CvSeq *GetAreaMaxContour(CvSeq *contour)


{//

在给定的

contour

中找到面积最大的一个轮廓,并返回指向该轮廓的指针


doublecontour_area_temp=0,contour_area_max=0;


CvSeq* area_max_contour = 0 ;//

指向面积最大的轮廓


CvSeq*c=0;


//printf(“Total Contours Detected: %d/n”, Nc );


for(c=contour;c!=NULL; c=c->h_next )


{//

寻找面积最大的轮廓,即循环结束时的

area_max_contour


contour_area_temp= fabs(cvContourArea( c, CV_WHOLE_SEQ )); //

获取当前轮廓面积


if(contour_area_temp > contour_area_max )


{


contour_area_max= contour_area_temp; //

找到面积最大的轮廓


area_max_contour= c;//

记录面积最大的轮廓


}


}


returnarea_max_contour;


}


2.0



函数计算并返回指定二维点集的最小区域包围矩形(可能是旋转的)。看样

minarea.cpp OpenCV

。开发者应该记住返回的

rotatedrect

可以包含负索引数据时关闭包含垫单元边界。



C++:

RotatedRect

minAreaRect

(InputArray

points

)




Python:

cv2.

minAreaRect

(points) → retval




C:

CvBox2D

cvMinAreaRect2

(constCvArr*

points

, CvMemStorage*

storage

=NULL )



Parameters:


    • points


Input vector of 2D points, stored in:

      • std::vector<> or Mat (C++ interface)
      • CvSeq* or CvMat* (C interface)


源代码实现原理:


contourArea

轮廓面积


Calculates a contour area.



C++:

double

contourArea

(InputArray

contour

,bool

oriented

=false )




Python:

cv2.

contourArea

(contour[, oriented]) → retval




C:

double

cvContourArea

(constCvArr*

contour

, CvSlice

slice

=CV_WHOLE_SEQ, int

oriented

=0 )



Parameters:


    • contour

      – Input vector of 2D points (contour vertices), stored in std::vector or Mat.

    • oriented

      – Oriented area flag. If it is true, the function returns a signed area value, depending on the contour orientation (clockwise or counter-clockwise). Using this feature you can determine orientation of a contour by taking the sign of an area. By default, the parameter is false, which means that the absolute value is returned.


The function computes a contour area.Similarly to



moments()



,the area is computed using the Green formula. Thus, the returned area and thenumber of non-zero pixels, if you draw the contour using



drawContours()



or



fillPoly()



,can be different. Also, the function will most certainly give a wrong resultsfor contours with self-intersections.


Example:


vector<Point> contour;


contour.push_back(Point2f(0, 0));


contour.push_back(Point2f(10, 0));


contour.push_back(Point2f(10, 10));


contour.push_back(Point2f(5, 4));



double area0 = contourArea(contour);


vector<Point> approx;


approxPolyDP(contour, approx, 5, true);


double area1 = contourArea(approx);


cout << “area0 =” << area0<< endl <<


“area1 =” << area1 << endl <<


“approx poly vertices” << approx.size() << endl;


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


以下几个函数可以尝试:

minAreaRect

:计算一个最小面积的外接矩形,

contourArea

可以计算轮廓内连通区域的面积;

pointPolygenTest

可以用来判断一个点是否在一个多边形内。

mathShapes

可以比较两个形状的相似性,相当有用的一个函数。