void CBandMath::NDVICal(CString m_pathname,int Bands,int Width,int Height,int DataType)
{
	int fRed,fNearRed;

	if(DataType==1)//TM
	{
		fRed=3;
		fNearRed=5;
	}
	
	CString m_OutFile;
	
	float *NDVIWr = new float[Height*Width];
	unsigned char *Red         = new unsigned char[Height*Width];
	unsigned char *NearRed     = new unsigned char[Height*Width];

	CFile fReadFile,fWriteFile;
	if(!fReadFile.Open(m_pathname,CFile::modeReadWrite))            //打开图像文件(参数)
	{
		AfxMessageBox("不能打开所读文件!");  
		return;
	}
	    
	//打开写入文件(其它类的变量)
	if(!fWriteFile.Open('001',CFile::modeCreate|CFile::modeWrite))
	{
		AfxMessageBox("请选择输出文件!");  
		return;
	}
	
	fReadFile.Seek((fRed-1)*Height*Width,CFile::begin);
	fReadFile.Read(Red,Height*Width);
	
	fReadFile.Seek((fNearRed-1)*Height*Width,CFile::begin);
	fReadFile.Read(NearRed,Height*Width);          
	
	for(int k=0;k<Height*Width;k++)
		NDVIWr[k]=((float)NearRed[k]-Red[k])/(NearRed[k]+Red[k]);
	
	fWriteFile.Write(NDVIWr,sizeof(float)*Height*Width);

	delete []NDVIWr;               
	delete []Red ;      
	delete []NearRed ;  
	
	fReadFile.Close();
	fWriteFile.Close();
}

将ndvi处理后结果写入输出文件
'001'的band是多少内容:无法确定,因为代码中没有明确指定输出文件的波段数。

该代码演示了如何从TM图像中提取NDVI数据并将其写入文件。代码使用C++编写,并使用CFile类进行文件操作。代码中定义了NDVICal函数,用于计算NDVI并写入文件。

代码首先根据DataType判断数据类型,并根据TM数据类型设置红波段和近红外波段的索引。然后,代码打开输入图像文件和输出文件。从输入文件中读取红波段和近红外波段数据,并计算NDVI值。最后,将NDVI值写入输出文件。

代码中使用的输出文件名'001',但没有明确指定输出文件的波段数。因此,无法确定'001'的波段数。

NDVI计算 - 从TM图像提取NDVI数据并写入文件

原文地址: https://www.cveoy.top/t/topic/jls0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录