c++ GDAL 完成PCA
int main()
{
const char* imagePath = "D:/data/before.img";
const char* txtPath = "D:/data/result.txt";
UpdateRasterFile(imagePath, txtPath);
return 0;
}
通过UpdateRasterFile(imagePath, txtPath);完成PCA的计算和输出
一、获取图像信息
void PCAProcess(const char* imagePath, const char* txtPath)
{
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
GDALDataset* Img = (GDALDataset*)GDALOpen(imagePath, GA_Update);
if (Img == NULL)
{
cout << "打开图像" << imagePath << "失败";
return;
}
else
{
cout << "打开图像成功!" << endl;
}
double GeoTransform[6];
Img->GetGeoTransform(GeoTransform);
const char* Project = Img->GetProjectionRef();//读取投影信息
int nBands = Img->GetRasterCount();
int XSize = Img->GetRasterXSize();
int YSize = Img->GetRasterYSize();
int XOff = 0;//偏移
int YOff = 0;
int BufXSize = XSize;//屏幕显示大小
int BufYSize = YSize;
Mat Dataset(nBands, XSize * YSize, CV_8U, Scalar::all(0));//创建一个矩阵
for (int n = 1; n <= nBands; ++n)//逐波段
{
GDALRasterBand* xBand = Img->GetRasterBand(n);//获得第i个波段
unsigned char* pBuffer = (unsigned char*)CPLMalloc(sizeof(unsigned char) * (XSize) * (YSize) * 1);
xBand->RasterIO(GF_Read, XOff, YOff,XSize, YSize, pBuffer, BufXSize, BufYSize, GDT_Byte, 0, 0);
for (int i = 0; i

最低0.47元/天 解锁文章
1810

被折叠的 条评论
为什么被折叠?



