largeQ
不要让它太复杂。我使用了你的一些代码。PS:我只能帮助您达到可以专注于尝试测量水位的程度。但我会在最后给你一个提示import numpy as npimport cv2def show(img): cv2.imshow('a',img) cv2.waitKey() cv2.destroyAllWindows()mask = cv2.imread("azC2r.jpg",0)img = cv2.imread("azC2r.jpg")print('Image shape: {}'.format(img.shape))ret,thresh = cv2.threshold(mask,50,255,cv2.THRESH_BINARY)thresh = cv2.blur(thresh,(7,7))thresh[thresh<254]=0kernel = np.ones((7,7))thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)kernel = np.ones((9,9))thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)im2, contours,_ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)contours = [cnt for cnt in contours if cv2.contourArea(cnt)<100000]cv2.drawContours(img, contours, -1, (0,255,0), 3)# Thank to this snippet to# https://stackoverflow.com/questions/37912928/fill-the-outside-of-contours-opencvstencil = np.zeros(img.shape).astype(img.dtype)color = [255, 255, 255]cv2.fillPoly(stencil, contours, color)img = cv2.bitwise_and(img, stencil)# END of snippetimg[np.where(np.all(img==[255,255,255],axis = 2))]=[0,0,0]show(img)结果我做了什么?:我会用图片来解释你。灰度阈值阈值模糊以填充线条所有不是纯白色的东西 [255,255,255] 都变成了 [0,0,0]用形态学技巧摆脱单独的小颗粒绘制特定尺寸的轮廓 - 去除太大和太小的轮廓最后用 cv2.polly 去掉countours 之外的所有东西并将其变成黑色。最先看到的图像的结果至于水位测量我真的不知道,我不想深入研究,但也许你可以玩 sobelysobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)