关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

c++ 读取 .tfw 文件数据(读取 .tif 影像中的投影信息)

发布时间:2023-06-26 16:00:35

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

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

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


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


#include#include#include#include#include#include#include #include#include#include#include#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;  }   //读取正确匹配特征点   vectoritem; //用于存放文件中的一行数据   string temp; //把文件中的一行数据作为字符串放入容器中    while (getline(ifs, temp)) //利用getline()读取每一行,并放入到 item 中  {  item.push_back(temp);  }   vectorres;   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#include#include#include#include#include#include #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; }

   



/template/Home/leiyu/PC/Static