首页 > 解决方案 > 去除轮廓区域外的图片

问题描述

我正在尝试删除没有轮廓的图像。关于如何实现它们有什么建议吗?下面是我的轮廓代码

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()

在此处输入图像描述

有没有办法只得到轮廓内的皮肤,希望你能帮忙。先感谢您。

标签: pythonjupytercontourroi

解决方案


尝试将cv2.drawContours()函数的最后一个参数更改为-1

cv2.drawContours(im, contours, -1, (0, 255, 0), -1)
cv2.drawContours(imgray, contours, -1, (0, 255, 0), -1)

推荐阅读