AttributeError: 'tuple' object has no attribute 'sort'

报错

  • 源码
contours, hierarchy = cv2.findContours(img_handled, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# 据面积排序
contours.sort(key=lambda c: abs(cv2.contourArea(c)), reverse=True)

因为contours是元组没有sort属性
所以更改contours为列表

  • 修改
 contours, hierarchy = cv2.findContours(img_handled, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    # 据面积排序
 contours = list(contours)         # 添加!!!
 contours.sort(key=lambda c: abs(cv2.contourArea(c)), reverse=True)
posted @ 2022-10-08 12:10  衣囧~  阅读(1471)  评论(0编辑  收藏  举报