openMVG例子学习之undistoBrown
写在前面
这个代码来自于\openMVG\src\openMVG_Samples当中
似乎这些代码在安装时cmake当中已经生成过
这里主要来对源码的主要逻辑进行分析。
/*功能:
实现不同模式(灰度,RGB,RGBA)图片下的畸变矫正
输入:
图片路径、畸变中心c、畸变因子k、焦距f
输出:
把矫正好的图片写入到输出文件中
注意:
可以在控制台实现进度条功能
代码核心为最后的一段循环
*/
#include "openMVG/cameras/Camera_Pinhole_Radial.hpp"
#include "openMVG/cameras/Camera_undistort_image.hpp"
#include "openMVG/image/image_io.hpp"
#include "third_party/cmdLine/cmdLine.h"
#include "third_party/progress/progress_display.hpp"
#include "third_party/stlplus3/filesystemSimplified/file_system.hpp"
#include <cstdlib>
#include <iostream>
#include <string>
int main()
{
std::string sPath;
std::string sOutPath;
// 临时储存Brown's 畸变模型
openMVG::Vec2 c; // 畸变中心
openMVG::Vec3 k; // 畸变因子
double f = 0;// 焦距
std::string suffix = "JPG";
if (sOutPath == sPath)
{
std::cerr << "输入和输出路径设置为相同的值。" << std::endl;
return 1;
}
//如果该路径不存在就创建一个
if (!stlplus::folder_exists(sOutPath))
stlplus::folder_create(sOutPath);
std::cout << "Used Brown's distortion model values: \n"
<< " Distortion center: " << c.transpose()/*转置*/ << "\n"
<< " Distortion coefficients (K1,K2,K3): "
<< k.transpose() << "\n"
<< " Distortion focal: " << f << std::endl;
const std::vector<std::string> vec_fileNames =
stlplus::folder_wildcard(sPath, "*." + suffix, false, true);
//folder_wildcard如果文件夹及其文件存在就返回true
std::cout << "\nLocated " << vec_fileNames.size() << " files in " << sPath
<< " with suffix " << suffix;
//创建进度条
/*
my_progress_bar(预计的最大值)
++my_progress_bar; 进行增加,判定是否绘制在控制台上
*/
C_Progress_display my_progress_bar(vec_fileNames.size());
for (size_t j = 0; j < vec_fileNames.size(); ++j, ++my_progress_bar)
{
//read the depth
int w, h, depth;
std::vector<unsigned char> tmp_vec;
const std::string sOutFileName =
stlplus::create_filespec(sOutPath, stlplus::basename_part(vec_fileNames[j]), "png");
const std::string sInFileName = stlplus::create_filespec(sPath, stlplus::filename_part(vec_fileNames[j]));
const int res = openMVG::image::ReadImage(sInFileName.c_str(), &tmp_vec, &w, &h, &depth);
const openMVG::cameras::Pinhole_Intrinsic_Radial_K3 cam(w, h, f, c(0), c(1), k(0), k(1), k(2));
if (res == 1)
{
switch (depth)
{
//不同情况下修正畸变影像
case 1: //Greyscale
{
openMVG::image::Image<unsigned char> imageGreyIn, imageGreyU;
imageGreyIn = Eigen::Map<openMVG::image::Image<unsigned char>::Base>
(&tmp_vec[0], h, w);
openMVG::cameras::UndistortImage(imageGreyIn, &cam, imageGreyU);
WriteImage(sOutFileName.c_str(), imageGreyU);
break;
}
case 3: //RGB
{
openMVG::image::Image<openMVG::image::RGBColor> imageRGBIn, imageRGBU;
imageRGBIn = Eigen::Map<openMVG::image::Image<openMVG::image::RGBColor>::Base>
((openMVG::image::RGBColor*)&tmp_vec[0], h, w);
openMVG::cameras::UndistortImage(imageRGBIn, &cam, imageRGBU);
WriteImage(sOutFileName.c_str(), imageRGBU);
break;
}
case 4: //RGBA
{
openMVG::image::Image<openMVG::image::RGBAColor> imageRGBAIn, imageRGBAU;
imageRGBAIn = Eigen::Map<openMVG::image::Image<openMVG::image::RGBAColor>::Base>
((openMVG::image::RGBAColor*)&tmp_vec[0], h, w);
openMVG::cameras::UndistortImage(imageRGBAIn, &cam, imageRGBAU);
WriteImage(sOutFileName.c_str(), imageRGBAU);
break;
}
}
}//end if res==1
else
{
std::cerr << "\nThe image contains " << depth << "layers. This depth is not supported!\n";
}
} //每个文件的结束循环
return 0;
}