title: Python - OpenCv Camera Calibration - Saving map matrix to file and read them back
body: <p>I'm trying to store into a txt file the result of my camera calibration intended as mapx and mapy retrived from the following script:</p>
  
  <pre><code>mapx, mapy = cv2.initUndistortRectifyMap(mtx, dist, None, newcameramtx, (w, h), cv2.CV_16SC2)
  </code></pre>
  
  <p>Those are two matrix with respective shape: <code>(480, 640, 2)</code> and <code>(480, 640)</code> </p>
  
  <p>The save function is working properly and it is the following one:</p>
  
  <pre><code>def write(filename, data):
      with open(filename, 'w') as outfile:
          outfile.write('# Array shape: {0}\n'.format(data.shape))
  
          for data_slice in data:
              np.savetxt(outfile, data_slice, fmt='%-7.0f')
              outfile.write('# New slice\n')
  </code></pre>
  
  <p>Once the file is stored appears like this (mapy example):</p>
  
  <pre><code># Array shape: (480, 640)
  372    
  170    
  992    
  823    
  621    
  452    
  282    
  113    
  935
  ....
  86     
  193    
  # New slice
  274    
  203    
  131    
  92     
  21     
  1006   
  935
  ....
  </code></pre>
  
  <p>I'm reading it back with this function:</p>
  
  <pre><code>shapex = (480, 640, 2)
  shapey = (480, 640)
  
  file_mapx = "mapx.txt"
  file_mapy = "mapy.txt"
  
  mapx = np.loadtxt(file_mapx) if os.path.exists(file_mapx) else None
  mapy = np.loadtxt(file_mapy) if os.path.exists(file_mapy) else None  
  
  
  if mapx is not None:
      print("Reshape mapx")
      mapx = mapx.reshape(shapex)
  if mapy is not None:
      print("Reshape mapy")
      mapy = mapy.reshape(shapey)
  </code></pre>
  
  <p>Everything seams working correctly but when I use those maps for the undistortion function </p>
  
  <pre><code># undistort
  undistorted_img = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>undistorted_img = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
  cv2.error: OpenCV(4.1.1) /home/myuser/opencv/modules/imgproc/src/imgwarp.cpp:1815: error: (-215:Assertion failed) ((map1.type() == CV_32FC2 || map1.type() == CV_16SC2) &amp;&amp; map2.empty()) || (map1.type() == CV_32FC1 &amp;&amp; map2.type() == CV_32FC1) in function 'remap'
  </code></pre>
  
  <p>I tried following the question <a href="https://stackoverflow.com/questions/3685265/how-to-write-a-multidimensional-array-to-a-text-file">here</a> but I didn't manage to understand the mistake, any help?</p>