案例来源于© Fu Xianjun. All Rights Reserved
目标:
***— 查找轮廓
cv2.RETR_EXTERNAL 只检测外轮廓
cv2.RETR_LIST检测的轮廓不建立等级关系
cv2.RETR_CCOMP建立两个等级的轮廓
cv2.RETR_TREE建立一个等级树结构的轮廓
method:
cv2.CHAIN_APPROX_NONE存储所有的轮廓点
cv2.CHAIN_APPROX_SIMPLE压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息。
img = cv2.imread("shape.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#-----------#阈值处理
ret,binary = cv2.threshold(gray,200,255,0)
#-----------#查找轮廓
contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,\
cv2.CHAIN_APPROX_SIMPLE)
img = cv2.drawContours(img,contours,-1,(255,255,0),5)
#计算图像的矩特征
retval = cv2.moments(contours[1])
print(retval["m00"])
area = cv2.contourArea(contours[1])
print(area)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()
1.显示面积小于20000的轮廓
img = cv2.imread("shape.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#-----------#阈值处理
ret,binary = cv2.threshold(gray,200,255,0)
#-----------#查找轮廓
contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,\
cv2.CHAIN_APPROX_SIMPLE)
for i in range(len(contours)):
area = cv2.contourArea(contours[i])
if area<20000:
img = cv2.drawContours(img,contours,i,(255,255,0),5)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()
2.显示长度小于600的轮廓
img = cv2.imread("shape.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#-----------#阈值处理
ret,binary = cv2.threshold(gray,200,255,0)
#-----------#查找轮廓
contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,\
cv2.CHAIN_APPROX_SIMPLE)
for i in range(len(contours)):
length = cv2.arcLength(contours[i],True)
print(length)
if length<600:
img = cv2.drawContours(img,contours,i,(255,255,0),5)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()
二.使用Hu特征进行形状匹配
img1 = cv2.imread("1.png")
gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
ret,binary1 = cv2.threshold(gray1,200,255,0)
contours1,hierarchy = cv2.findContours(binary1,cv2.RETR_LIST,\
cv2.CHAIN_APPROX_SIMPLE)
img2 = cv2.imread("2.png")
gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret,binary2 = cv2.threshold(gray2,200,255,0)
contours2,hierarchy = cv2.findContours(binary2,cv2.RETR_LIST,\
cv2.CHAIN_APPROX_SIMPLE)
img3 = cv2.imread("3.png")
gray3 = cv2.cvtColor(img3,cv2.COLOR_BGR2GRAY)
ret,binary3 = cv2.threshold(gray3,200,255,0)
contours3,hierarchy = cv2.findContours(binary3,cv2.RETR_LIST,\
cv2.CHAIN_APPROX_SIMPLE)
retval1 = cv2.matchShapes(contours1[0],contours2[0],1,0.0)
retval2 = cv2.matchShapes(contours1[0],contours3[0],1,0.0)
print(retval1,retval2)
三.轮廓拟合
1.矩形包围框
img = cv2.imread("shape.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#-----------#阈值处理
ret,binary = cv2.threshold(gray,200,255,0)
#-----------#查找轮廓
contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,\
cv2.CHAIN_APPROX_SIMPLE)
x,y,w,h = cv2.boundingRect(contours[0])
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),1)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()
2.最小包围矩形框
img = cv2.imread("shape.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#-----------#阈值处理
ret,binary = cv2.threshold(gray,200,255,0)
#-----------#查找轮廓
contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,\
cv2.CHAIN_APPROX_SIMPLE)
rect = cv2.minAreaRect(contours[1])
print(rect)
points = cv2.boxPoints(rect)
print(rect)
points = np.int64(points)
img = cv2.drawContours(img,[points],0,(0,0,0),2)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()
3.最小包围圆形
img1 = cv2.imread("shape.jpg")
cv2.imshow("o",img1)
gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
ret,binary1 = cv2.threshold(gray1,200,255,0)
contours1,hierarchy = cv2.findContours(binary1,cv2.RETR_LIST,\
cv2.CHAIN_APPROX_SIMPLE)
(x,y),r = cv2.minEnclosingCircle(contours[1])
center = (int(x),int(y))
r = int(r)
cv2.circle(img1,center,r,(0,0,0),2)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()
4.最优拟合椭圆
import cv2
img = cv2.imread("shape.jpg")
cv2.imshow("o",img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,binary = cv2.threshold(gray,127,255,0)
contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,\
cv2.CHAIN_APPROX_SIMPLE)
ellipse = cv2.fitEllipse(contours[1])
print("ellipse=",ellipse)
cv2.ellipse(img,ellipse,(0,255,0),3)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()
5.最优拟合直线
import cv2
img = cv2.imread("shape.jpg")
cv2.imshow("o",img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,binary = cv2.threshold(gray,127,255,0)
contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,\
cv2.CHAIN_APPROX_SIMPLE)
r,c = img.shape[:2]
[vx,vy,x,y] = cv2.fitLine(contours[1],cv2.DIST_L2,0,0.01,0.01)
lefty = int((-x*vy/vx) + y)
righty = int(((c-x)*vy/vx) + y)
cv2.line(img,(c-1,righty),(0,lefty),(0,255,0),2)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()