去除轮廓区域外的图片

问题描述

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

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,0),3)
cv2.drawContours(imgray,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()

enter image description here

有没有办法只得到轮廓里面的皮肤,希望能帮到你。提前致谢。

解决方法

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

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