Opencv 错误:(-215:断言失败)totalSampleCount>;函数'中的0;GMM::endLearning';

Opencv 错误:(-215:断言失败)totalSampleCount>;函数'中的0;GMM::endLearning';,opencv,Opencv,我正在尝试使用opencv删除图片的背景。 当我运行单个文件时。这是可行的。 代码如下: def bgremove(name,count): import cv2 import numpy as np # cv2.namedWindow('image',cv2.WINDOW_NORMAL) #Load the Image imgo = cv2.imread(name)# the place to input picture path heigh

我正在尝试使用opencv删除图片的背景。 当我运行单个文件时。这是可行的。 代码如下:

def bgremove(name,count):
    import cv2
    import numpy as np
    # cv2.namedWindow('image',cv2.WINDOW_NORMAL)

    #Load the Image

    imgo = cv2.imread(name)# the place to input picture path
    height,width = imgo.shape[:2]

    #Create a mask holder
    mask = np.zeros(imgo.shape[:2],np.uint8)

    #Grab Cut the object
    bgdModel = np.zeros((1,65),np.float64)
    fgdModel = np.zeros((1,65),np.float64)

    #Hard Coding the Rect… The object must lie within this rect.
    rect = (10,10,width-30,height-30)
    cv2.grabCut(imgo,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
    mask = np.where((mask==2)|(mask==0),0,1).astype('uint8')
    img1 = imgo*mask[:,:,np.newaxis]

    #Get the background
    background = imgo-img1

    #Change all pixels in the background that are not black to white
    background[np.where((background > [0,0,0]).all(axis = 2))] = [255,255,255]

    #Add the background and the image
    final = background + img1
    DP1=count

    #To be done – Smoothening the edges….
    cv2.imwrite("A%s.JPG"%DP1, final)
但是,当我在for循环中使用该函数时。它会弹出:

错误:(-215:断言失败)函数中的totalSampleCount>0 “GMM::endLearning”


当我生成一组图片时,我遇到了这个问题,问题是矩形
rect
太小。我不知道你的图片的尺寸,但是试试一个更大的矩形,它可能会解决这个问题