需要附加imutils
# -*- coding:utf-8 -*-
# import the necessary packages
from scipy.spatial import distance as dist
from imutils import perspective
from imutils import contours
import numpy as np
import imutils
import cv2
def midpoint(ptA, ptB): #定义中点
return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
image = cv2.imread('1111.jpg')
cv2.namedWindow("yuantu",0)
cv2.imshow("yuantu",image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)#二值化操作
cv2.namedWindow("erzhihua",0)
cv2.imshow("erzhihua",gray)
gray = cv2.GaussianBlur(gray, (7, 7), 0)#高斯滤波
cv2.namedWindow("guss",0)
cv2.imshow("guss",gray)
retval,result = cv2.threshold(gray,128,255,cv2.THRESH_BINARY)#阈值分割
cv2.namedWindow("yuzhi",0)
cv2.imshow("yuzhi",result)
# perform edge detection, then perform a dilation + erosion to
# close gaps in between object edges
edged = cv2.Canny(result, 50, 100)
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)
# find contours in t