为了账号安全,请及时绑定邮箱和手机立即绑定

使用 openCV-python 将两个轮廓分割成两个相同大小的不同图像

使用 openCV-python 将两个轮廓分割成两个相同大小的不同图像

慕田峪4524236 2021-09-11 14:51:45
我在第一张图像中有两个轮廓。我需要分割出单个轮廓并像这样制作两个图像:和。单个输出图像必须与输入图像具有相同的尺寸。如何使用 openCV-python 实现这一点?我绘制轮廓的代码:    image, contours, hier = cv2.findContours(im, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)    for c in contours:    rect = cv2.minAreaRect(c)    box = cv2.boxPoints(rect)    # convert all coordinates floating point values to int    box = np.int0(box)    # draw a red 'nghien' rectangle    cv2.drawContours(im, [box], 0, (0, 0, 255))    cv2.drawContours(im, contours, -1, (255, 255, 0), 1)
查看完整描述

1 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

您正在cv2.drawContours以错误的方式使用 。传递-1as 轮廓索引将绘制所有轮廓而不是单个轮廓。要绘制单个轮廓,您需要将相应的索引传递为:


_, cnt, hierarchy = cv2.findContours(canvas.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)


for i in xrange(len(cnt)):

    output_canvas = np.zeros(canvas.shape, dtype=np.uint8)

    cv2.drawContours(output_canvas, cnt, i, np.array([255, 255, 255, 255]), -1)

    cv2.imwrite("./contour{}.png".format(i), output_canvas)


查看完整回答
反对 回复 2021-09-11
  • 1 回答
  • 0 关注
  • 122 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信