Je suis en train d'obtenir l'exemple de messi au travail: https://docs.opencv.org/3.1.0/d8/d83/tutorial_py_grabcut.htmlComment obtenir grabCut travailler python OpenCV avec GC_INIT_WITH_MASK
Dans ma configuration, je veux l'ensemble du processus automatisé.
Par exemple, je prends une image à partir du Web: http://wanderlustandlipstick.com/travel-tips/opting-out-full-body-scanners/
Et en utilisant des outils OpenCV J'AutoGenerate le masque suivant:
noir est censé être un certain fond, White est censé être un certain premier plan, et Gray est censé être inconnu.
Après le didacticiel Messi (https://docs.opencv.org/3.1.0/d8/d83/tutorial_py_grabcut.html), voici mon code. Cependant, il ne montre que la petite zone de cercle blanc, comme si elle traite gris comme le noir (certains arrière-plan)
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()
Le résultat est juste un masque hors du cercle, comme si la zone grise est traitée en noir .