How to format xy dots for undistortPoints with python cv2 api?

I'm having trouble formatting the x, y list of points to go to unistortPoints (opencv 2.4.1).

The error message is specific to C ++ and complains about an array of points that are not of type CV_32FC2. Should I be able to pass numpy to an Nx2 array?

import cv2 camera_matrix = array(mat('1.3e+03, 0., 6.0e+02; 0., 1.3e+03, 4.8e+02; 0., 0., 1.'), dtype=float32) dist_coeffs = array(mat('-2.4-01, 9.5e-02, -4.0e-04, 8.9e-05, 0.'), dtype=float32) test = zeros((10,2), dtype=float32) print test.shape, type(test) xy_undistorted = cv2.undistortPoints(test, camera_matrix, dist_coeffs) 

leads to:

 opencv/modules/imgproc/src/undistort.cpp:279: error: (-215) CV_IS_MAT(_src) && CV_IS_MAT(_dst) && (_src->rows == 1 || _src->cols == 1) && (_dst->rows == 1 || _dst->cols == 1) && _src->cols + _src->rows - 1 == _dst->rows + _dst->cols - 1 && (CV_MAT_TYPE(_src->type) == CV_32FC2 || CV_MAT_TYPE(_src->type) == CV_64FC2) && (CV_MAT_TYPE(_dst->type) == CV_32FC2 || CV_MAT_TYPE(_dst->type) == CV_64FC2) in function cvUndistortPoints 

The / python 2 / video.py samples use the use of projectPoints, which takes an array and modifies it (-1.3), which leads to an Nx3 array for this function, it looks like the same format should work here.

+6
source share
1 answer

I know little about camera calibration. But, seeing your code and error, I changed it as follows:

 import cv2 import numpy as np camera_matrix = np.array([[1.3e+03, 0., 6.0e+02], [0., 1.3e+03, 4.8e+02], [0., 0., 1.]], dtype=np.float32) dist_coeffs = np.array([-2.4-01, 9.5e-02, -4.0e-04, 8.9e-05, 0.], dtype=np.float32) test = np.zeros((10,1,2), dtype=np.float32) xy_undistorted = cv2.undistortPoints(test, camera_matrix, dist_coeffs) print xy_undistorted 

Below is the result obtained, check if it is correct:

 [[[ 0.0187303 0.01477836]] [[ 0.0187303 0.01477836]] [[ 0.0187303 0.01477836]] [[ 0.0187303 0.01477836]] [[ 0.0187303 0.01477836]] [[ 0.0187303 0.01477836]] [[ 0.0187303 0.01477836]] [[ 0.0187303 0.01477836]] [[ 0.0187303 0.01477836]] [[ 0.0187303 0.01477836]]] 

What is the problem:

The error says that the source should have EITHER one row OR one column . And it should be CV_32FC2 or CV_64FC2, means two channels and floating point. So make your src form (10,1,2) or (1,10,2) . Both methods work and give the same result (I checked it myself). The only problem is that I do not know if this is correct, so see for yourself.

+8
source

Source: https://habr.com/ru/post/917988/


All Articles