|
发表于 2023-8-6 16:29:00
|
显示全部楼层
问题出在cv2.findContours()函数上。在OpenCV 3.x版本中,cv2.findContours()函数返回两个值:contours和hierarchy。但在OpenCV 4.x版本中,cv2.findContours()函数只返回一个值:contours。因此,在你的代码中,尝试将以下行:
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
改为:
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
这样就可以解决该错误。修改后的代码如下:
import cv2
# 读取二值化tiff图像
image = cv2.imread('F:\duneline\overlay_image.tif', cv2.IMREAD_GRAYSCALE)
# 提取边缘
edges = cv2.Canny(image, 50, 150, apertureSize=3)
# 寻找轮廓
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 找到最左侧的轮廓
leftmost_contour = min(contours, key=lambda contour: cv2.minEnclosingCircle(contour)[0][0])
# 绘制左侧边缘线
cv2.drawContours(image, [leftmost_contour], -1, (0, 255, 0), 2)
# 显示结果
cv2.imshow('Left Edge', image)
cv2.waitKey(0)
cv2.destroyAllWindows() |
|