python - 如何让 grabCut 与 GC_INIT_WITH_MASK 一起工作 opencv python

标签 python opencv numpy matplotlib

我正在尝试让 messi 示例起作用:https://docs.opencv.org/3.1.0/d8/d83/tutorial_py_grabcut.html

在我的设置中,我希望整个过程自动化。

例如,我从网上抓取一张图片: http://wanderlustandlipstick.com/travel-tips/opting-out-full-body-scanners/

Image Scan - imagescan.png

并使用一些 opencv 工具自动生成以下掩码:

Image Mask - imagemask.png

黑色应该是某个背景,白色应该是某个前景,灰色应该是未知。

按照梅西教程(https://docs.opencv.org/3.1.0/d8/d83/tutorial_py_grabcut.html),下面是我的代码。但是,它只显示了白色的小圆圈区域,就像把灰色当成黑色一样(某背景)

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread("imagescan.png")
dimy = np.shape(img)[0] # seems to be backwards (x,y)
# https://stackoverflow.com/questions/22490721/how-can-i-get-the-x-and-y-dimensions-of-a-ndarray-numpy-python
dimx = np.shape(img)[1]
mask = np.zeros((dimy,dimx),np.uint8) # zeroes as array/matrix size of image
bgdModel = fgdModel = np.zeros((1,65),np.float64)    

newmask = cv2.imread('imagemask.png',0)

# informational purposes
removeBg = (newmask == 0)
removeBg = np.ravel(removeBg)
np.bincount(removeBg)
keepFg = (newmask == 255)
keepFg = np.ravel(keepFg)
np.bincount(keepFg)

#otherEl = (not (newmask == 0 or newmask == 255)) # throws error
#otherEl = np.ravel(otherEl)
#np.bincount(otherEl)

# appears at least one of each elements is required
# otherwise throws bgdSamples.empty error / fgdSamples.empty error
mask[newmask == 0] = 0
mask[newmask == 255] = 1

mask, bgdModel, fgdModel = cv2.grabCut(img,mask,None,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_MASK)

mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img2 = img*mask2[:,:,np.newaxis]
plt.imshow(img2),plt.colorbar(),plt.show()

结果只是圆圈外的蒙版,就好像灰色区域被视为黑色一样。

Output is not what I want!

最佳答案

在蒙版图像中,基本上有 3 种颜色:黑色、白色、灰色。在下面的代码行中,您将设置背景和前景,但不是可能的前景。

mask[newmask == 0] = 0
mask[newmask == 255] = 1

尝试使用 OpenCV 提供的常量(cv2.GC_BGD 等)以避免混淆。

# this line sets the grey areas - meaning any color not 0 and not 255 - to probable foreground.
mask = np.where(((newmask>0) & (newmask<255)),cv2.GC_PR_FGD,0).astype('uint8')
mask[newmask == 0] = cv2.GC_BGD
mask[newmask == 255] = cv2.GC_FGD

Result .

关于python - 如何让 grabCut 与 GC_INIT_WITH_MASK 一起工作 opencv python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46825504/

相关文章:

python - 将 Numpy 中的矩阵列表相乘

python - 如何将 ML 模型的前后处理合并为 ONNX 格式

python - 如何在不丢失数据的情况下更改 .tif 光栅文件的分辨率

function - 使用 cv::equalizeHist() 的 OpenCV 直方图均衡过程:需要帮助理解最后一步

c++ - 如何估计相机从场景中拍摄优质图像的曝光时间

python - 为什么 NumPy 数组优于标准库数组?

python - 在Python中,json.dumps无法处理{integer : 'string' ,}的字典

python - Airflow 轮换加密 key

opencv - 如何接收Jetson TX1嵌入式摄像头的图像?

python - 在 3d 数组上快速处理 numpy polyfit?