关于读取 .tif 影像的投影信息,下面介绍了两种方法:

        1、按读取 .txt 的方法读取 .tfw 文件;

        2、使用 GDAL 直接从 .tif 影像中读取投影信息!

1、按读取 .txt 的方法读取 .tfw 文件

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <cmath>

#include <opencv2\opencv.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include "opencv2\imgproc\types_c.h"

using namespace std;
using namespace cv;


int main() 
{
	ifstream ifs;							//创建流对象

	ifs.open(".\\wgs.tfw", ios::in);		//打开文件

	if (!ifs.is_open())						//判断文件是否打开
	{
		cout << "打开文件失败!!!";
		return 0;
	}

	//读取正确匹配特征点

	vector<string> item;				//用于存放文件中的一行数据

	string temp;						//把文件中的一行数据作为字符串放入容器中


	while (getline(ifs, temp))          //利用getline()读取每一行,并放入到 item 中
	{
		item.push_back(temp);
	}

	vector<double> res;

	for (int i = 0; i < item.size(); i++)
	{
		//数据类型转换,将string类型转换成float,如果字符串不是有数字组成,q则字符被转化为 0
		res.emplace_back(atof(item[i].c_str()));
	}

	for (double num : res)
		cout << num << endl;

	system("pause");
	return 0;
}

2、使用 GDAL 直接从 .tif 影像中读取投影信息

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <cmath>

#include"gdal_priv.h"

using namespace std;



int main() 
{
	GDALAllRegister();

	GDALDataset* podataset = (GDALDataset*)GDALOpen("..\\WGS84\\wgs.tif", GA_ReadOnly);
    
    //保存 .tif 文件的投影信息' x=geo[0]、y=geo[3]、像素大小=geo[1]
	double geo[6];

	podataset->GetGeoTransform(geo);

	for (int i = 0; i < 6; i++)
		cout << geo[i] << endl;

	system("pause");
	return 0;
}

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐