How to use this code to correct barrel distortion?

asked 2018-05-14 03:39:00 -0500

garbalawal gravatar image

updated 2018-05-14 03:58:20 -0500

How to correct barrel distortion with this code:

DLL_IPT4V_EXPORT void CIpt4vMain::ApplyCorrectBarrelDistortion(Mat & InputMat,Mat& map_x, Mat& map_y,float qvDepth, float fixStrength, float fixZoom, float lensRadius
, long optWidth /*= 1944*/, long optHeight/* = 2580*/)

{ Mat src = InputMat.clone(); //ApplyLensCorrection(double fixStrength, double fixZoom, double lensRadius, long long edgeHandling, long long superSamplingAmount

//cout << "qvDepth :" << qvDepth << " fixStrength :" << fixStrength << " fixZoom :" << fixZoom << " lensRadius :" << lensRadius << endl;
//float qvDepth = 32;//24;
//float fixStrength = 4.5; // has to utilized further
//float fixZoom = 0.5;
//float lensRadius =2;

//Calculate the center of the image
//double midX = 0;
//double midY = 0;
long tWidth = optWidth;
long tHeight = optHeight;

// the center 
double midX = (double)src.cols / 2;
double midY = (double)src.rows / 2;

//Rotation values
double theta = 0;
double sRadius = 0;
double sRadius2 = 0;
double sDistance = 0;
double radius = 0;


double j = 0;
double k = 0;
//X and Y values, remapped around a center point of (0, 0)
double nX = 0;
double nY = 0;
double QuickVal = 0;
float ssX;
float ssY;

//Source X and Y values, which may or may not be used as part of a bilinear interpolation function
double srcX = 0;
double srcY = 0;

sRadius = sqrt(tWidth * tWidth + tHeight * tHeight) / 2;
//cout << "sRadius :" << sRadius << endl;

double refDistance = 0;//modified 0 to 2
if (fixStrength == 0)
{
    fixStrength = 0.00000001;
}
refDistance = sRadius * 2 / fixStrength;

sRadius = sRadius * (lensRadius / 100);
sRadius2 = sRadius * sRadius;
//cout << "refDistance :" << refDistance << " sRadius :" << sRadius << " sRadius2 :" << sRadius2 << endl;
float ReplaceX = 0.0f, ReplaceY = 0.0f;
float sampleIndex = 1; //has to be changed in future
for (float x = 0.0f; x <= tWidth; x++)
{
    QuickVal = x * qvDepth;
    for (float y = 0.0f; y <= tHeight; y++)
    {

        //Remap the coordinates around a center point of (0, 0)
        nX = x - midX;
        nY = y - midY;

        //Offset the pixel amount by the supersampling lookup table
        for (float ii = 1; ii<4.0f; ii++) {
            j = nX + ii;
            k = nY + ii;

            //Calculate distance automatically
            sDistance = (j * j) + (k * k);
            //cout<<"nx :"<<nX<<" ny :"<<nY<<" j :"<<j<<" k :"<<k<<" sDistance :"<<sDistance<<" sRadius2 :"<<sRadius2<<endl;
            if (sDistance <= sRadius2)
            {

                sDistance = sqrt(sDistance);
                radius = sDistance / refDistance;

                if (radius == 0)
                {
                    theta = 1;
                }
                else
                {
                    theta = atan(radius) / radius;
                }

                srcX = midX + theta * j * fixZoom;
                srcY = midY + theta * k * fixZoom;

                map_x.at<float>(x, y) = midX + cos(fabs(theta)) * j * fixZoom;

                map_y.at<float>(x, y) = midY + sin(fabs(theta)) * k * fixZoom;
            }
            else
            {
                map_x.at<float>(x, y) = x + cos(fabs(theta));//* fixZoom;//x;
                map_y.at<float>(x, y) = y + sin(fabs(theta));//* fixZoom;//y;       
            }
        }


    }
}

}

void Main (){

Mat map_x, map_y; float qvDepth = 32.0f, fixStrength= 1.5f, fixZoom = 1.0f, lensRadius = 2.0f;

// create destination and the maps

matOutput.create(matInput.size(), matInput.type());
map_x.create(matInput.size(), CV_32FC1/*IMREAD_GRAYSCALE*/);
map_y.create(matInput.size(), CV_32FC1/*IMREAD_GRAYSCALE*/);

ApplyCorrectBarrelDistortion(matInput, 
    map_x, map_y, qvDepth, fixStrength, fixZoom, lensRadius, 3320, 10000);
remap(matInput, matOutput, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
}

test picture : https://ibb.co/mXobiy this code is from this answer : https://stackoverflow.com/questions/3... , but my result is a picture more small ... (more)

edit retag flag offensive close merge delete