效果不是特别好import cv2 import numpy as np img cv2.imread(ellipse_paper.jpg) gray cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blur cv2.GaussianBlur(gray, (5,5), 1.5) edges cv2.Canny(blur, 50, 150) contours, _ cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: if len(cnt) 10: continue # 拟合椭圆: 返回 ((cx,cy), (major,minor), angle) ellipse cv2.fitEllipse(cnt) (cx, cy), (w, h), angle ellipse a w / 2.0 b h / 2.0 theta np.radians(angle) print(f中心({cx:.2f},{cy:.2f}),长半轴{a:.2f},短半轴{b:.2f},旋转角{angle:.2f}°) cv2.ellipse(img, ellipse, (0,255,0), 2) cv2.imwrite(result.png, img) cv2.imshow(fit, img) cv2.waitKey(0) cv2.destroyAllWindows()