编程实现BIPBSQBIL三种格式转换教学内容.docx
- 文档编号:17556420
- 上传时间:2023-07-26
- 格式:DOCX
- 页数:17
- 大小:591.08KB
编程实现BIPBSQBIL三种格式转换教学内容.docx
《编程实现BIPBSQBIL三种格式转换教学内容.docx》由会员分享,可在线阅读,更多相关《编程实现BIPBSQBIL三种格式转换教学内容.docx(17页珍藏版)》请在冰点文库上搜索。
编程实现BIPBSQBIL三种格式转换教学内容
编程实现BIP、BSQ、BIL三种格式转换
一、实验目的
理解遥感图像BIP、BIL、BSQ三种格式数据的组织方式,以及它们互相转换原理和方法。
二、实验原理
ENVI栅格图像文件,是以字节数据为单位,再按照指定顺序组织、排列而成,具体有如下三种方式:
1.BIP(像元波段交叉式):
以一对像元为基本单位进行记录;
2.BIL(波段按行交叉格式):
按照扫描行为单位各波段同一扫描行数据依次记录图像按顺序第一个像元所有的波段,接着是第二个像元的所有波段,然后是第三个像元的所有波段,等等,直到像元总数为止;
3.BSQ(波段顺序格式):
以波段为单位,每波段所有扫描行依次记录,每行数据后面紧接着同一波谱波段的下一行数据。
所以实现三种格式的转换,实际上是对图像字节进行重新排序。
三、实验思路
获取图像相关信息
(可通过读取头文件)
根据图像格式读取字节数据
1
对获取的字节数据按照目标文件格式顺
序写入、
保存
四、实验步骤
1.新建项目文件:
本实验选用的语言是C#,开发工具是VisualStudio2010,通过“文件一新建一项目一C#—Windows窗体应用程序”,命名“格式转换”即可;
2.编写代码:
通过实验思路的流程图,我们确定代码也应该分这三个功能来写,对应可以组织为三个大方法,再细分,调用即可。
(1)•编写读取头文件代码:
ENVI中栅格图像被分为两个文件存储,一个
“.HDR”头文件和一个数据文件,头文件存储了图像描述信息,它是一个文本文件,我们可以用System.lO.SreamReader字符读取流按行读取,再用字符
串匹配方法,以读取字段的方式获取图像的行数、列数、波段数、基本数据类型,即可,具体代码可见后文代码附录;
(2).编写数据读取数据文件代码:
图像数据组织方式有BIP、BIL、BSQ
三种,那么对应的也应该有三个方法按照行、列、波段交叉顺序对图像字节数
据进行读取,具体代码见后文;
(3).编写字节数据重排列存储代码:
在读取了图像数据文件后,可以通过
FileStream类按照目标类型格式文件组织顺序,把字节数据写入到文件中保存即可,具体代码见后文。
3.编译与调试:
通过用预先知道内部字节数据的实验文件,进行测试,辅助断点调试,可排除一般问题,接着编译生成目标程序;
4.运行程序查看结果:
通过对多种格式、多种基本数据类型、多种像素大小的文件进行多种方式转换,再利用ENVI打开两个图像查看是否一样,再辅以
“Link”功能看同一像素点值是否相同,这样可以深层确定转换的正确性。
五、结果与分析
1.结果
图像数据基本类型代码,如:
”1”为1字节byte,”2”为2
字节short等
程序运行界面
转换后结果
2.分析总结
(1).读取头文件时,不能通过各字段的现有位,按位读取,应该按行读取,再利用字符串匹配方式,找到指定字段,这样避免字段位变化时读取出错误信息;
(2).再读取数据文件和写入存储时,应该要以基本数据类型长度个字节为
单位进行,不能单纯的以一个字节操作,不然会不具有普遍性,遇到以“int、
float”为基本类型的文件时,就会出错;
(3)•要想节省代码,可先将各种格式数据读取存储为一种格式字节数组中,再从这个格式往其他格式进行转换,这样可以节省不少工作量,但是引入了中间数组,耗费了内存。
(4)
1.读取头文件信息
///
III
读取头文件信息
///
///
vparam
name="strFileName"x/param>
头文件路径和名称
///
vparam
name="iColumnsCount"x/param>
>像素列数
///
vparam
name="iLinesCount"x/param>
像素行数
///
vparam
name="iBandsCount"x/param>
波段数
///
vparam
name="iType">v/param>基本数据类型代码
///
vparam
name="strInterLeave"x/param>
文件组织格式
///
vreturns>v/returns>返回是否读取成功
public
static
boolReadHDRStringstrFileName,out
iLinesCount,
outintiBandsCount,out
intiType,out
outint
StringstrInterLe
intiColumnsCount.
ave)
//初始化各个变量
StreamReaderhdrfile=null;
try
exOf("=")+1,
exOf("=")+1,
hdrfile=newStreamReader(strFileName);
stringcontent="";
while(hdrfile.EndOfStream!
=true)
{//获取像素列数
content=hdrfile.ReadLine();
if(content.Contains("samples"))
{
Stringsamples=content.Substring(content.Indcontent.Length-content.IndexOf("=")-1).Trim();
iColumnsCount=Convert.ToInt32(samples);
System.Console.WriteLine(samples);
break;
}
}
while(hdrfile.EndOfStream!
=true)
{//获取像素行数
content=hdrfile.ReadLine();
(content.Contains("lines"))
Stringlines=content.Substring(content.Ind-content.IndexOf("=")-1).Trim();
iLinesCount=Convert.ToInt32(lines);
System.Console.WriteLine(lines);
break;
!
=true)
while(hdrfile.EndOfStream{//获取波段个数
content=hdrfile.ReadLine();
if(content.Contains("bands"))
{
Stringbands=content.Substring(content.Ind
content.Length-content.IndexOf("=")-1).Trim();
iBandsCount=Convert.ToInt32(bands);
System.Console.WriteLine(bands);
break;
}
}
while(hdrfile.EndOfStream!
=true)
{//获取数据种类
content=hdrfile.ReadLine();
if(content.Contains("datatype"))
{
dexOf("=")+1,
content.Length
Stringtype=content.Substring(content.lncontent.IndexOf("=")-1).Trim();
iType=Convert.ToInt32(type);
System.Console.WriteLine(type);
break;
}
}
while(hdrfile.EndOfStream!
=true)
{//获取数据解译方式
content=hdrfile.ReadLine();
if(content.Contains("interleave"))
{
tent.lndexOf("=")
+1,content.Length
Stringinterleve=content.Substring(con
-content.IndexOf("=")-1).Trim();
strInterLeave=interleve;
System.Console.WriteLine(interleve);
blnSuccess=true;
break;
}
}
}
catch
{//读取失败
hdrfile.Close();
hdrfile.Dispose();
returnfalse;
}
hdrfile.Close();
hdrfile.Dispose();
//关闭文件流,释放内存
returnblnSuccess;
2.读取数据文件进行转换存储
///
///
bip转换为bsq
///
///
name="strInputFile">
源文件名称与路径
///
name="strOutputFile">
目标文件名称与路径
///
name="pixComCounts">
像素行数
///
name="pixLineCounts">
像素列数
///
name="bands">波段数
///
name="type">基本数据类型代码
///
le,int
Open);
publicstatic
pixComCounts,int
boolBipToBsq(stringstrlnputFile,
pixLineCounts,intbands,inttype)
string
boolblnSuccess=true;
FileStreaminputF=newFileStream(strInputFile,
FileStreamoutputF=newFileStream(strOutputFile,
e.CreateNew);
inttotalsize=pixComCounts*pixLineCounts
type;//计算输入文件总字节数
strOutputFi
FileMode.
FileMod
*bands*
if(totalsize!
=inputF.Length)
{
returnfalse;
}
byte[]bts=newbyte[totalsize];
intnum=0,bt;
-1)
while((bt=inputF.ReadByte())>
{//读取出全部字节数据,存储在数组中
bts[num]=(byte)bt;
num++;
}
bands;bandnum++)
pixLineCounts;row++)
for(intbandnum=0;bandnum<
{//读取波段写入
for(introw=0;row<
{//按行写入
Counts;columnum++)
for
{//
*bands*row+
columnum*
type
type;typenum++)
+typenum]);
}
}
}
(intcolumnum=0;columnum 读取列写入 intstartpos=pixComCounts*type *bands+bandnum*type; for(inttypenum=0;typenum< {//读取数据基本单元类型输入数据 outputF.WriteByte(bts[startpos } outputF.Flush();//保存缓存文件 outputF.Close();//关闭撤销变量文件 outputF.Dispose(); inputF.CIose(); inputF.Dispose();returnblnSuccess; } if(totalsize! =inputF.Length) { returnfalse; } byte[]bts=newbyte[totalsize]; intnum=0,bt; while((bt=inputF.ReadByte())>-1) {//读取出全部字节数据,存储在数组中 bts[num]=(byte)bt; num++; } for(introw=0;row {//按行写入数据 for(intbandnum=0;bandnum {//按波段写入数据 intstartpos=pixComCounts*pixLineCount s*type*bandnum+row*pixComCounts*type; for(intcolumnum=0;columnum Counts;columnum++) //写入没一列数据 0;typenum for(inttypenum pe;typenum++) +columnum*type+typenum]); {〃按数据基本单元类型输入数据 outputF.WriteByte(bts[startpos outputF.Dispose(); inputF.Close(); inputF.Dispose();returnblnSuccess; /// III bil转换为bip /// /// name="strlnputFile"> 源文件名称与路径 /// name="strOutputFile"> 目标文件名称与路径 /// name="pixComCounts"> 像素行数 /// name="pixLineCounts"> 像素列数 /// name="bands">波段数 /// name="type">基本数据类型代码 /// public static boolBilToBip(stringstrlnputFile, string pixComCounts. intpixLineCounts,int bands,int type) strOutput { File,int Open); e.CreateNew); bts byte[] intnum=0, newbyte[totalsize]; bt; while((bt=inputF.ReadByte())>-1) {//读取出全部字节数据,存储在数组中 bts[num]=(byte)bt; num++; } for(introw=0;row {//按行写入 for(intcolumnum=0;columnum columnum++) {//读取列写入 bandnum++) *row*bands 置 type;typenum++) +typenum]); {//读取波段写入 +pixComCounts for(intbandnum=0;bandnum intstartpos=pixComCounts*type *type*bandnum+type*columnum;//获取基准位 for(inttypenum=0;typenum< {//读取数据基本单元类型输入数据 outputF.WriteByte(bts[startpos } } } outputF.Close();//关闭撤销变量文件outputF.Dispose(); inputF.Close(); inputF.Dispose(); returnblnSuccess; } 由于篇幅有限只提供这几种可以互相转换的方式的代码
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编程 实现 BIPBSQBIL 格式 转换 教学内容