error: (-215:Assertion failed) totalSampleCount > 0 in function 'GMM::endLearning'











up vote
1
down vote

favorite












Im trying to use the opencv to remove the background of my pictures.
When Im running a single file. It works out.
The code as below:



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)


However, when I use the function in a for loop. it pops-up:




error: (-215:Assertion failed) totalSampleCount > 0 in function
'GMM::endLearning'




when Im generating a group of pictures










share|improve this question




























    up vote
    1
    down vote

    favorite












    Im trying to use the opencv to remove the background of my pictures.
    When Im running a single file. It works out.
    The code as below:



    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)


    However, when I use the function in a for loop. it pops-up:




    error: (-215:Assertion failed) totalSampleCount > 0 in function
    'GMM::endLearning'




    when Im generating a group of pictures










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Im trying to use the opencv to remove the background of my pictures.
      When Im running a single file. It works out.
      The code as below:



      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)


      However, when I use the function in a for loop. it pops-up:




      error: (-215:Assertion failed) totalSampleCount > 0 in function
      'GMM::endLearning'




      when Im generating a group of pictures










      share|improve this question















      Im trying to use the opencv to remove the background of my pictures.
      When Im running a single file. It works out.
      The code as below:



      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)


      However, when I use the function in a for loop. it pops-up:




      error: (-215:Assertion failed) totalSampleCount > 0 in function
      'GMM::endLearning'




      when Im generating a group of pictures







      opencv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 4:15









      ZdaR

      12.8k33352




      12.8k33352










      asked Nov 22 at 2:27









      Elvis Sun

      62




      62





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423062%2ferror-215assertion-failed-totalsamplecount-0-in-function-gmmendlearnin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423062%2ferror-215assertion-failed-totalsamplecount-0-in-function-gmmendlearnin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)