[英]Mask Values on Foreground Extraction
I am working on this code to perform foreground extraction but I don't understand the meaning of mask2 = np.where((mask==2 | (mask == 0), 0, 1).astype('uint8'))
and img = img*mask2[:, :, np.newaxis]
lines. 我正在使用此代码执行前景提取,但我不了解
mask2 = np.where((mask==2 | (mask == 0), 0, 1).astype('uint8'))
和img = img*mask2[:, :, np.newaxis]
行。 Here is the code 这是代码
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('tut12img.jpg')
mask = np.zeros(img.shape[:2], np.uint8)
bgdModel = np.zeros((1,65), np.float64)
fgdModel = np.zeros((1,65), np.float64)
rect = (161, 79, 150, 150)
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()
Why musk is zero when it is 2 or 0? 为什么麝香为2或0时为零? What is special about it?
有什么特别之处? what kind of matrix is musk2?
musk2是哪种矩阵? Thanks!
谢谢!
code and photo comes from https://pythonprogramming.net/grabcut-foreground-extraction-python-opencv-tutorial/?completed=/template-matching-python-opencv-tutorial/ 代码和照片来自https://pythonprogramming.net/grabcut-foreground-extraction-python-opencv-tutorial/?completed=/template-matching-python-opencv-tutorial/
mask2 is the foreground mask. mask2是前景蒙版。 so you only use foreground pixels which is done by setting every pixel 0 that is background, else 1
因此您只使用前景像素,方法是将每个像素设置为背景,即设置为0,否则设置为1
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
Pixels are put into 4 classes. 像素分为4类。
background (0) 背景(0)
foreground (1) 前景(1)
probably background (2) 大概背景(2)
probably foreground (3) 可能是前景(3)
Of course it would have been more obvious if the author of that code would have used the proper flags like cv2.GC_BGD instead of 0... 当然,如果该代码的作者使用正确的标志,例如cv2.GC_BGD而不是0,那将会更加明显。
Also mask2 might not be the most informative name. 而且mask2可能不是最有用的名称。
It is all in the OpenCV manual. 这一切都在OpenCV手册中。 All you have to do is google 1-2 words and read 1 paragraph...
所有您需要做的就是google 1-2个单词并阅读1个段落...
http://docs.opencv.org/3.2.0/d7/d1b/group__imgproc__misc.html#ga909c1dda50efcbeaa3ce126be862b37f http://docs.opencv.org/3.2.0/d7/d1b/group__imgproc__misc.html#ga909c1dda50efcbeaa3ce126be862b37f
mask Input/output 8-bit single-channel mask.
mask输入/输出8位单通道掩码。 The mask is initialized by the function when mode is set to GC_INIT_WITH_RECT.
当模式设置为GC_INIT_WITH_RECT时,该掩码由函数初始化。 Its elements may have one of the cv::GrabCutClasses.
它的元素可能具有cv :: GrabCutClasses之一。
and 和
enum cv::GrabCutClasses
枚举cv :: GrabCutClasses
class of the pixel in GrabCut algorithm
GrabCut算法中像素的类别
GC_BGD an obvious background pixels
GC_BGD背景像素明显
GC_FGD an obvious foreground (object) pixel
GC_FGD一个明显的前景(对象)像素
GC_PR_BGD a possible background pixel
GC_PR_BGD可能的背景像素
GC_PR_FGD a possible foreground pixel
GC_PR_FGD可能的前景像素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.