annotate trafficintelligence/tests/cvutils.txt @ 1030:aafbc0bab925

moved method around to avoid cross-dependencies
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 19 Jun 2018 10:04:52 -0400
parents cc5cb04b04b0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
930
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
1 >>> from numpy import array, round, ones, dot, linalg, absolute
1030
aafbc0bab925 moved method around to avoid cross-dependencies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1028
diff changeset
2 >>> import cv2
aafbc0bab925 moved method around to avoid cross-dependencies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1028
diff changeset
3 >>> from trafficintelligence import cvutils
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
4 >>> img = cv2.imread("../samples/val-dor-117-111.png")
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
5 >>> width = img.shape[1]
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
6 >>> height = img.shape[0]
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
7 >>> intrinsicCameraMatrix = array([[ 377.42, 0. , 639.12], [ 0. , 378.43, 490.2 ], [ 0. , 0. , 1. ]])
930
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
8 >>> distortionCoefficients = array([-0.11759321, 0.0148536, 0.00030756, -0.00020578, -0.00091816])# distortionCoefficients = array([-0.11759321, 0., 0., 0., 0.])
929
be28a3538dc9 work in progress on projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
9 >>> multiplicationFactor = 1.31
933
8ac7f61c6e4f major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 932
diff changeset
10 >>> [map1, map2], tmp = cvutils.computeUndistortMaps(width, height, multiplicationFactor, intrinsicCameraMatrix, distortionCoefficients)
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
11 >>> undistorted = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
929
be28a3538dc9 work in progress on projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
12 >>> (undistorted.shape == array([int(round(height*multiplicationFactor)), int(round(width*multiplicationFactor)), 3])).all()
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
13 True
929
be28a3538dc9 work in progress on projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
14 >>> imgPoints = array([[[150.,170.],[220.,340.],[340.,440.],[401.,521.]]])
be28a3538dc9 work in progress on projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
15 >>> newCameraMatrix = cv2.getDefaultNewCameraMatrix(intrinsicCameraMatrix, (int(round(width*multiplicationFactor)), int(round(height*multiplicationFactor))), True)
932
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
16 >>> undistortedPoints = cv2.undistortPoints(imgPoints, intrinsicCameraMatrix, distortionCoefficients, P = newCameraMatrix).reshape(-1, 2) # undistort and project as if seen by new camera
930
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
17 >>> invNewCameraMatrix = linalg.inv(newCameraMatrix)
929
be28a3538dc9 work in progress on projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
18 >>> tmp = ones((imgPoints[0].shape[0], 3))
be28a3538dc9 work in progress on projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
19 >>> tmp[:,:2] = undistortedPoints
930
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
20 >>> reducedPoints = dot(invNewCameraMatrix, tmp.T).T
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
21 >>> origPoints = cv2.projectPoints(reducedPoints, (0.,0.,0.), (0.,0.,0.), intrinsicCameraMatrix, distortionCoefficients)[0].reshape(-1,2)
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
22 >>> (round(origPoints[1:,:]) == imgPoints[0][1:,:]).all()
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
23 True
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
24 >>> (absolute(origPoints[0,:]-imgPoints[0][0,:])).max() < 6.
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
25 True
932
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
26 >>> reducedPoints2 = cvutils.newCameraProject(undistortedPoints.T, invNewCameraMatrix)
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
27 >>> (reducedPoints == reducedPoints).all()
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
28 True
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
29
933
8ac7f61c6e4f major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 932
diff changeset
30 >>> undistortedPoints2 = cv2.undistortPoints(imgPoints, intrinsicCameraMatrix, distortionCoefficients).reshape(-1, 2) # undistort and project as if seen by new camera
8ac7f61c6e4f major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 932
diff changeset
31 >>> undistortedPoints2 = cvutils.newCameraProject(undistortedPoints2.T, newCameraMatrix)
8ac7f61c6e4f major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 932
diff changeset
32 >>> (undistortedPoints == undistortedPoints2.T).all()
8ac7f61c6e4f major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 932
diff changeset
33 True
8ac7f61c6e4f major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 932
diff changeset
34
932
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
35 >>> undistortedPoints = cv2.undistortPoints(imgPoints, intrinsicCameraMatrix, distortionCoefficients).reshape(-1, 2) # undistort to ideal points
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
36 >>> origPoints = cvutils.worldToImageProject(undistortedPoints.T, intrinsicCameraMatrix, distortionCoefficients).T
930
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
37 >>> (round(origPoints[1:,:]) == imgPoints[0][1:,:]).all()
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
38 True
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
39 >>> (absolute(origPoints[0,:]-imgPoints[0][0,:])).max() < 6.
7db0f2853bfd corrected projection back to image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 929
diff changeset
40 True
932
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
41
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
42 >>> undistortedPoints = cvutils.imageToWorldProject(imgPoints[0].T, intrinsicCameraMatrix, distortionCoefficients)
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
43 >>> origPoints = cvutils.worldToImageProject(undistortedPoints, intrinsicCameraMatrix, distortionCoefficients).T
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
44 >>> (round(origPoints[1:,:]) == imgPoints[0][1:,:]).all()
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
45 True
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
46 >>> (absolute(origPoints[0,:]-imgPoints[0][0,:])).max() < 6.
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 930
diff changeset
47 True