python凹多边形分割_使用Opencv python从Image中裁剪凹面多边形

Steps find region using the poly points create mask using the poly points do mask op to crop add white bg if needed

编码:

# 2018.01.17 20:39:17 CST

# 2018.01.17 20:50:35 CST

import numpy as np

import cv2

img = cv2.imread("test.png")

pts = np.array([[10,150],[150,100],[300,150],[350,100],[310,20],[35,10]])

## (1) Crop the bounding rect

rect = cv2.boundingRect(pts)

x,y,w,h = rect

croped = img[y:y+h, x:x+w].copy()

## (2) make mask

pts = pts - pts.min(axis=0)

mask = np.zeros(croped.shape[:2], np.uint8)

cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)

## (3) do bit-op

dst = cv2.bitwise_and(croped, croped, mask=mask)

## (4) add the white background

bg = np.ones_like(croped, np.uint8)*255

cv2.bitwise_not(bg,bg, mask=mask)

dst2 = bg+ dst

cv2.imwrite("croped.png", croped)

cv2.imwrite("mask.png", mask)

cv2.imwrite("dst.png", dst)

cv2.imwrite("dst2.png", dst2)

来源图片:

kyBXv.png

结果:

Mg78c.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值