编程实现BIP、BSQ、BIL三种格式转换(共10页)_第1页
编程实现BIP、BSQ、BIL三种格式转换(共10页)_第2页
编程实现BIP、BSQ、BIL三种格式转换(共10页)_第3页
编程实现BIP、BSQ、BIL三种格式转换(共10页)_第4页
编程实现BIP、BSQ、BIL三种格式转换(共10页)_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上编程实现BIP、BSQ、BIL三种格式转换一、 实验目的理解遥感图像BIP、BIL、BSQ三种格式数据的组织方式,以及它们互相转换原理和方法。二、 实验原理 ENVI栅格图像文件,是以字节数据为单位,再按照指定顺序组织、排列而成,具体有如下三种方式:1.BIP(像元波段交叉式):以一对像元为基本单位进行记录 ;2.BIL(波段按行交叉格式):按照扫描行为单位各波段同一扫描行数据依次记录, 图像按顺序第一个像元所有的波段,接着是第二个像元的所有波段,然后是第三个像元的所有波段,等等,直到像元总数为止;3.BSQ(波段顺序格式):以波段为单位,每波段所有扫描行依次记录,

2、每行数据后面紧接着同一波谱波段的下一行数据。 所以实现三种格式的转换,实际上是对图像字节进行重新排序。三、 实验思路获取图像相关信息(可通过读取头文件) 根据图像格式读取字节数据对获取的字节数据按照目标文件格式顺序写入、保存四、 实验步骤1 新建项目文件:本实验选用的语言是C#,开发工具是VisualStudio2010,通过“文件新建项目C#Windows窗体应用程序”,命名“格式转换”即可;2 编写代码:通过实验思路的流程图,我们确定代码也应该分这三个功能来写,对应可以组织为三个大方法,再细分,调用即可。(1).编写读取头文件代码:ENVI中栅格图像被分为两个文件存储,一个“.HDR”头文

3、件和一个数据文件,头文件存储了图像描述信息,它是一个文本文件,我们可以用System.IO.SreamReader字符读取流按行读取,再用字符串匹配方法,以读取字段的方式获取图像的行数、列数、波段数、基本数据类型,即可,具体代码可见后文代码附录; (2).编写数据读取数据文件代码:图像数据组织方式有BIP、BIL、BSQ三种,那么对应的也应该有三个方法按照行、列、波段交叉顺序对图像字节数据进行读取,具体代码见后文; (3).编写字节数据重排列存储代码:在读取了图像数据文件后,可以通过FileStream类按照目标类型格式文件组织顺序,把字节数据写入到文件中保存即可,具体代码见后文。3. 编译与

4、调试:通过用预先知道内部字节数据的实验文件,进行测试,辅助断点调试,可排除一般问题,接着编译生成目标程序;4. 运行程序查看结果:通过对多种格式、多种基本数据类型、多种像素大小的文件进行多种方式转换,再利用ENVI打开两个图像查看是否一样,再辅以“Link”功能看同一像素点值是否相同,这样可以深层确定转换的正确性。五、 结果与分析1. 结果图像数据基本类型代码,如:”1”为1字节byte,”2”为2字节short等程序运行界面LinK确定相同位置数据一致目标格式(BIL)源格式(BSQ)转换后结果2. 分析总结 (1).读取头文件时,不能通过各字段的现有位,按位读取,应该按行读取,再利用字符串

5、匹配方式,找到指定字段,这样避免字段位变化时读取出错误信息;(2).再读取数据文件和写入存储时,应该要以基本数据类型长度个字节为单位进行,不能单纯的以一个字节操作,不然会不具有普遍性,遇到以“int、float”为基本类型的文件时,就会出错;(3).要想节省代码,可先将各种格式数据读取存储为一种格式字节数组中,再从这个格式往其他格式进行转换,这样可以节省不少工作量,但是引入了中间数组,耗费了内存。六、 源代码附录1.读取头文件信息  / <summary>     / 读取头文件信息  

6、60;  / </summary>     / <param name="strFileName"></param>头文件路径和名称     / <param name="iColumnsCount"></param>像素列数     / <param name="iLinesCou

7、nt"></param>像素行数     / <param name="iBandsCount"></param>波段数     / <param name="iType"></param>基本数据类型代码     / <param name="strInterLeave"&g

8、t;</param>文件组织格式     / <returns></returns>返回是否读取成功     public static bool ReadHDR(String strFileName,out int iColumnsCount,out int iLinesCount,out int iBandsCount,out int iType,

9、out String strInterLeave)           bool blnSuccess = false;       iColumnsCount = -1;       iLinesCount = -1;   &#

10、160;   iBandsCount = -1;       iType = -1;       strInterLeave = ""       /初始化各个变量       StreamReader

11、0;hdrfile=null;       try                 hdrfile = new StreamReader(strFileName);          string content=&qu

12、ot;"          while(hdrfile.EndOfStream!=true)          /获取像素列数             content = hdrfile.ReadLine();  

13、60;          if(content.Contains("samples")                              String sampl

14、es = content.Substring(content.IndexOf("=") + 1, content.Length - content.IndexOf("=") -1).Trim();                 iColumnsCount = Convert.ToInt3

15、2(samples);                 System.Console.WriteLine(samples);                 break;       

16、60;                                 while (hdrfile.EndOfStream != true)        &

17、#160;    /获取像素行数               content = hdrfile.ReadLine();               if (content.Contains("lines")&

18、#160;                                String lines = content.Substring(content.IndexOf("=") + 1, cont

19、ent.Length - content.IndexOf("=") - 1).Trim();                  iLinesCount = Convert.ToInt32(lines);           

20、60;      System.Console.WriteLine(lines);                  break;                   &

21、#160;                        while (hdrfile.EndOfStream != true)              /获取波段个数 

22、0;              content = hdrfile.ReadLine();                if (content.Contains("bands")      

23、;                            String bands = content.Substring(content.IndexOf("=") + 1, content.Length - content.In

24、dexOf("=") - 1).Trim();                  iBandsCount = Convert.ToInt32(bands);                 

25、 System.Console.WriteLine(bands);                  break;                        

26、0;                     while (hdrfile.EndOfStream != true)               /获取数据种类     

27、             content = hdrfile.ReadLine();                  if (content.Contains("data type")   

28、0;                                 String type = content.Substring(content.IndexOf("=") + 1, co

29、ntent.Length - content.IndexOf("=") - 1).Trim();                   iType = Convert.ToInt32(type);           &#

30、160;       System.Console.WriteLine(type);                   break;                 &

31、#160;                               while (hdrfile.EndOfStream != true)          

32、;       /获取数据解译方式                  content = hdrfile.ReadLine();                 

33、; if (content.Contains("interleave")                                      String inte

34、rleve = content.Substring(content.IndexOf("=") + 1, content.Length - content.IndexOf("=") - 1).Trim();                    strInterLeave

35、 = interleve;                    System.Console.WriteLine(interleve);                    b

36、lnSuccess = true;                    break;                         &#

37、160;                                 catch            /读取失败  

38、0;             hdrfile.Close();                hdrfile.Dispose();               

39、 return false;                        hdrfile.Close();            hdrfile.Dispose();    &#

40、160;       /关闭文件流,释放内存            return blnSuccess;      2.读取数据文件进行转换存储 / <summary>    / bip转换为bsq    / </summ

41、ary>    / <param name="strInputFile"></param>源文件名称与路径    / <param name="strOutputFile"></param>目标文件名称与路径    / <param name="pixComCounts"></param

42、>像素行数    / <param name="pixLineCounts"></param>像素列数    / <param name="bands"></param>波段数    / <param name="type"></param>基本数据类型代码  

43、  / <returns></returns>是否转换成功    public static bool BipToBsq(string strInputFile,string strOutputFile,int pixComCounts,int pixLineCounts,int bands,int type)        

44、0;   bool blnSuccess = true;        FileStream inputF = new FileStream(strInputFile, FileMode.Open);        FileStream outputF = new FileStream(s

45、trOutputFile, FileMode.CreateNew);        int totalsize = pixComCounts * pixLineCounts * bands * type;/计算输入文件总字节数        if (totalsize != inputF.Length)

46、60;                   return false;                byte bts = new bytetotalsize;   &#

47、160;    int num = 0, bt;        while (bt = inputF.ReadByte() > -1)        /读取出全部字节数据,存储在数组中          

48、  btsnum = (byte)bt;            num+;                for (int bandnum = 0; bandnum < bands; bandn

49、um+)        /读取波段写入            for (int row = 0; row < pixLineCounts; row+)            /按行写入 

50、;                                   for (int columnum = 0; columnum < pixComCounts;

51、0;columnum+)                 /读取列写入                    int startpos = pixComCounts * type&

52、#160;* bands * row + columnum * type * bands + bandnum * type;                    for (int typenum = 0; typenu

53、m < type; typenum+)                    /读取数据基本单元类型输入数据                     &#

54、160;  outputF.WriteByte(btsstartpos +  typenum);                                      

55、;                           outputF.Flush();/保存缓存文件        outputF.Close();/关闭撤销变量文件       

56、0;outputF.Dispose();        inputF.Close();        inputF.Dispose();        return blnSuccess;     / <summary>    / bsq

57、转换为bil    / </summary>    / <param name="strInputFile"></param>源文件名称与路径    / <param name="strOutputFile"></param>目标文件名称与路径    / <param

58、60;name="pixComCounts"></param>像素行数    / <param name="pixLineCounts"></param>像素列数    / <param name="bands"></param>波段数    / <param name="ty

59、pe"></param>基本数据类型代码    / <returns></returns>是否转换成功    public static bool BsqToBil(string strInputFile, string strOutputFile, int pixComCounts, int pixLineCounts, int ba

60、nds, int type)            bool blnSuccess = true;        FileStream inputF = new FileStream(strInputFile, FileMode.Open);     

61、;   FileStream outputF = new FileStream(strOutputFile, FileMode.CreateNew);        int totalsize = pixComCounts * pixLineCounts * bands * type;/计算输入文件总字节数   

62、60;    if(totalsize!=inputF.Length)                    return false;                byte b

63、ts = new bytetotalsize;        int num = 0,bt;        while(bt=inputF.ReadByte()>-1)        /读取出全部字节数据,存储在数组中      &#

64、160;     btsnum = (byte)bt;            num+;                for (int row = 0; row < pix

65、LineCounts; row+)        /按行写入数据            for (int bandnum = 0; bandnum < bands;bandnum+)           

66、 /按波段写入数据                int startpos = pixComCounts * pixLineCounts * type * bandnum+row*pixComCounts*type;          &#

67、160;     for (int columnum = 0; columnum < pixComCounts; columnum+)                 /写入没一列数据         &

68、#160;          for (int typenum = 0; typenum <type;typenum+)                    /按数据基本单元类型输入数据    

69、;                    outputF.WriteByte(btsstartpos + columnum*type+typenum);                   

70、                                                 output

71、F.Flush();/保存缓存文件        outputF.Close();/关闭撤销变量文件        outputF.Dispose();        inputF.Close();        inputF.Dispose();  

72、0;     return blnSuccess;     / <summary>    / bil转换为bip    / </summary>    / <param name="strInputFile"></param>源文件名称与路径 

73、0;  / <param name="strOutputFile"></param>目标文件名称与路径    / <param name="pixComCounts"></param>像素行数    / <param name="pixLineCounts"></param>像素列数  

74、  / <param name="bands"></param>波段数    / <param name="type"></param>基本数据类型代码    / <returns></returns>是否转换成功    public static bool BilT

75、oBip(string strInputFile, string strOutputFile, int pixComCounts, int pixLineCounts, int bands, int type)            bool blnSuccess = true;     

76、   FileStream inputF = new FileStream(strInputFile, FileMode.Open);        FileStream outputF = new FileStream(strOutputFile, FileMode.CreateNew);        i

77、nt totalsize = pixComCounts * pixLineCounts * bands * type;/计算输入文件总字节数        if (totalsize != inputF.Length)               &

78、#160;    return false;                byte bts = new bytetotalsize;        int num = 0, bt;        while (bt = inputF.ReadByte() > -1)        /读

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论