Where is the value of _err in cvCalibrateCamera2Internal() function gets updated?

Hi ,

I’m using cv::calibrateCamera() API for Intrinsic camera calibration . I’m using Opencv 4.4 version library / source code. For this cv::calibrateCamera() api , I found below routine in calibration.cpp source code in calib3d module :
cv::calibrateCamera → calibrateCameraRO → cvCalibrateCamera2Internal()

In cvCalibrateCamera2Internal() function (at line number 1683 to 1817 ) “run the optimization” is called . It seems here LM optimizer is called. My target is to understand optimization method that Opencv using for distortion parameters calculation .

Below is the code part ( from line 1779 to 1787) that’s confusing me.

double viewErr = norm(_err, NORM_L2SQR);

            if( perViewErrors )
                perViewErrors->data.db[i] = std::sqrt(viewErr / ni);

            reprojErr += viewErr;
        }
        if( _errNorm )
            *_errNorm = reprojErr;

When I step through the ‘_err’ change , I did not get where it gets updated.
‘_err’ value changes after coming back from cvProjectPoint2() function. It’s updated with random value.

There seems confusion messed up between ‘_mp’ and ‘_err’. You can see the below code ( at line 1750-1752 ) in calibration.cpp:

cvSub( &_mp, &_mi, &_mp );
            if (perViewErrors || stdDevs)
                cvCopy(&_mp, &_me);

So it seems this code might have bug.
Can anyone/Opencv team please check this in ‘calibration.cpp’ in calib3d module source code in Opencv 4.4 version and confirm .
If I’m wrong , kindly correct me and let me understand How ‘_err’ is updated in Optimization section?

Thanks in advance.