编程实现BIPBSQBIL三种格式转换详解.docx

上传人:b****1 文档编号:339168 上传时间:2023-04-29 格式:DOCX 页数:16 大小:589.52KB
下载 相关 举报
编程实现BIPBSQBIL三种格式转换详解.docx_第1页
第1页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第2页
第2页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第3页
第3页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第4页
第4页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第5页
第5页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第6页
第6页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第7页
第7页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第8页
第8页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第9页
第9页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第10页
第10页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第11页
第11页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第12页
第12页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第13页
第13页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第14页
第14页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第15页
第15页 / 共16页
编程实现BIPBSQBIL三种格式转换详解.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

编程实现BIPBSQBIL三种格式转换详解.docx

《编程实现BIPBSQBIL三种格式转换详解.docx》由会员分享,可在线阅读,更多相关《编程实现BIPBSQBIL三种格式转换详解.docx(16页珍藏版)》请在冰点文库上搜索。

编程实现BIPBSQBIL三种格式转换详解.docx

编程实现BIPBSQBIL三种格式转换详解

编程实现BIP、BSQ、BIL三种格式转换

一、实验目的

理解遥感图像BIP、BIL、BSQ三种格式数据的组织方式,以及它们互相转换原理和方法。

二、实验原理

ENVI栅格图像文件,是以字节数据为单位,再按照指定顺序组织、排列而成,具体有如下三种方式:

1.BIP(像元波段交叉式):

以一对像元为基本单位进行记录;

2.BIL(波段按行交叉格式):

按照扫描行为单位各波段同一扫描行数据依次记录,图像按顺序第一个像元所有的波段,接着是第二个像元的所有波段,然后是第三个像元的所有波段,等等,直到像元总数为止;

3.BSQ(波段顺序格式):

以波段为单位,每波段所有扫描行依次记录,每行数据后面紧接着同一波谱波段的下一行数据。

所以实现三种格式的转换,实际上是对图像字节进行重新排序。

三、实验思路

 

四、实验步骤

1.新建项目文件:

本实验选用的语言是C#,开发工具是VisualStudio2010,通过“文件—新建—项目—C#—Windows窗体应用程序”,命名“格式转换”即可;

2.编写代码:

通过实验思路的流程图,我们确定代码也应该分这三个功能来写,对应可以组织为三个大方法,再细分,调用即可。

(1).编写读取头文件代码:

ENVI中栅格图像被分为两个文件存储,一个“.HDR”头文件和一个数据文件,头文件存储了图像描述信息,它是一个文本文件,我们可以用System.IO.SreamReader字符读取流按行读取,再用字符串匹配方法,以读取字段的方式获取图像的行数、列数、波段数、基本数据类型,即可,具体代码可见后文代码附录;

(2).编写数据读取数据文件代码:

图像数据组织方式有BIP、BIL、BSQ三种,那么对应的也应该有三个方法按照行、列、波段交叉顺序对图像字节数据进行读取,具体代码见后文;

(3).编写字节数据重排列存储代码:

在读取了图像数据文件后,可以通过FileStream类按照目标类型格式文件组织顺序,把字节数据写入到文件中保存即可,具体代码见后文。

3.编译与调试:

通过用预先知道内部字节数据的实验文件,进行测试,辅助断点调试,可排除一般问题,接着编译生成目标程序;

4.运行程序查看结果:

通过对多种格式、多种基本数据类型、多种像素大小的文件进行多种方式转换,再利用ENVI打开两个图像查看是否一样,再辅以“Link”功能看同一像素点值是否相同,这样可以深层确定转换的正确性。

五、结果与分析

1.结果

程序运行界面

转换后结果

2.分析总结

(1).读取头文件时,不能通过各字段的现有位,按位读取,应该按行读取,再利用字符串匹配方式,找到指定字段,这样避免字段位变化时读取出错误信息;

(2).再读取数据文件和写入存储时,应该要以基本数据类型长度个字节为单位进行,不能单纯的以一个字节操作,不然会不具有普遍性,遇到以“int、float”为基本类型的文件时,就会出错;

(3).要想节省代码,可先将各种格式数据读取存储为一种格式字节数组中,再从这个格式往其他格式进行转换,这样可以节省不少工作量,但是引入了中间数组,耗费了内存。

 

六、源代码附录

1.读取头文件信息

 /// 

    /// 读取头文件信息

    /// 

    /// 头文件路径和名称

    /// 像素列数

    /// 像素行数

    /// 波段数

    /// 基本数据类型代码

    /// 文件组织格式

    /// 返回是否读取成功

    public static bool ReadHDR(String strFileName,out int iColumnsCount,out int iLinesCount,out int iBandsCount,out int iType,out String strInterLeave)

    {

       bool blnSuccess = false;

       iColumnsCount = -1;

       iLinesCount = -1;

       iBandsCount = -1;

       iType = -1;

       strInterLeave = "";

       //初始化各个变量

       StreamReader hdrfile=null;

       try

       {

          hdrfile = new StreamReader(strFileName);

          string content="";

          while(hdrfile.EndOfStream!

=true)

          {//获取像素列数

             content = hdrfile.ReadLine();

             if(content.Contains("samples"))

             {

                 String samples = content.Substring(content.IndexOf("=") + 1, content.Length - content.IndexOf("=") -1).Trim();

                 iColumnsCount = Convert.ToInt32(samples);

                 System.Console.WriteLine(samples);

                 break;

               }

             }

             while (hdrfile.EndOfStream !

= true)

             {//获取像素行数

               content = hdrfile.ReadLine();

               if (content.Contains("lines"))

               {

                  String lines = content.Substring(content.IndexOf("=") + 1, content.Length - content.IndexOf("=") - 1).Trim();

                  iLinesCount = Convert.ToInt32(lines);

                  System.Console.WriteLine(lines);

                  break;

                }

              }

              while (hdrfile.EndOfStream !

= true)

              {//获取波段个数

                content = hdrfile.ReadLine();

                if (content.Contains("bands"))

                {

                 String bands = content.Substring(content.IndexOf("=") + 1, 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("data type"))

                  {

                   String type = content.Substring(content.IndexOf("=") + 1, content.Length - content.IndexOf("=") - 1).Trim();

                   iType = Convert.ToInt32(type);

                   System.Console.WriteLine(type);

                   break;

                  }

                }

               while (hdrfile.EndOfStream !

= true)

                { //获取数据解译方式

                  content = hdrfile.ReadLine();

                  if (content.Contains("interleave"))

                  {

                    String interleve = content.Substring(content.IndexOf("=") + 1, content.Length - content.IndexOf("=") - 1).Trim();

                    strInterLeave = interleve;

                    System.Console.WriteLine(interleve);

                    blnSuccess = true;

                    break;

                  }

                }

            }

            catch

            {//读取失败

                hdrfile.Close();

                hdrfile.Dispose();

                return false;

            }

            hdrfile.Close();

            hdrfile.Dispose();

            //关闭文件流,释放内存

            return blnSuccess;

     }

2.读取数据文件进行转换存储

/// 

    /// bip转换为bsq

    /// 

    /// 源文件名称与路径

    /// 目标文件名称与路径

    /// 像素行数

    /// 像素列数

    /// 波段数

    /// 基本数据类型代码

    /// 是否转换成功

    public static bool BipToBsq(string strInputFile,string strOutputFile,int pixComCounts,int pixLineCounts,int bands,int type)

    {

        bool blnSuccess = true;

        FileStream inputF = new FileStream(strInputFile, FileMode.Open);

        FileStream outputF = new FileStream(strOutputFile, FileMode.CreateNew);

        int totalsize = pixComCounts * pixLineCounts * bands * type;//计算输入文件总字节数

        if (totalsize !

= inputF.Length)

        {

            return false;

        }

        byte[] bts = new byte[totalsize];

        int num = 0, bt;

        while ((bt = inputF.ReadByte()) > -1)

        {//读取出全部字节数据,存储在数组中

            bts[num] = (byte)bt;

            num++;

        }

        for (int bandnum = 0; bandnum < bands; bandnum++)

        {//读取波段写入

            for (int row = 0; row < pixLineCounts; row++)

            {//按行写入

                    

                for (int columnum = 0; columnum < pixComCounts; columnum++)

                { //读取列写入

                    int startpos = pixComCounts * type * bands * row + columnum * type * bands + bandnum * type;

                    for (int typenum = 0; typenum < type; typenum++)

                    {//读取数据基本单元类型输入数据

                        outputF.WriteByte(bts[startpos +  typenum]);

                    }

                }

            }

        }

        outputF.Flush();//保存缓存文件

        outputF.Close();//关闭撤销变量文件

        outputF.Dispose();

        inputF.Close();

        inputF.Dispose();

        return blnSuccess;

    }

/// 

    /// bsq转换为bil

    /// 

    /// 源文件名称与路径

    /// 目标文件名称与路径

    /// 像素行数

    /// 像素列数

    /// 波段数

    /// 基本数据类型代码

    /// 是否转换成功

    public static bool BsqToBil(string strInputFile, string strOutputFile, int pixComCounts, int pixLineCounts, int bands, int type)

    {

        bool blnSuccess = true;

        FileStream inputF = new FileStream(strInputFile, FileMode.Open);

        FileStream outputF = new FileStream(strOutputFile, FileMode.CreateNew);

        int totalsize = pixComCounts * pixLineCounts * bands * type;//计算输入文件总字节数

        if(totalsize!

=inputF.Length)

        {

            return false;

        }

        byte[] bts = new byte[totalsize];

        int num = 0,bt;

        while((bt=inputF.ReadByte())>-1)

        {//读取出全部字节数据,存储在数组中

            bts[num] = (byte)bt;

            num++;

        }

        for (int row = 0; row < pixLineCounts; row++)

        {//按行写入数据

            for (int bandnum = 0; bandnum < bands;bandnum++)

            {//按波段写入数据

                int startpos = pixComCounts * pixLineCounts * type * bandnum+row*pixComCounts*type;

                for (int columnum = 0; columnum < pixComCounts; columnum++)

                { //写入没一列数据

                    for (int typenum = 0; typenum 

                    {//按数据基本单元类型输入数据

                        outputF.WriteByte(bts[startpos + columnum*type+typenum]);

                        }

                }

            }

        }

        outputF.Flush();//保存缓存文件

        outputF.Close();//关闭撤销变量文件

        outputF.Dispose();

        inputF.Close();

        inputF.Dispose();

        return blnSuccess;

    }

/// 

    /// bil转换为bip

    /// 

    /// 源文件名称与路径

    /// 目标文件名称与路径

    /// 像素行数

    /// 像素列数

    /// 波段数

    /// 基本数据类型代码

    /// 是否转换成功

    public static bool BilToBip(string strInputFile, string strOutputFile, int pixComCounts, int pixLineCounts, int bands, int type)

    {

        bool blnSuccess = true;

        FileStream inputF = new FileStream(strInputFile, FileMode.Open);

        FileStream outputF = new FileStream(strOutputFile, FileMode.CreateNew);

        int totalsize = pixComCounts * pixLineCounts * bands * type;//计算输入文件总字节数

        if (totalsize !

= inputF.Length)

        {

            return false;

        }

        byte[] bts = new byte[totalsize];

        int num = 0, bt;

        while ((bt = inputF.ReadByte()) > -1)

        {//读取出全部字节数据,存储在数组中

            bts[num] = (byte)bt;

            num++;

    

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2