1.图像的矩
cv2.moments()
图像的矩可以帮助计算物体的某些特征,如对象的质心,对象的区域等.
import cv2
import numpy as np
img = cv2.imread('img7.png',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 )
输出:{'m00': 283.0, 'm10': 8260.666666666666, 'm01': 34747.666666666664, 'm20': 251349.8333333333, 'm11': 1008063.0, 'm02': 4274513.166666666, 'm30': 7941981.4, 'm21': 30484543.9, 'm12': 123258620.46666667, 'm03': 526819846.70000005, 'mu20': 10223.989595602674, 'mu11': -6208.702394974302, 'mu02': 8080.874165684916, 'mu30': 8302.495426246896, 'mu21': -14552.154961312423, 'mu12': 11791.528133469663, 'mu03': -3268.923251092434, 'nu20': 0.12765785058625623, 'nu11': -0.07752253611575, 'nu02': 0.10089867729257346, 'nu30': 0.006162296011483629, 'nu21': -0.010800931752771139, 'nu12': 0.008751933371317017, 'nu03': -0.0024262672459139235}
此刻,可以提取有用的数据,如面积,质心等.
质心由关系给出:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
2.轮廓面积
cv2.contourArea(contour[, oriented])
3.轮廓周长
cv2.arcLength(curve, closed)
第二个参数指定形状是否为闭合轮廓
4.轮廓近似
它根据我们指定的精度将轮廓形状近似为具有较少顶点数的另一个形状.它是Douglas-Peucker算法的一种实现方式.
cv2.approxPolyDP(curve, epsilon, closed[, approxCurve])
第二个参数epsilon,它是从轮廓到近似轮廓的最大距离.第三个参数指定曲线是否闭合.
下面,在第二幅图像中,绿线表示epsilon =弧长的10%的近似曲线. 第三幅图像显示相同的epsilon =弧长的1%.
import cv2
import numpy as np
img = cv2.imread('img8.png')
cv2.imshow('src',img)
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[1]
epsilon = 0.1*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
cv2.polylines(img, [approx], True, (0, 0, 255), 2)
cv2.imshow('show',img)
cv2.waitKey()
5.凸包
凸包看起来类似轮廓近似,但是它不是(两者在某些情况下可能提供相同的结果).
convexHull(points[, hull[, clockwise[, returnPoints]]])
:检查曲线的凸性缺陷并进行修正.
- points:传入的轮廓
- hull:输出
- clockwise:方向标志,如果为True,则顺时针方向输出凸包.
- returnPoints:默认情况下为True,然后它返回hull points的坐标; 如果为False,则返回与hull points对应的轮廓点的索引
下面的手形图像. 红线表示手的凸包, 双面箭头标记显示凸起缺陷.
import cv2
import numpy as np
img = cv2.imread('img8.png')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[1]
hull = cv2.convexHull(cnt)
returnPoints = True,得到以下值:
array([[[192, 135]],
[[ 9, 135]],
[[ 9, 12]],
[[192, 12]]], dtype=int32)
如果想找到凸性缺陷,需要传递returnPoints = False,得到以下结果:
array([[129],
[ 67],
[ 0],
[142]], dtype=int32)
6.检查凸性
cv2.isContourConvex(contour)
:检查曲线是否凸起
7.外接矩形
7.1 直边外接矩形
它是一个直的矩形,它不考虑对象的旋转。因此,边界矩形的面积不会最小.
cv.boundingRect()
设(x,y)为矩形的左上角坐标,(w,h)为宽度和高度
import cv2
import numpy as np
img = cv2.imread('img7.png')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow('show',img)
cv2.waitKey()
7.2 最小外接矩形
cv.minAreaRect
返回一个Box2D结构,其中包含以下detals - (center(x,y),(width,height),rotation of rotation)
cv.boxPoints
画上述矩形.
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img,[box],0,(0,0,255),2)
8 最小封闭圈
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
cv2.circle(img,center,radius,(0,255,0),2)
9 拟合椭圆
ellipse = cv2.fitEllipse(cnt)
cv2.ellipse(img,ellipse,(0,255,0),2)
10 拟合直线
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)