diff scripts/undistort-video.py @ 926:dbd81710d515

new feature tracking in image space with point undistortion
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 10 Jul 2017 18:04:41 -0400
parents a71455bd8367
children c03d2c0a4c04
line wrap: on
line diff
--- a/scripts/undistort-video.py	Mon Jul 10 01:38:12 2017 -0400
+++ b/scripts/undistort-video.py	Mon Jul 10 18:04:41 2017 -0400
@@ -16,6 +16,7 @@
 parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file')
 parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float)
 parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float)
+parser.add_argument('--mask', dest = 'maskFilename', help = 'name of the mask file, to undistort to see how it covers the undistortion errors')
 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int, default = 0)
 parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to save', type = int)
 parser.add_argument('-d', dest = 'destinationDirname', help = 'name of the directory where the undistorted frames are saved')
@@ -26,9 +27,6 @@
 args = parser.parse_args()
 
 intrinsicCameraMatrix = np.loadtxt(args.intrinsicCameraMatrixFilename)
-#distortionCoefficients = args.distortionCoefficients
-#undistortedImageMultiplication = args.undistortedImageMultiplication
-#firstFrameNum = params.firstFrameNum
 if args.destinationDirname is None:
     destinationDirname = ''
 else:
@@ -43,6 +41,9 @@
 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
 [map1, map2] = cvutils.computeUndistortMaps(width, height, args.undistortedImageMultiplication, intrinsicCameraMatrix, args.distortionCoefficients)
+if args.maskFilename is not None:
+    mask = cv2.imread(args.maskFilename)
+    undistortedMask = cv2.remap(mask, map1, map2, interpolation=cv2.INTER_LINEAR)/255
 
 if capture.isOpened():
     ret = True
@@ -59,6 +60,8 @@
         if ret:
             img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
             cv2.imwrite(destinationDirname+'undistorted-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img)
+            if args.maskFilename is not None:
+                cv2.imwrite(destinationDirname+'undistorted+mask-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), cv2.multiply(img, undistortedMask))
         frameNum += 1
 
 if args.encodeVideo: