OpenCV之特征轮廓(Contours Features)相关函数的使用(2)

 

为了发现不同的特征轮廓,比如面积,周长,质心,边界箱,OpenCV提供了很多相关的函数。

1、Moments(图片矩)

函数:cv2.moments()

理解:图片的矩能能帮我们获取很有用的数据,比如通过图片的矩可以计算物体的质心、物体的面积等。

参数:

array         Raster image (single-channel, 8-bit or floating-point 2D array) or an array ( 1×N or N×1 ) of 2D points (Point or Point2f ).

binaryImage     If it is true, all non-zero image pixels are treated as 1's. The parameter is used for images only.

import cv2
import numpy as np
img = cv2.imread('../../../image/star.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
M = cv2.moments(cnt)
print( M )

输出结果:

{'mu02': 583938186.1642084, 'mu03': -99129134.18457031, 'm11': 8724755208.583332, 'nu02': 0.10258205922746799, 'm12': 2905995556564.0835, 'mu21': 105554896.9121399, 'mu20': 647182089.9830132, 'nu20': 0.11369229322318157, 'm30': 4724565376122.75, 'nu21': 6.750859295602893e-05, 'mu11': 49011.16095542908, 'mu12': 13629319.905517578, 'nu11': 8.609928131199437e-06, 'nu12': 8.716755325287694e-06, 'm02': 7738351999.666666, 'm03': 2742466249223.5, 'm00': 75448.0, 'm01': 23233299.666666664, 'mu30': -1971980.228515625, 'nu30': -1.2611978644155867e-06, 'nu03': -6.339893804566895e-05, 'm10': 28332679.5, 'm20': 11286837583.166666, 'm21': 3475787575543.4165}

 

2、Contour Area(轮廓面积)

函数:cv2.contourArea()

理解:通过该函数计算轮廓的面积,也可以通过图像的矩中的M[‘m00’]获取。

参数:

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.

area = cv2.contourArea(cnt)
      print(area)   // 75448.0

 

3、Contour Perimeter(轮廓的周长)

函数:cv2.arcLength()

理解:轮廓的周长也称为弧长,第二个参数如果为true,表示封闭的轮廓,否则只是一个曲线。

参数:

curve       Input vector of 2D points

closed      Flag indicating whether the curve is closed or not.

perimeter = cv2.arcLength(cnt,False)
      print (perimeter)  //1986.52306557

perimeter1 = cv2.arcLength(cnt,True)
       print (perimeter1)  //1987.52306557

 

4、Contour Approximation(轮廓的近视值)

函数:cv2.approxPolyDP(cnt,epsilon,True)

理解:通过用户指定的精度,用最少的顶点数来近视轮廓的形状。第二个参数表示,从轮廓到近视轮廓的最大距离,这是一个精确度的参数,需要用户基于经验进行选择。第三个参数表示轮廓是否封闭,true表示封闭。该函数的核心是Ramer-Douglas-Peucker算法。下图可以通俗易懂地理解该函数的含义,其中第二幅图第二个参数为弧长的10%,第三幅图的第二参数为弧长的1%。

epsilon = 0.1*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
print ("*******")
print (approx)
print ("*******")

 

5、Contour Hull(轮廓的凸壳)

函数:cv2.convexHull()

参数:hull = cv2.convexHull(points[, hull[, clockwise[, returnPoints]]

  • points are the contours we pass into.
  • hull is the output, normally we avoid it.
  • clockwise : Orientation flag. If it is True, the output convex hull is oriented clockwise. Otherwise, it is oriented counter-clockwise.
  • returnPoints : By default, True. Then it returns the coordinates of the hull points. If False, it returns the indices of contour points corresponding to the hull points.

      理解:凸壳就是凸起来的凸曲线,如果它是内部凸起来的,我们的专业术语叫它凸性缺陷,比如下面的一个手型图,其中最外面的红颜色轮廓就是凸壳,其中里面的双箭头就是凸缺陷,换句话说,就是凸壳与轮廓的局部最大偏差。

6、Checking Convexity(检查凸性)

函数:cv2.isContourConvex()

理解:是返回true,否则返回false

参数:

contour   Input vector of 2D points

k = cv2.isContourConvex(cnt)
      print (k)

 

7、Straight Bounding Rectangle(直边矩形边框)

函数:cv2.boundingRect()

理解:该函数由于不考虑旋转,所以面积一般不是最少的。

参数:

points      Input 2D point set

x,y,w,h = cv2.boundingRect(cnt)

cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

        注解:其中(x,y)是图像的左上角坐标,w,h分别是宽和高。

 

8、Rotated Rectangle(旋转矩形边框)

函数:cv2.minAreaRect()

理解:为了获取矩形边框的最少值,可以使用该函数,该函数返回(x,y,width,height,旋转角度)等参数。但是要获取矩形的四个角,必须的用下面函数boxPoints进行返回。

参数:

points      Input vector of 2D points

 

函数:cv2.boxPoints()

理解:见上面函数

参数:

         box  The input rotated rectangle

         points      The output array of four vertices of rectangles.

rect = cv2.minAreaRect(cnt)

box = cv2.boxPoints(rect)

box = np.int0(box)

cv2.drawContours(img,[box],0,(0,0,255),2)

      解析:比如上面这张图片,其中绿颜色的是直线矩形边框、红颜色的是旋转矩形边框。

 

9、Minimun Enclosing Circle(最少封闭圆)

函数:cv2.minEnclosingCircle()

理解:包含物体最少面积的外接圆。

参数:

points Input vector of 2D points, stored in std::vector<> or Mat

center Output center of the circle.

radius  Output radius of the circle.

(x,y),radius = cv2.minEnclosingCircle(cnt)

center = (int(x),int(y))

radius = int(radius)

cv2.circle(img,center,radius,(0,255,0),2)

10、Fitting an Ellipse(拟合椭圆)

ellipse = cv2.fitEllipse(cnt)

cv2.ellipse(img,ellipse,(0,255,0),2)

11、Fitting a Line(拟合直线)

rows,cols = img.shape[:2]

[vx,vy,x,y] = cv2.fitLine(cnt, cv2.DIST_L2,0,0.01,0.01)

lefty = int((-x*vy/vx) + y)

righty = int(((cols-x)*vy/vx)+y)

cv2.line(img,(cols-1,righty),(0,lefty),(0,255,0),2)

12、Reference Material

资料1(moments):

http://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#ga556a180f43cab22649c23ada36a8a139

资料2(contourArea):

http://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#ga2c759ed9f497d4a618048a2f56dc97f1

资料3(arcLength):

http://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#ga8d26483c636be6b35c3ec6335798a47c

资料4(Ramer-Douglas-Peucker algorithm):

http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm

资料5(convexHull):

http://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#ga014b28e56cb8854c0de4a211cb2be656

资料6(isContourConvex):

http://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#ga8abf8010377b58cbc16db6734d92941b

资料7(boundingRect):

http://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#gacb413ddce8e48ff3ca61ed7cf626a366

资料8(minAreaRect):

http://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#ga3d476a3417130ae5154aea421ca7ead9

资料9(boxPoints):

http://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#gaf78d467e024b4d7936cf9397185d2f5c

资料10(minEnclosingCircle):

http://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#ga8ce13c24081bbc7151e9326f412190f1

资料11:

http://docs.opencv.org/3.3.0/dd/d49/tutorial_py_contour_features.html

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: