OpenCV fisheye calibration cuts too much of the resulting image

前端 未结 5 781
长情又很酷
长情又很酷 2020-12-03 15:28

I am using OpenCV to calibrate images taken using cameras with fish-eye lenses.

The functions I am using are:

  • findChessboardCorners(...);
5条回答
  •  隐瞒了意图╮
    2020-12-03 16:03

    As mentioned by Paul Bourke here:

    a fisheye projection is not a "distorted" image, and the process isn't a "dewarping". A fisheye like other projections is one of many ways of mapping a 3D world onto a 2D plane, it is no more or less "distorted" than other projections including a rectangular perspective projection

    To get a projection without image cropping, (and your camera has ~180 degrees FOV) you can project the fisheye image in a square using something like this:

    Source code:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    // - compile with:
    // g++ -ggdb `pkg-config --cflags --libs opencv` fist2rect.cpp -o fist2rect
    // - execute:
    // fist2rect input.jpg output.jpg
    
     using namespace std;
     using namespace cv;
     #define PI 3.1415926536
    
     Point2f getInputPoint(int x, int y,int srcwidth, int srcheight)
     {
        Point2f pfish;
        float theta,phi,r, r2;
        Point3f psph;
        float FOV =(float)PI/180 * 180;
        float FOV2 = (float)PI/180 * 180;
        float width = srcwidth;
        float height = srcheight;
    
        // Polar angles
        theta = PI * (x / width - 0.5); // -pi/2 to pi/2
        phi = PI * (y / height - 0.5);  // -pi/2 to pi/2
    
        // Vector in 3D space
        psph.x = cos(phi) * sin(theta);
        psph.y = cos(phi) * cos(theta);
        psph.z = sin(phi) * cos(theta);
    
        // Calculate fisheye angle and radius
        theta = atan2(psph.z,psph.x);
        phi = atan2(sqrt(psph.x*psph.x+psph.z*psph.z),psph.y);
    
        r = width * phi / FOV;
        r2 = height * phi / FOV2;
    
        // Pixel in fisheye space
        pfish.x = 0.5 * width + r * cos(theta);
        pfish.y = 0.5 * height + r2 * sin(theta);
        return pfish;
    }
    int main(int argc, char **argv)
    {
        if(argc< 3)
            return 0;
        Mat orignalImage = imread(argv[1]);
        if(orignalImage.empty())
        {
            cout<<"Empty image\n";
            return 0;
        }
        Mat outImage(orignalImage.rows,orignalImage.cols,CV_8UC3);
    
        namedWindow("result",CV_WINDOW_NORMAL);
    
        for(int i=0; i= orignalImage.cols || inP2.y >= orignalImage.rows)
                    continue;
    
                if(inP2.x < 0 || inP2.y < 0)
                    continue;
                Vec3b color = orignalImage.at(inP2);
                outImage.at(Point(i,j)) = color;
    
            }
        }
    
        imwrite(argv[2],outImage);
    
    }
    

提交回复
热议问题