我对如何过滤出轮廓坐标(在元组中)并仅保留轮廓部分与该图像的顶部分割层相对应感到困惑。
我的代码是这个
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(img, (1, 1), 1000)
flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
thresh = cv2.dilate(thresh, None)
thresh = cv2.dilate(thresh, None)
thresh = cv2.erode(thresh, None)
thresh = cv2.erode(thresh, None)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
perimeters = [cv2.arcLength(contours[i], True) for i in range(len(contours))]
listindex = [i for i in range(1) if perimeters[i] > perimeters[0] / 2]
num_of_layers = len(listindex)
imgcont = img.copy()
[cv2.drawContours(imgcont, [contours[i]], 0, (0, 255, 0), 5) for i in listindex]
M = cv2.moments(contours[0])
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
print(cx, cy)
plt.imshow(imgcont, cmap='gray')