nishihara.me


navigation
home
twitter
github
flickr
about
I am a developer and photography enthusiast.

How to improve OpenCV performance on lens undistortion from a video feed

03 Sep 2015

Currently, for my master’s dissertation, I am developing a software for IOS (iPad) which uses OpenCV to process images. On this project I am using a simple fisheye lens which is attached in front of the iPad´s back camera. As I need to process images without the lens distortion, I calibrated the camera and used the cv::undistort method from OpenCV on each new video frame. The framerate dropped a lot due to the cv::undistort.

Reading from the OpenCV documentation I discovered that the cv::undistort method was using a combination of cv::initUndistortRectifyMap and cv::remap. The cv::initUndistortRectifyMap is an expensive method which was being called on each frame and didn´t change as the camera parameters were fixed.

To improve the OpenCV performance, I simply cached the map results from cv::initUndistortRectifyMap and called cv::remap with the cached results:

cv::Mat map1, map2;

// Do this once after camera calibration
// Particularly, on IOS, try to do this initialization on viewDidLoad
// After this initialization, cache the map1 and map2
cv::initUndistortRectifyMap(intrinsic_Matrix, distortion_coeffs, Mat_<double>::eye(3,3), intrinsic_Matrix, cv::Size(1280,720), CV_16SC2, map1, map2 );
	
//..

// On -(void)processImage:(cv::Mat&)image, reuse the initialized map1 and map2
cv::Mat tmp = image.clone();
cv::remap(tmp, image, map1, map2, INTER_LINEAR, BORDER_CONSTANT);