将相机标定测量内参矩阵、畸变系数与去畸变返回无畸变图融合
使用时修改dir为存放图片的文件夹名称
修改board_size为行列的角点数
建议目录下存放多几张图片进行去畸变
下一步与去透视变换融合,生成一个矩阵,同时完成去畸变与透视变换。
#include<opencv2/opencv.hpp>
#include <fstream>
#include<iostream>
#include<stdlib.h>
#include<iosfwd>
#include <ostream>
#include <vector>
using namespace cv;
using namespace std;
#define linux 1
vector<string> getFilesList(string dir);
#ifdef linux
#include <memory.h>
#include <dirent.h>
vector<string> getFilesList(string dirpath) {
vector<string> 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<string> 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 <io.h>
vector<string> getFilesList(string dir)
{
vector<string> 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<string> 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); /

本文介绍了一个使用OpenCV进行相机标定的过程,包括提取角点、计算内参矩阵及畸变系数,并实现图像的畸变校正。通过实际代码展示了如何从图像中自动检测角点并进行亚像素级别的精确化。
最低0.47元/天 解锁文章
896

被折叠的 条评论
为什么被折叠?



