Created
July 15, 2018 16:41
-
-
Save saurabheights/7e6801a6bf520d783e46156c38502d9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transformPixelBetweenCameras(pixelLocationInSrcFrame, | |
depth, | |
srcCameraMatrix, | |
srcCameraDistortionCoefficients, | |
srcCameraRVec, | |
srcCameraTVec, | |
dstCameraMatrix, | |
dstCameraDistortionCoefficients, | |
dstCameraRVec, | |
dstCameraTVec): | |
srcFrameHomogeneousPixelLoc = np.array([pixelLocationInSrcFrame[0], pixelLocationInSrcFrame[1], 1.0], | |
dtype=np.float64) | |
srcFrameHomogeneousPixelLoc.reshape((3, 1)) | |
if depth == 0 or depth > 1500: # Static Camera was too close to board but still some values are above 6000 | |
depth = 1000 | |
else: | |
print("Using depth frames depth: %d".format(depth)) | |
# Compute R and T matrix for source frame | |
srcCameraR, _ = cv2.Rodrigues(srcCameraRVec) | |
srcCameraT = srcCameraTVec | |
# Compute Rinv and Tinv matrix for source frame | |
srcCameraRInv = srcCameraR.transpose() | |
srcCameraTInv = np.matmul(srcCameraRInv, -srcCameraT) | |
# First undistort the source image pixel location. | |
pixelLocationInSrcFrame = cv2.undistort(srcFrameHomogeneousPixelLoc, srcCameraMatrix, | |
srcCameraDistortionCoefficients, srcCameraMatrix) | |
# Compute Camera inverse matrix and use Rinv and Tinv matrix to find XYZ location. | |
srcCameraMatrixInv = np.linalg.inv(srcCameraMatrix) | |
srcCameraCoordinates = np.matmul(srcCameraMatrixInv, srcFrameHomogeneousPixelLoc) | |
xyzPoint = (np.matmul(srcCameraRInv, srcCameraCoordinates)).transpose() + srcCameraTInv.transpose() | |
xyzPoint = xyzPoint * depth/1000.0 | |
# The reverse, moving xyzPoint to pixel in destination can be done with a single opencv call | |
dstImagePoints, _ = cv2.projectPoints(xyzPoint, | |
dstCameraRVec, | |
dstCameraTVec, | |
dstCameraMatrix, | |
dstCameraDistortionCoefficients) | |
return dstImagePoints |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Call it as:-