openMVG例子学习之undistoBrown

这篇博客介绍了如何使用openMVG库进行图像的Brown畸变矫正。通过分析提供的C++代码,展示了从读取图像、设定畸变参数到执行矫正并保存结果的完整流程。代码涉及了灰度、RGB和RGBA三种模式的图像矫正,并利用了进度条显示处理进度。
摘要由CSDN通过智能技术生成

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值