title: remove following: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'
body: <p>I am trying to extract the silhouettes from video by below python code but after extraction, at last point, I am getting the following error: </p>
  
  <p>cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'</p>
  
  <p>how to remove this error.</p>
  
  <p>Python code is:</p>
  
  <pre><code>import numpy as np
  import cv2
  import os
  cap = cv2.VideoCapture('E:\\DCIM_with_two_cycle\\In_Lab\\021\\MVI_0098.MP4')
  fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
  count = 0;
  while 1:
      count = count + 1
      ret, frame = cap.read()
      fgmask = fgbg.apply(frame)
      # cv2.imshow('frame', fgmask)
      if count &lt; 10:
          cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image000' + str(count) + '.jpg', fgmask)
      elif count &lt; 100:
          cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image00' + str(count) + '.jpg', fgmask)
      elif count &lt; 1000:
          cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image0' + str(count) + '.jpg', fgmask)
      elif count &lt; 10000:
          cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image' + str(count) + '.jpg', fgmask)
      k = cv2.waitKey(30) &amp; 0xff
      if k == 27:
          break
  cap.release()
  #cv2.destroyAllWindows()
  </code></pre>