如何让grabCut使用GC_INIT_WITH_MASK来使用opencv python [英] How to get grabCut to work opencv python with GC_INIT_WITH_MASK
问题描述
我正在尝试使messi示例工作:
并使用一些 opencv 工具自动生成以下掩码:
黑色应该是某个背景,白色应该是某个前景,而灰色应该是未知的.
遵循messi教程(
在蒙版图像中,您基本上有 3 种颜色:黑色、白色、灰色.在以下代码行中,您设置的是背景和前景,但不是可能的前景.
mask [newmask == 0] = 0mask [newmask == 255] = 1
尝试使用 OpenCV 提供的常量(cv2.GC_BGD
等)以避免混淆.
#这行代码将灰色区域(表示非0和255的任何颜色)设置为可能的前景.mask = np.where(((newmask>0) & (newmask<255)),cv2.GC_PR_FGD,0).astype('uint8')mask [newmask == 0] = cv2.GC_BGDmask [newmask == 255] = cv2.GC_FGD
.
I am trying to get the messi example to work: https://docs.opencv.org/3.1.0/d8/d83/tutorial_py_grabcut.html
In my setup, I want the entire process to be automated.
For example, I grab an image from the web: http://wanderlustandlipstick.com/travel-tips/opting-out-full-body-scanners/
And using some opencv tools I autogenerate the following mask:
Black is supposed to be a certain background, White is supposed to be a certain foreground, and Grey is supposed to be unknown.
Following the messi tutorial (https://docs.opencv.org/3.1.0/d8/d83/tutorial_py_grabcut.html), below is my code. However, it only shows the small white circle area, as if it is treating grey like black (certain background)
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()
The result is just a mask off the circle, as if the gray area is being treated as black.
In the mask image, you basically have 3 colors: black,white,grey. In the following lines of code, you're setting background and foreground, but not the probable foreground.
mask[newmask == 0] = 0
mask[newmask == 255] = 1
Try using using OpenCV provided constants (cv2.GC_BGD
etc) to avoid confusion.
# 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
.
这篇关于如何让grabCut使用GC_INIT_WITH_MASK来使用opencv python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!