python - 去除轮廓区域外的图片
问题描述
我正在尝试删除没有轮廓的图像。关于如何实现它们有什么建议吗?下面是我的轮廓代码
import numpy as np
import cv2
im = cv2.imread('after.png')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
largest_area = sorted(contours, key=cv2.contourArea)[-1]
mask = np.zeros(im.shape, np.uint8)
cv2.drawContours(im, contours, -1, (0, 255, 0), 3)
cv2.drawContours(imgray, contours, -1, (0, 255, 0), 3)
image_remainder = cv2.bitwise_and(im, 255 - mask)
#cv2.imshow('Image', im)
cv2.imshow('Image GRAY', imgray)
cv2.imshow('extract', image_remainder)
#cv2.imwrite("remainder.png", image_remainder)
cv2.waitKey(0)
cv2.destroyAllWindows()
有没有办法只得到轮廓内的皮肤,希望你能帮忙。先感谢您。
解决方案
尝试将cv2.drawContours()
函数的最后一个参数更改为-1
cv2.drawContours(im, contours, -1, (0, 255, 0), -1)
cv2.drawContours(imgray, contours, -1, (0, 255, 0), -1)
推荐阅读
- python - 尝试在没有已知父包的情况下进行相对导入 - 无法从并行字典导入 python 文件
- rust - 如果按照 Clippy 的建议使用 && 折叠嵌套会改变行为吗?
- javascript - 按开始和结束时间计算持续时间
- r - 如何生成具有不同方差(异方差)的随机数据。在 R 中
- javascript - 使用 window.open() 函数打开已打开的选项卡而不重新加载已打开的选项卡
- javascript - 从 Devtools 处理组件删除
- javascript - 如何使用 Puppeteer 处理弹出窗口
- java - 在我的数组中找到中位数,同时将数字从小到大排序
- angularjs - 为什么 dateAdapter.setLocale 默认为某些语言?
- javascript - 如何使用 Javascript 将样式表应用于元素