opencv使用绘制轮廓的功能提取图片前景

import cv2
import numpy as np


ori = cv2.imread('../data/hua.png', 1)
cv2.imshow('ori', ori)
img = cv2.cvtColor(ori, cv2.COLOR_BGR2GRAY)
cv2.imshow('img', img)
ret, binary = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

cv2.imshow('binary', binary)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

mask = np.zeros(ori.shape, np.uint8)
mask = cv2.drawContours(mask, contours, -1, (255, 255, 255), -1)
cv2.imshow('mask', mask)
loc = cv2.bitwise_and(ori, mask)
cv2.imshow('loc', loc)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imwrite('../data/mask.jpg', mask)
cv2.imwrite('../data/loc.jpg', loc)

注意点:
1-绘制轮廓必须是二值图像,且白色为前景色
原图
在这里插入图片描述
mask
在这里插入图片描述
loc
在这里插入图片描述

在不是高精度的抠图的使用上,这个方法还是有特定的抠图功能的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值