矩可以求重心
import cv2
import numpy as np
import matplotlib.pyplot as plt
A = cv2.imread('E:/python/box.png')
imgray=cv2.cvtColor(A,cv2.COLOR_BGR2GRAY)
B=imgray.copy()
#ret,thresh=cv2.threshold(imgray,150,255,0)
th2 = cv2.adaptiveThreshold(imgray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
image,contours,hierarchy=cv2.findContours(th2,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt=contours[0]
M=cv2.moments(cnt)
print(M)
cx=int(M['m10']/M['m00'])
cy=int(M['m01']/M['m00'])
print(cx,cy)
求面积
M[‘m00’])或者
area=cv2.contourArea(cnt)
求周长
perimeter=cv2.arcLength(cnt,True)
print(‘周长’,perimeter)
其中True代表形状是闭合,第二个参数用来指定对象形状是闭合还是打开(曲线)
轮廓近似
epsilon=0.1cv2.arcLength(cnt,True)
approx=cv2.approxPolyDp(cnt,epsilon,True)
epsilon1=0.01cv2.arcLength(cnt,True)
approx1=cv2.approxPolyDP(cnt,epsilon,True)
plt.subplot(131),plt.imshow(B,‘gray’),plt.title(‘orignal’)
plt.subplot(132),plt.imshow(approx1,‘gray’),plt.title(‘0.01’)
plt.subplot(133),plt.imshow(approx,‘gray’),plt.title(‘0.1’)
凸包
hull=cv2.convexHull(cnt)
hull=cv2.convexHull(points,hull,clockwise,returnpoints)
第一个参数为传入轮廓,第二个通常不需要,第三个True则凸包方向为顺时针,第四个True会返回凸包点坐标,否则返回凸包点对应轮廓上点
hull=cv2.convexHull(cnt,hull,False,False)
凸性检测:检测曲线是不是凸的
k=cv2.isContourConvex(cnt)
print(k)
True
边界矩形:直边界矩形和旋转边界矩形
x,y,w,h=cv2.boundingRect(cnt)
img=cv2.rectangle(B,(x,y),(x+w,y+h),(0,255,0),2)直边界矩形没有考虑旋转,并不是最小的
旋转边界矩形:返回box2d结构,包含矩形左上角点x,y和宽高,以及旋转角度
rect=cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box_d = np.int0(box)
img2=cv2.drawContours(img, [box_d], 0, (255,0,0), 3)
#最小外接圆
(x,y),radius=cv2.minEnclosingCircle(cnt)
center=(int(x),int(y))
radius=int(radius)
img=cv2.circle(B,center,radius,(0,255,0),2)
#椭圆拟合
ellipse=cv2.fitEllipse(cnt)
im=cv2.ellipse(img,ellipse,(212,212,0),2)
plt.subplot(121),plt.imshow(img1,‘gray’)
#直线拟合
[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)
img4=cv2.line(img,(cols-1,righty),(0,lefty),(50,99,8),2)
import cv2
import numpy as np
import matplotlib.pyplot as plt
A = cv2.imread('E:/python/cd.png')
imgray=cv2.cvtColor(A,cv2.COLOR_BGR2GRAY)
B=imgray.copy()
ret,thresh=cv2.threshold(imgray,150,255,0)
#th2 = cv2.adaptiveThreshold(imgray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
image,contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnt=contours[0]
M=cv2.moments(cnt)
print(M)
cx=int(M['m10']/M['m00'])
cy=int(M['m01']/M['m00'])
print(cx,cy)
print('面积为',M['m00'])
area=cv2.contourArea(cnt)
print('面积为',area)
perimeter=cv2.arcLength(cnt,True)
print('周长',perimeter)
hull=cv2.convexHull(cnt,False)
rows, cols = B.shape
img= np.zeros((rows,cols,3),np.uint8)
#矩形拟合
x,y,w,h=cv2.boundingRect(cnt)
img1=cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
rect=cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box_d = np.int0(box)
img2=cv2.drawContours(img, [box_d], 0, (255,0,0), 3)
#最小外接圆
(x,y),radius=cv2.minEnclosingCircle(cnt)
center=(int(x),int(y))
radius=int(radius)
img3=cv2.circle(img,center,radius,(0,0,255),2)
#椭圆拟合
ellipse=cv2.fitEllipse(cnt)
im=cv2.ellipse(img,ellipse,(212,212,0),2)
plt.subplot(121),plt.imshow(img1,'gray')
#直线拟合
[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)
img4=cv2.line(img,(cols-1,righty),(0,lefty),(50,99,8),2)
cv2.imshow('img',img1)
k = cv2.waitKey(0)
if k == ord('s'):
cv2.destroyAllWindows()