Re-distorting a set of points after camera scale

I am working on a project in Python to calibrate a small thermal photographic camera sensor (FLIR Lepton). Because of limited resolution initial distortion removal is not very exact. By using an iterative method I should be able to refine this calibration (for those of y'all with admission to scientific articles, see this link). This requires me to take the post-obit steps:

  1. Use a set of images of a calibration blueprint to estimate the initial distortion
  2. Undistort the images
  3. Use a perspective correction to the undistorted images
  4. Re-judge the calibration bespeak positions
  5. Remap these refined calibration points back to the original images
  6. Use the refined points to re-estimate the distortion
  7. Repeat until the RMS-mistake converges

I am stuck at step four. Beneath you come across the commands I used to remove the camera distortion from the original image using the camera matrices and the distortion matrix.

            mapx,mapy = cv2.initUndistortRectifyMap(mtx,dist,None,newcameramtx,(w,h),five) dst = cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)                      

So far I am non able to figure out how to reverse these commands to remap the new point positions to the original image. So far I am able to do (roughly in order of the steps above):

image description image description image description image description image description image description

I have looked online and found the aforementioned question a bunch of times, with several examples in C++, which I cannot fully embrace and modify for my purposes. I take tried the solution suggested past this post simply this has not yielded the desired results, see last image above. There is my code of that solution

            def distortBackPoints(ten, y, cameraMatrix, dist):  fx = cameraMatrix[0,0] fy = cameraMatrix[ane,i] cx = cameraMatrix[0,two] cy = cameraMatrix[1,two] k1 = dist[0][0] * -1 k2 = dist[0][1] * -1 k3 = dist[0][four] * -1 p1 = dist[0][2] * -i p2 = dist[0][three] * -1 10 = (10 - cx) / fx y = (y - cy) / fy  r2 = x*x + y*y  xDistort = x * (1 + k1 * r2 + k2 * r2 * r2 + k3 * r2 * r2 * r2) yDistort = y * (1 + k1 * r2 + k2 * r2 * r2 + k3 * r2 * r2 * r2)  xDistort = xDistort + (2 * p1 * x * y + p2 * (r2 + two * ten * x)) yDistort = yDistort + (p1 * (r2 + 2 * y * y) + two * p2 * x * y)  xDistort = xDistort * fx + cx; yDistort = yDistort * fy + cy;  return xDistort, yDistort                      

And so using this control to phone call the function

            corners2 = [] for point in corners:     x, y = distortBackPoints(betoken[0][0], point[0][1], newcameramtx, dist)     corners2.append([x,y])                      

I am new to OpenCV and reckoner vision, so my knowledge nigh the algebra of these solutions is express. Any easily on examples or correction to my electric current code would be profoundly appreciated.

Kind regards,

Bart