使用opencv 3.1.0进行鱼眼校正

时间:2016-11-07 10:55:29

标签: opencv fisheye

我使用以下代码校准鱼眼镜头。 重映射调用后,输出图像非常小I have attached a sample images and it's outputs 使用15个图像来训练校准。 我在这里看到一个类似的问题OpenCV fisheye undistort issues,但同样的解决方案对我没用。

我不确定我做了什么错误。

#include "opencv2/opencv.hpp"
#include <vector>
#include <string>
#include <time.h>
#include <math.h>
#include <ctype.h>
#include <numeric> 

#include <cv.h>
#include <highgui.h>
#include <fstream> 


using namespace cv;
using namespace std;

int fishEye = 1;

int main() {
    int numBoards = 0;
    int numCornersHor = 3;
    int numCornersVer = 3;
    std::ifstream myfile;
    myfile.open ("file.list");
    std::string filename;
    namedWindow("win2", WINDOW_NORMAL);
    namedWindow("input", WINDOW_NORMAL);
    namedWindow("output", WINDOW_NORMAL);
    namedWindow("ImageView", WINDOW_NORMAL);
    namedWindow("Input", WINDOW_NORMAL);

    int numSquares = numCornersHor * numCornersVer;
    Size board_sz = Size(numCornersHor, numCornersVer);

    vector<vector<Point3f>> object_points;
    vector<vector<Point2f>> image_points;

    vector<Point2f> corners;
    int successes=0;

    Mat image[25];
    Mat gray_image;
    int i = 0;
    while (myfile >> filename) {
       image[i] = imread(filename, CV_LOAD_IMAGE_COLOR);   
       i++;numBoards++;
       std::cout << "file names are " << filename << std::endl;
    }

    vector<Point3f> obj;
    for(int j=0;j<numSquares;j++)
        obj.push_back(Point3f(j/numCornersHor, j%numCornersHor, 0.0f));


    i = 0;
    while(successes < numBoards) {
        cvtColor(image[i], gray_image, CV_BGR2GRAY);

        bool found = findChessboardCorners(image[i], board_sz, corners, CV_CALIB_CB_ADAPTIVE_THRESH + CV_CALIB_CB_FILTER_QUADS );

        if(found) {
            cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
            drawChessboardCorners(gray_image, board_sz, corners, found);
            image_points.push_back(corners);
            object_points.push_back(obj);
            printf("Snap stored!");
            successes++;
         } else {
           cout << "didnt find chessboard\n";
         }

         std::cout << "displaying image idx "<< i << std::endl;
         imshow("win2", gray_image);
         waitKey(0);

         if(successes>=numBoards)
              break;
         i++;
    }

     std::cout << "finished checkerboard inputs\n";
     Mat intrinsic = Mat::eye(3, 3, CV_64F);
     Mat distCoeffs = Mat::ones(1, 4, CV_64F);
     vector<Mat> rvecs;
     vector<Mat> tvecs;

     intrinsic = (Mat_<double> (3,3)  << 301.9876398926938, 0, 318.4379656472112, 
                                   0, 358.4785204130175, 264.5157579802344, 
                                   0, 0, 1);
     distCoeffs = (Mat_<double> (4,1) <<  -0.04151489617936081 , -0.03302245034149642, 0.05356350135813352, -0.02297579335011486 );

     if (fishEye == 0) {
         calibrateCamera(object_points, image_points, image[0].size(), intrinsic, distCoeffs, rvecs, tvecs);
      } else {
         int flag = cv::fisheye::CALIB_FIX_SKEW;
         cv::fisheye::calibrate(object_points, image_points, image[0].size(), intrinsic, distCoeffs, rvecs, tvecs, flag);
      }

      std::cout << "intrinsic" << intrinsic << std::endl;
      std::cout << "distCoeffs" << distCoeffs << std::endl; 


      Mat output;
      Mat newK;
      Mat view, map1, map2;
      Mat identity = Mat::eye(3, 3, CV_64F);
      Size newSize(640, 480);
      Mat rview(newSize, image[0].type());
      Mat imageUndistorted;
      i = 0;
      while(1) {
          if (fishEye == 0) {
             undistort(image[i], imageUndistorted, intrinsic, distCoeffs);
             imshow("input", image[i]);
             imshow("output", imageUndistorted);
        } else {

             fisheye::estimateNewCameraMatrixForUndistortRectify(intrinsic, distCoeffs, image[i].size(), Matx33d::eye(), newK, 1);
           fisheye::initUndistortRectifyMap(intrinsic, distCoeffs, Matx33d::eye(), newK, image[i].size(), CV_16SC2, map1, map2);
           remap(image[i], rview, map1, map2, INTER_LINEAR);
           //fisheye::undistortImage(image[i], output, intrinsic, distCoeffs, identity);

           imshow("ImageView", rview);
           imshow("Input", image[i]);
           //imshow("output", output);
        }

        waitKey(0);
        i++;
        if (i >= numBoards) i = 0;
     }
     return 0;
  }

0 个答案:

没有答案