title: OpenCV Calibrating Camera cropping error
body: <p>I am working on calibrating my camera for OpenCV 3. Currently I am following tutorials and documentation and I have come across a problem. The end of my code has me crop the image, as to remove the visible distortion (as seen bellow)<a href="https://i.stack.imgur.com/qvcXC.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qvcXC.jpg" alt="Working Distorted Image"></a>
  However when I try this I sometimes get the error<br>
  <code>libpng warning: Image width is zero in IHDR
  libpng warning: Image height is zero in IHDR
  libpng error: Invalid IHDR data</code>
  It does not stop the program from running, however it saves an image with no pixels. I assume the crop function is cropping the entire image and this happens about 80%~ of the time.</p>
  
  <pre><code>import numpy as np
  import cv2
  import glob
  import time
  
  thyme = ('1')
  
  # termination criteria
  criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
  
  # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
  objp = np.zeros((6*9,3), np.float32)
  objp[:,:2] = np.mgrid[0:6,0:9].T.reshape(-1,2)
  
  # Arrays to store object points and image points from all the images.
  objpoints = [] # 3d point in real world space
  imgpoints = [] # 2d points in image plane.
  
  images = glob.glob('*.jpg')
  
  for fname in images:
      img = cv2.imread(fname)
      gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  
  
      # Find the chess board corners
      ret, corners = cv2.findChessboardCorners(gray, (6,9),None)
  
      # If found, add object points, image points (after refining them)
      if ret == True:
          objpoints.append(objp)
  
          ticks = time.time()
          thyme = str(ticks)
          corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
          imgpoints.append(corners2)
  
          # Draw and display the corners
          img = cv2.drawChessboardCorners(img, (6,9), corners2,ret)
  
          ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
  
          img = cv2.imread('left1.jpg')
          h,  w = img.shape[:2]
          newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
  
          # undistort
          dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
  
          # crop the image
          x,y,w,h = roi
          print('1')
          dst = dst[y:y+h, x:x+w]
          print('2')
          cv2.imwrite('calibresult' + thyme + '.png',dst)
          print('3')
  
          cv2.imshow('img',img)
          cv2.waitKey(500)
  
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>The print statements are included so that I can see if the image throws the error or if it comes out clean.</p>
  
  <p>EDIT: Here is my error</p>
  
  <p><code>1
  2
  libpng warning: Image width is zero in IHDR
  libpng warning: Image height is zero in IHDR
  libpng error: Invalid IHDR data
  3
  1
  2
  libpng warning: Image width is zero in IHDR
  libpng warning: Image height is zero in IHDR
  libpng error: Invalid IHDR data
  3
  1
  2
  libpng warning: Image width is zero in IHDR
  libpng warning: Image height is zero in IHDR
  libpng error: Invalid IHDR data
  3</code></p>