最佳答案
你只需要调整灰度图像的阈值以包括灰色虚线路径,同时找到轮廓,由于输入图像的主要部分是白色所以我们可以选择接近255的阈值,比如230。然后找到轮廓阈值。
您可以使用 cv2.approxPolyDP
来计算多项式的近似形状,但这不会有太大帮助,因此该步骤是可选的。
代码片段可能如下所示:
import cv2
img = cv2.imread("/Users/anmoluppal/Downloads/1tl6D.jpg")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 230, 255, cv2.THRESH_BINARY_INV)
img_, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
largest_contour_area = 0
for cnt in contours:
if (cv2.contourArea(cnt) > largest_contour_area):
largest_contour_area = cv2.contourArea(cnt)
largest_contour = cnt
epsilon = 0.001*cv2.arcLength(largest_contour,True)
approx = cv2.approxPolyDP(largest_contour,epsilon,True)
final = cv2.drawContours(img, [approx], 0, [0, 255, 0])
关于opencv - 如何在 python 中获取平面图的外部轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38366439/