【坐标转换】像素坐标转投影坐标、投影坐标转像素坐标(附有完整代码及测试结果)
本文主要是对像素坐标转投影坐标、投影坐标转像素坐标的原理进行介绍及代码上的实现!!!一、.tif 影像基本信息介绍1、影像借本信息上图是我使用 QGIS 打开的一副遥感影像的基本属性,上图中 (CRS)范围中的数据对应着该遥感影像的左上角和右下角坐标;2、遥感影像左上角、右下角坐标和像素大小、宽度、高度之间的关系二、投影坐标与像素坐标间的转化原理1、像素坐标转投影坐标2、投影坐标转像素坐标三、代码
·
本文主要是对像素坐标转投影坐标、投影坐标转像素坐标的原理进行介绍及代码上的实现!!!
误差基本为零!
坐标转换整套流程包括:像素坐标转投影坐标、投影坐标转大地坐标、大地坐标转空间直角坐标、七参数转换、空间直角坐标转大地坐标、大地坐标转投影坐标、投影坐标转像素坐标; 本人均已实现,且每一个环节都已经过测试、如有需要欢迎在下方留言评论!!!
一、.tif 影像基本信息介绍
1、影像借本信息
上图是我使用 QGIS 打开的一副遥感影像的基本属性,上图中 (CRS)范围中的数据对应着该遥感影像的左上角和右下角坐标;
2、遥感影像左上角、右下角坐标和像素大小、宽度、高度之间的关系
二、投影坐标与像素坐标间的转化原理
1、像素坐标转投影坐标
2、投影坐标转像素坐标
三、代码实现
1、像素坐标转投影坐标
#include<iostream>
#include<fstream>
#include<ogrsf_frmts.h>
#include<ogr_geometry.h>
#include<gdal_priv.h>
#include<gdal.h>
#include "gdalwarper.h"
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
//获取 .tif 图像的投影信息
void getTfw(const char* path, double* geo)
{
//path是.tif的绝对路径
//geo[6]为数组
GDALAllRegister();
GDALDataset* podataset = (GDALDataset*)GDALOpen(path, GA_ReadOnly);
//保存 .tif 文件的投影信息' x=geo[0]、y=geo[3]、像素大小=geo[1]
//double oldGeo[6];
podataset->GetGeoTransform(geo);
}
//像素坐标转投影坐标(基本无误差)源坐标系
void pixelsPoint2ProjectPoint(vector<Point2d>& PixelsPoints, vector<Point2d>& ProjectPoints, string path)
{
//vector<Point2d> res; //保存投影坐标
//获取 .tif 文件的投影信息
getTfw(OLDPATH, oldGeo);
//创建保存投影坐标的文件
//ofstream ofile(".\\oldProjectPoints.txt");
ofstream ofile(path);
if (!ofile.is_open())
{
cout << "打开文件失败!" << endl;
return;
}
for (auto it = PixelsPoints.begin(); it != PixelsPoints.end(); it++)
{
Point2d projectPoint;
projectPoint.x = oldGeo[1] * (*it).x + oldGeo[0];
projectPoint.y = oldGeo[3] - oldGeo[1] * (*it).y;
//保存旧坐标系的投影坐标
ProjectPoints.push_back(projectPoint);
//把旧坐标系的投影坐标保存到 .txt 文件中
ofile << setprecision(20) << projectPoint.x << "," << setprecision(20) << projectPoint.y << endl;
}
ofile.close();
}
2、投影坐标转像素坐标
//获取 .tif 图像的投影信息
void getTfw(const char* path, double* geo)
{
//path是.tif的绝对路径
//geo[6]为数组
GDALAllRegister();
GDALDataset* podataset = (GDALDataset*)GDALOpen(path, GA_ReadOnly);
//保存 .tif 文件的投影信息' x=geo[0]、y=geo[3]、像素大小=geo[1]
//double oldGeo[6];
podataset->GetGeoTransform(geo);
}
//投影坐标转像素坐标(基本无误差)
void projectPoint2PixelsPoint(vector<Point2d>& ProjectPoints, vector<Point2d>& PixelsPoints, string path)
{
//vector<Point2d> res; //保存像素坐标
//获取 .tif 文件的投影信息,应转像素时转一次就够了(影像不同或许需要转两次)
//NEWPATH为tif影像路径,double newGeo[6];
getTfw(NEWPATH, newGeo);
//创建保存投影坐标的文件
//ofstream ofile(".\\newPixelsPoints.txt");
ofstream ofile(path);
if (!ofile.is_open())
{
cout << "打开文件失败!" << endl;
return;
}
for (auto it = ProjectPoints.begin(); it != ProjectPoints.end(); it++)
{
Point2d pixelsPoint;
pixelsPoint.x = ((*it).x - newGeo[0]) / newGeo[1];
pixelsPoint.y = (newGeo[3] - (*it).y) / newGeo[1];
//保存新坐标系的像素坐标
PixelsPoints.push_back(pixelsPoint);
ofile << setprecision(20) << pixelsPoint.x << "," << setprecision(20) << pixelsPoint.y << endl;
}
ofile.close();
}
说明:获取 .tif 影像的投影信息请参考:c++ 读取 .tfw 文件数据(读取 .tif 影像中的投影信息)_一米九零小胖子的博客-CSDN博客_tfw文件
四、测试结果
1、像素坐标
2、像素坐标转投影坐标结果
3、投影坐标转像素坐标结果
由上述转换结果可知,转换误差基本为零!!!
坐标转换整套流程包括:像素坐标转投影坐标、投影坐标转大地坐标、大地坐标转空间直角坐标、七参数转换、空间直角坐标转大地坐标、大地坐标转投影坐标、投影坐标转像素坐标; 本人均已实现,且每一个环节都已经过测试、如有需要欢迎在下方留言评论!!!
更多推荐
所有评论(0)