getOptimalNewCameraMatrix aspect ratio distortion

I’m noticing a structured / grid pattern in your calibration images when I zoom in. It reminds me of the type of pattern you can get when you have a raw bayer image and either don’t apply a bayer demosaicing algorithm, or if you use the wrong pattern when you demosaic the image. That’s just a guess, of course, and it’s probably not causing you too much trouble but it is something worth addresssing.

As for how to undistort the image to a larger output image (to use all of the source pixels without downscaling), I don’t use undistortImage with getOptimalNewCameraMatrix. It’s possible you can use these together to achieve what you want, but I had to do it differently because I was also applying a perspective distortion at the same time.

What I did:

  1. Adjust my camera matrix to a larger output image. this involves changing the image center so it is in the correct place proportionally, and then scaling the focal length*

  2. Use initUndistortRectifyMap and remap to undistort your original image to the larger output image. You might be able to just call undistortImage, but I needed to do it manually in two steps because I was applying a perspective distortion, too.

*Unfortunately I don’t have my original code that did this, and what I am referencing is a little different, so I’m going based on memory / understanding here. I think you will want to adjust the focal length (for newCameraMat that you pass to initUndistortRectifyMap) to be a little bit shorter than would be implied by direct scaling. For example, if you create an image that is 50% larger in width/height, I think you will want your focal length to be scaled to something that is less than 50% longer for the newCameraMat. This will result in you using more of the pixels (hopefully all of them) from your input image when creating the (larger resolution) output image.

I hope that make sense.

Here is a comment I have in my code that relates to scaling the camera matrix for a different size image:

    // Scaling the camera matrix when using pixel coordinates requires some strange 1/2 pixel shifting.
    // See link below for more information (Hammer's answer)
    // https://dsp.stackexchange.com/questions/6055/how-does-resizing-an-image-affect-the-intrinsic-camera-matrix

I hope this helps.