OPENCV相机标定+去畸变

 将相机标定测量内参矩阵、畸变系数与去畸变返回无畸变图融合

使用时修改dir为存放图片的文件夹名称

修改board_size为行列的角点数

建议目录下存放多几张图片进行去畸变

下一步与去透视变换融合,生成一个矩阵,同时完成去畸变与透视变换。

#include
#include 
#include 
#include
#include
#include  
#include 
using namespace cv;
using namespace std;
#define linux 1


vector getFilesList(string dir);
#ifdef linux
#include 
#include 
vector getFilesList(string dirpath) {
	vector allPath;
	DIR *dir = opendir(dirpath.c_str());
	if (dir == NULL)
	{
		cout << "opendir error" << endl;
		return allPath;
	}
	struct dirent *entry;
	while ((entry = readdir(dir)) != NULL)
	{
		if (entry->d_type == DT_DIR) {//It's dir
			if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
				continue;
			string dirNew = dirpath + "/" + entry->d_name;
			vector tempPath = getFilesList(dirNew);
			allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());

		}
		else {
			//cout << "name = " << entry->d_name << ", len = " << entry->d_reclen << ", entry->d_type = " << (int)entry->d_type << endl;
			string name = entry->d_name;
			string imgdir = dirpath + "/" + name;
			//sprintf("%s",imgdir.c_str());
			allPath.push_back(imgdir);
		}

	}
	closedir(dir);
	//system("pause");
	return allPath;
}
#endif

#ifdef _WIN32//__WINDOWS_
#include 
vector getFilesList(string dir)
{
	vector allPath;
	// 在目录后面加上"\\*.*"进行第一次搜索
	string dir2 = dir + "\\*.*";

	intptr_t handle;
	_finddata_t findData;

	handle = _findfirst(dir2.c_str(), &findData);
	if (handle == -1) {// 检查是否成功
		cout << "can not found the file ... " << endl;
		return allPath;
	}
	while (_findnext(handle, &findData) == 0)
	{
		if (findData.attrib & _A_SUBDIR) 是否含有子目录
		{
			//若该子目录为"."或"..",则进行下一次循环,否则输出子目录名,并进入下一次搜索
			if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
				continue;
			// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
			string dirNew = dir + "\\" + findData.name;
			vector tempPath = getFilesList(dirNew);
			allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
		}
		else //不是子目录,即是文件,则输出文件名和文件的大小
		{
			string filePath = dir + "\\" + findData.name;
			allPath.push_back(filePath);
		}
	}
	_findclose(handle);    // 关闭搜索句柄
	return allPath;
}
#endif


cv::Mat  NewCameraMatrix;


int main() {
    string dir = "../img6";


    //读取每一幅图像,从中提取出角点,然后对角点进行亚像素精确化
    cout << "开始提取角点………………";
    int image_count = 0;  /* 图像数量 */
    Size image_size;  /* 图像的尺寸 */
    Size board_size = Size(6, 8);   //6 4 10 7 9 6   69 68 68/* 标定板上每行、列的角点数 */
    vector image_points_buf;  /* 缓存每幅图像上检测到的角点 */
    vector> image_points_seq; /* 保存检测到的所有角点 */
    string filename;
    vector allFileList = getFilesList(dir);
    int pic_num = allFileList.size();
    string pic_name[pic_num];
    for (size_t i = 0; i < allFileList.size(); i++)

        pic_name[i] = allFileList.at(i);


    while (image_count < pic_num) {
        filename = pic_name[image_count];
        image_count++;
        // 用于观察检验输出
        cout << "image_count = " << image_count << endl;
        /* 输出检验*/
        Mat imageInput = imread(filename);
        if (image_count == 1)  //读入第一张图片时获取图像宽高信息
        {
            image_size.width = imageInput.cols;
            image_size.height = imageInput.rows;
            cout << "image_size.width = " << image_size.width << endl;
            cout << "image_size.height = " << image_size.height << endl;
        }

        /* 提取角点 */
        if (0 == findChessboardCorners(imageInput, board_size, image_points_buf)) {
            cout << "can not find chessboard corners!\n"; //找不到角点
            exit(1);
        } else {
            Mat view_gray;
            cvtColor(imageInput, view_gray, COLOR_RGB2GRAY);
            /* 亚像素精确化 */
            find4QuadCornerSubpix(view_gray, image_points_buf, Size(11, 11)); //对粗提取的角点进行精确化
            image_points_seq.push_back(image_points_buf);  //保存亚像素角点
            /* 在图像上显示角点位置 */
            drawChessboardCorners(view_gray, board_size, image_points_buf, true); //用于在图片中标记角点
//            imshow("Camera Calibration", view_gray);//显示图片
//            waitKey(50);//暂停0.5S
        }
    }
    int total = image_points_seq.size();
    cout << "total = " << total << endl;
    cout << "角点提取完成!\n";
    //以下是摄像机标定
    cout << "开始标定………………";
    /*棋盘三维信息*/
    Size square_size = Size(10, 10);  /* 实际测量得到的标定板上每个棋盘格的大小 */
    vector> object_points; /* 保存标定板上角点的三维坐标 */
    /*内外参数*/
    Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 摄像机内参数矩阵 */
    vector point_counts;  // 每幅图像中角点的数量
    Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0)); /* 摄像机的5个畸变系数:k1,k2,p1,p2,k3 */
    vector tvecsMat;  /* 每幅图像的旋转向量 */
    vector rvecsMat; /* 每幅图像的平移向量 */
    /* 初始化标定板上角点的三维坐标 */
    int i, j, t;
    for (t = 0; t < image_count; t++) {
        vector tempPointSet;
        for (i = 0; i < board_size.height; i++) {
            for (j = 0; j < board_size.width; j++) {
                Point3f realPoint;
                /* 假设标定板放在世界坐标系中z=0的平面上 */
                realPoint.x = i * square_size.width;
                realPoint.y = j * square_size.height;
                realPoint.z = 0;
                tempPointSet.push_back(realPoint);
            }
        }
        object_points.push_back(tempPointSet);
    }
    /* 初始化每幅图像中的角点数量,假定每幅图像中都可以看到完整的标定板 */
    for (i = 0; i < image_count; i++) {
        point_counts.push_back(board_size.width * board_size.height);
    }
    /* 开始标定 */
    calibrateCamera(object_points, image_points_seq, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, 0);
    cout << "标定完成!\n";
    //对标定结果进行评价
    cout << "开始评价标定结果………………\n";
    double total_err = 0.0; /* 所有图像的平均误差的总和 */
    double err = 0.0; /* 每幅图像的平均误差 */
    vector image_points2; /* 保存重新计算得到的投影点 */
    for (i = 0; i < image_count; i++) {
        vector tempPointSet = object_points[i];
        /* 通过得到的摄像机内外参数,对空间的三维点进行重新投影计算,得到新的投影点 */
        projectPoints(tempPointSet, rvecsMat[i], tvecsMat[i], cameraMatrix, distCoeffs, image_points2);
        /* 计算新的投影点和旧的投影点之间的误差*/
        vector tempImagePoint = image_points_seq[i];
        Mat tempImagePointMat = Mat(1, tempImagePoint.size(), CV_32FC2);
        Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);
        for (int j = 0; j < tempImagePoint.size(); j++) {
            image_points2Mat.at(0, j) = Vec2f(image_points2[j].x, image_points2[j].y);
            tempImagePointMat.at(0, j) = Vec2f(tempImagePoint[j].x, tempImagePoint[j].y);
        }
        err = norm(image_points2Mat, tempImagePointMat, NORM_L2);
        total_err += err /= point_counts[i];
    }
    std::cout << "总体平均误差:" << total_err / image_count << "像素" << endl;
    std::cout << "评价完成!" << endl;
    Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 保存每幅图像的旋转矩阵 */
    cout << "相机内参数矩阵:" << endl;
    cout << cameraMatrix << endl << endl;
    cout << "畸变系数:\n";
    cout << distCoeffs << endl << endl << endl;


    Mat K = cameraMatrix;
    Mat D = distCoeffs;
//D.at(0,4)=0;

    const int nImage = pic_num;
    int ImgWidth = image_size.width;
    int ImgHeight = image_size.height;





/***************************************************/
    double fx = K.at(0, 0)
    , fy = K.at(1, 1)
    , ux = K.at(0, 2)
    , uy = K.at(1, 2);
    double k1 = D.at(0, 0), k2 = D.at(0, 1), k3 = D.at(0, 4), p1 = D.at(0,
                                                                                                        2), p2 = D.at(
            0, 3);
    double k4 = 0, k5 = 0, k6 = 0;


/**********************way2***********************/


//    Mat out2 = Mat(ImgHeight, ImgWidth, CV_8UC1);


    for (int i = 0; i < nImage; i++) {
        string InputPath = pic_name[i];
        cv::Mat img_tmp = cv::imread(InputPath);
        cvtColor(img_tmp, img_tmp, COLOR_RGB2GRAY);


//        ///**********way2**********/

        int max_x=-9999999,max_y=-9999999,min_x=9999999,min_y=9999999;
        double point_xy[4][2];//左上 右上,左下,右下
        for (int i = 0; i < ImgHeight; i++) {
            for (int j = 0; j < ImgWidth; j++) {
                double xDistortion = (j - ux) / fx;
                double yDistortion = (i - uy) / fy;

                double xCorrected, yCorrected;

                double x0 = xDistortion;
                double y0 = yDistortion;
                for (int j = 0; j < 10; j++) {
                    double r2 = xDistortion * xDistortion + yDistortion * yDistortion;

                    double distRadialA = 1 / (1. + k1 * r2 + k2 * r2 * r2 + k3 * r2 * r2 * r2);
                    double distRadialB = 1. + k4 * r2 + k5 * r2 * r2 + k6 * r2 * r2 * r2;

                    double deltaX = 2. * p1 * xDistortion * yDistortion + p2 * (r2 + 2. * xDistortion * xDistortion);
                    double deltaY = p1 * (r2 + 2. * yDistortion * yDistortion) + 2. * p2 * xDistortion * yDistortion;

                    xCorrected = (x0 - deltaX) * distRadialA * distRadialB;
                    yCorrected = (y0 - deltaY) * distRadialA * distRadialB;

                    xDistortion = xCorrected;
                    yDistortion = yCorrected;
                }
                float kk=1.5;
                xCorrected = xCorrected * fx + ux;
                yCorrected = yCorrected * fy + uy;
                if(yCorrected>max_y&&abs(yCorrected)max_x&&abs(xCorrected)ImgWidth*kk||abs(yCorrected)>ImgHeight)
                {
                    img_tmp.at(i,j)=127;
                }

            }


        }
        cout<<"size:   "<(i, j) = 0;


        for (int i = -move_y; i < ImgHeight_out; i++) {
            for (int j = -move_x; j < ImgWidth_out; j++) {
                double xCorrected = (j - ux) / fx;
                double yCorrected = (i - uy) / fy;

                double xDistortion, yDistortion;

                //我们已知的是经过畸变矫正或理想点的坐标;
                double r2 = xCorrected * xCorrected + yCorrected * yCorrected;

                double deltaRa = 1. + k1 * r2 + k2 * r2 * r2 + k3 * r2 * r2 * r2;
                double deltaRb = 1 / (1. + k4 * r2 + k5 * r2 * r2 + k6 * r2 * r2 * r2);
                double deltaTx = 2. * p1 * xCorrected * yCorrected + p2 * (r2 + 2. * xCorrected * xCorrected);
                double deltaTy = p1 * (r2 + 2. * yCorrected * yCorrected) + 2. * p2 * xCorrected * yCorrected;

                //下面为畸变模型;
                xDistortion = xCorrected * deltaRa * deltaRb + deltaTx;
                yDistortion = yCorrected * deltaRa * deltaRb + deltaTy;

                //最后再次通过相机模型将归一化的坐标转换到像素坐标系下;
                xDistortion = xDistortion * fx + ux;
                yDistortion = yDistortion * fy + uy;
                if (yDistortion >= 0 && yDistortion < ImgHeight && xDistortion >= 0 && xDistortion < ImgWidth)
                    out1.at(i+move_y, j+move_x) = img_tmp.at(yDistortion, xDistortion);

            }
        }

        cv::imshow("ou1", out1);

        cv::imshow("RawImage", img_tmp);
        cv::waitKey(0);
    }

}

你可能感兴趣的:(智能车视觉,OPENCV)