title: How to use camera parameters to calculate world distance in cm between pixels?
body: <p>I calibrated my camera using, separately, <a href="http://wiki.ros.org/camera_calibration/Tutorials/MonocularCalibration" rel="nofollow noreferrer">ROS</a>, <a href="https://docs.opencv.org/3.4.0/dc/dbb/tutorial_py_calibration.html" rel="nofollow noreferrer">OpenCv</a> and <a href="https://www.mathworks.com/help/vision/ug/single-camera-calibrator-app.html" rel="nofollow noreferrer">Matlab</a>.  People say that I need extrinsic parameters to calculate real distance between pixels in cm from the image. ROS does not provide extrinsic parameters explicitly, it provides a (4,3) projection matrix which is the output of multiplied intrinsic and extrinsic parameters.</p>
  
  <p><a href="https://i.stack.imgur.com/nOJV0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nOJV0.png" alt="Ros camera.yaml file which includes camera parameters"></a></p>
  
  <p>That is why I again calibrated my camera using OpenCv and Matlab to get extrinsic parameters. Although I searched how can I calculate real distance in cm between pixels (i.e from (x1,y1) to (x2,y2)), I could not figure out how to calculate the real distance. Moreover, I did not understand that which parameters to use for distance calculation. I want to use OpenCv to calculate the distance between pixels and write the output as a txt file so that I can use this txt file to move my robot. </p>
  
  <p>For example, here is array of pixel output sample for the path,</p>
  
  <pre><code>array([[  4.484375  , 799.515625  ],
         [ 44.484375  , 487.        ],
         [255.296875  , 476.68261719],
         [267.99707031, 453.578125  ],
         [272.484375  , 306.        ],
         [403.484375  , 300.515625  ],
         [539.484375  , 296.515625  ],
         [589.421875  , 270.00292969],
         [801.109375  , 275.18554688],
         [819.        , 467.515625  ]])
  </code></pre>
  
  <p>I want to find the real distance in cm between these pixels in an order. </p>
  
  <p>OpenCv Code that calculating parameters:</p>
  
  <pre><code>import numpy as np
  import cv2
  import glob
  
  # termination criteria
  criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
  
  cbrow = 6
  cbcol = 9
  
  # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
  objp = np.zeros((cbrow*cbcol,3), np.float32)
  objp[:,:2] = np.mgrid[0:cbcol,0:cbrow].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('C:\Users\Ender\Desktop\CalibrationPhotos\*.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, (cbcol,cbrow),None)
  
      # If found, add object points, image points (after refining them)
      if ret == True:
          print "%s: success" % fname
          objpoints.append(objp)
  
          cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
          imgpoints.append(corners)
  
          # Draw and display the corners
          cv2.drawChessboardCorners(img, (cbcol,cbrow), corners,ret)
          cv2.imshow('img',img)
          cv2.waitKey(150)
  
      else:
          print "%s: failed" % fname
          cv2.imshow('img',img)
          cv2.waitKey(1)
  
  cv2.destroyAllWindows()
  
  ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
  
  print "mtx"
  print mtx
  print "dist"
  print dist
  #print "rvecs"
  #print rvecs
  #print "tvecs"
  #print tvecs
  np.savez("CalibData.npz" ,mtx=mtx, dist=dist, rvecs=rvecs, tvecs=tvecs)
  
  #UNDISTROTION
  img = cv2.imread('C:\Users\Ender\Desktop\Maze\Maze Images\Partial Maze Images-Raw\Raw7.jpg')
  h,  w = img.shape[:2]
  newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
  
  dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
  
  # crop the image
  x,y,w,h = roi
  dst = dst[y:y+h, x:x+w]
  cv2.imwrite('calibresult.jpg',dst)
  
  #Re-projection Errors
  total_error = 0
  for i in xrange(len(objpoints)):
      imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
      error = cv2.norm(imgpoints[i],imgpoints2, cv2.NORM_L2)/len(imgpoints2)
      total_error += error
  
  print "total error: ", total_error/len(objpoints)
  </code></pre>
  
  <p>What is the way of doing this ?</p>