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

时间:2018-11-22 02:27:06

标签: 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
    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'

当我生成一组图片时

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题,问题是矩形rect太小了。我不知道您图片的尺寸,但是尝试使用更大的矩形,它可能会解决此问题。