# Undistort function is cropping photo

**URL:** <https://forum.opencv.org/t/undistort-function-is-cropping-photo/4221>\
**Category:** C++\
**Tags:** fisheye\
**Created:** [July 7, 2021, 4:07pm UTC](https://forum.opencv.org/t/undistort-function-is-cropping-photo/4221 "2021-07-07T16:07:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rg0001](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rg0001](https://forum.opencv.org/u/rg0001)\
**Post date:** [July 7, 2021, 4:07pm UTC](https://forum.opencv.org/t/undistort-function-is-cropping-photo/4221/1 "2021-07-07T16:07:03Z")

</div>

I am running an undistort routine in my code. It is undistorting but the image is being cropped. I have tried the intrinsic matrix Fx, Fy (0,0) & (1,1) from the camera calibration program and also Fx = 1, Fy = 1. Still cropped.

Is this normal? If not what can I do to fix?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [July 7, 2021, 6:34pm UTC](https://forum.opencv.org/t/undistort-function-is-cropping-photo/4221/2 "2021-07-07T18:34:40Z")

</div>

you’re gonna have to provide details.

---

<div class="post-metadata">

**Author:** ![rg0001](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rg0001](https://forum.opencv.org/u/rg0001)\
**Post date:** [July 8, 2021, 2:35pm UTC](https://forum.opencv.org/t/undistort-function-is-cropping-photo/4221/3 "2021-07-08T14:35:19Z")

</div>

Ok so my app takes a single shot and sometimes video of a scene. The camera is about 130 degrees fisheye and I use the code to snap about 50 snaps of a checker board test pattern of 9 x 6 internal corners. I calibrate with

`calibrateCamera(object_points, image_points, gray_image.size(), intrinsic, distCoeffs, rvecs, tvecs)`

and saved. I then display the original image and the undistorted side by side.

```auto
Mat imageUndistorted;
			while (1)
			{
				undistort(gray_image, imageUndistorted, intrinsic, distCoeffs);

				imshow("distorted image", gray_image);
				imshow("undistorted image", imageUndistorted);
				waitKey(1);
                          }

```

Is it possible the sensor formats are different where the optical format of the lens is oversized?

imageUndistorted is cropped but undistorted.  
I then migrate the results to other apps. For example:

```auto
double	CV_CX = 388.7006986754312,
		CV_CY = 256.263330213388,
		CV_FX = 251.2948616331513,
		CV_FY = 251.7462825743613;

	cv::Mat cam_intrinsic(3, 3, CV_64FC1);
	cam_intrinsic.at<double>(0, 0) = CV_FX;
	cam_intrinsic.at<double>(1, 0) = 0.0;
	cam_intrinsic.at<double>(2, 0) = 0.0;

	cam_intrinsic.at<double>(0, 1) = 0.0;
	cam_intrinsic.at<double>(1, 1) = CV_FY;
	cam_intrinsic.at<double>(2, 1) = 0.0;

	cam_intrinsic.at<double>(0, 2) = CV_CX;
	cam_intrinsic.at<double>(1, 2) = CV_CY;
	cam_intrinsic.at<double>(2, 2) = 1.0;
	// new coeffs 11.29.19

	double	CV_K1 = -0.2625519217437085,
		CV_K2 = 0.06420791311470325,
		CV_P1 = -0.0004198556583919676,
		CV_P2 = -0.001233205211600967,
		CV_P3 = -0.006391088209028103;

	cv::Mat distCoeffs(5, 1, CV_64FC1);
	distCoeffs.at<double>(0) = CV_K1;
	distCoeffs.at<double>(1) = CV_K2;
	distCoeffs.at<double>(2) = CV_P1;
	distCoeffs.at<double>(3) = CV_P2;
	distCoeffs.at<double>(4) = CV_P3;

```

Thanks for the reply. Hope this will help.

Could it be that the sensor formats are different where one is oversized creating both the circular cropping and the zoom effect?

 ![undistorteddistorted](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/9/97813f62016535ab75baec19232b97be1bdac624.png)

Poor quality is just temporary.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [July 8, 2021, 3:49pm UTC](https://forum.opencv.org/t/undistort-function-is-cropping-photo/4221/4 "2021-07-08T15:49:46Z")

</div>

> [@rg0001](#):
>
> The camera is about 130 degrees fisheye

there’s a special [fisheye submodule](https://docs.opencv.org/master/db/d58/group __calib3d__ fisheye.html)

---

<div class="post-metadata">

**Author:** ![rg0001](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rg0001](https://forum.opencv.org/u/rg0001)\
**Post date:** [July 9, 2021, 5:10pm UTC](https://forum.opencv.org/t/undistort-function-is-cropping-photo/4221/5 "2021-07-09T17:10:07Z")

</div>

Ok, so I started digging into the fisheye submodule. So far this is my test code:

```auto
while (1)
						{
							Mat mapx = Mat(size, CV_32FC1);
							Mat mapy = Mat(size, CV_32FC1);
							Mat R = Mat::eye(3, 3, CV_32F);

							
							cv::fisheye::initUndistortRectifyMap(cam_intrinsic, distCoeffs, R, cam_intrinsic, size, CV_32FC1, mapx, mapy);
							
							Mat t = single_image.clone();
							cv::remap(single_image, t, mapx, mapy, INTER_LINEAR);
							
							//The 4 distortion coefficients of the camera : k1, k2, k3, k4
						
							cv::imshow("undist", t);
							waitKey(1);
							
						}

```

The function `cv::fisheye::initUndistortRectifyMap` requires 4 K distortion coefficients: K1, K2, K3, K4. I am bringing K1, K2, P1, P2, P3. (at beginning of post).

```auto
double	CV_K1 = -0.2625519217437085,
		CV_K2 = 0.06420791311470325,
		CV_P1 = -0.0004198556583919676,
		CV_P2 = -0.001233205211600967,
		CV_P3 = -0.006391088209028103;

```

How do I resolve the distCoeffs issue?
