[英]how to change the color of the resulting foreground to white color after grabcut algorithm?
这是抓取算法的以下代码。 我想用白色代替原来的颜色得到结果。 这是从链接的以下代码获得的输出。
import numpy as np
import cv2
from matplotlib
import pyplot as plt
img = cv2.imread('messi5.jpg')
mask = np.zeros(img.shape[:2],np.uint8)
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
rect = (50,50,450,290)
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
plt.imshow(img),plt.colorbar(),plt.show()
在此行cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
,您将获得具有4个不同值的单个通道掩码,即:
- GC_FGD = 0
- GC_BGD = 1
- GC_PR_FGD = 2
- GC_PR_BGD = 3。
要从该掩码中获取二进制掩码,您需要使用值255
替换GC_FGD = 0
和GC_PR_FGD = 2
,可以通过以下方式完成:
mask2 = np.where((mask==2)|(mask==0), 0, 255).astype('uint8')
跳过乘法步骤,您将二进制掩码存储在mask2中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.