opencv-python找凸多边形

def get_new_cont(cnt):
    hull = cv2.convexHull(cnt,returnPoints = False)
    defects = cv2.convexityDefects(cnt,hull)
    if np.any(defects)==None:
        return np.array(cnt,dtype=np.int32)
    new_cont=[cnt[defects[0,0][0]][0], cnt[defects[0,0][1]][0]]
    for i in range(defects.shape[0]):
        s,e,f,d = defects[i,0]
        start = cnt[s][0]
        end = cnt[e][0]
        far = cnt[f][0]
        if i!=0:
            if s!=defects[i-1,0][1]:
                new_cont.append(start)
                new_cont.append(end)
            else:
                new_cont.append(end)
    return np.array(new_cont)
img = cv2.imread(maskpath)
img_gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
_,thresh_img = cv2.threshold(img_gray,245,255,cv2.THRESH_BINARY)
contours,_= cv2.findContours(thresh_img,2,1)
for cnt in contours:
    new_cont = get_new_cont(cnt)
    pts = new_cont.reshape((-1, 1, 2))
    img = cv2.polylines(img,pts=[pts], isClosed=True, color=(0, 0, 200), thickness=2)

cv2.imwrite(maskpath.replace("mask",savepath),img)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值