1.读取图片 imge = cv2.imread(IMAGE_PATH) 2.转灰度图 gray = cv2.cvtColor(imge, cv2.COLOR_BGR2GRAY) 3.图像缩放 ?image = cv2.resize(image, (400, int(400 * image.shape[0] / image.shape[1]))) 4.高期模糊 gray = cv2.GaussianBlur(gray, (5, 5), 0) 5.局部自适应阀值二值化 binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 101, 0) 6.轮廓检测 contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 7.轮廓边长 arc_length = cv2.arcLength(contour, True) 8.轮廓最小包围框 rect = cv2.minAreaRect(contour) 9.轮廓面积 ?area = cv2.contourArea(contour) 10.在图上画轮廓 cv2.drawContours(img, good_contours, -1, (0, 0, 255), 4) 11.角点检测 corners = cv2.goodFeaturesToTrack(gray, 10000, 0.05, 10, blockSize=5, useHarrisDetector=False, k=0.04) 12.角点亚像素处理 criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 40, 0.001) cv2.cornerSubPix(gray, corners, (5, 5), (-1, 1), criteria) 13.在图上画角点 for epoch in range(len(corners)): ??? cv2.circle(img, (corners[epoch][0][0], corners[epoch][0][1]), 3, (0, 255, 255), -1) 14.边缘检测 image = cv2.GaussianBlur(image, (5, 5), 0) edges = cv2.Canny(image, 100, 200)
|