C#结合GDAL使用Band的ReadRaster和WriteRaster方法实现3波段图像水平镜像

本处用到了Band的ReadRaster()和WriteRaster()方法。

 private void btnOzil_Click(object sender, EventArgs e)
        {
            string openFileName = "";
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                openFileName = ofd.FileName;
            }

            Gdal.AllRegister();

            //jpg格式不支持update
            Dataset srcDs = Gdal.Open(openFileName, Access.GA_ReadOnly);
           
            Band srcBand1=srcDs.GetRasterBand(1);
            Band srcBand2 = srcDs.GetRasterBand(2);
            Band srcBand3 = srcDs.GetRasterBand(3);

            DataType srcType = srcDs.GetRasterBand(1).DataType;//byte类型

            int bandCount = srcDs.RasterCount;
            int srcWidth = srcDs.RasterXSize;
            int srcHeight = srcDs.RasterYSize;

            Debug.WriteLine("原始影像数据类型是:{0}", srcType);
            Debug.WriteLine("原始影像的列数:{0}", srcWidth);
            Debug.WriteLine("原始影像的行数:{0}", srcHeight);

            int[] bandArray = new int[bandCount];
            for (int i = 0; i < bandCount; i++)
            {
                bandArray[i] = i + 1;
            }

            int[] dstBandArray = new int[]{1,2,3};
           
             int[] dataArray1 = new int[srcWidth * srcHeight * bandCount];
             int[] dataArray2 = new int[srcWidth * srcHeight * bandCount];
             int[] dataArray3= new int[srcWidth * srcHeight * bandCount];


             srcBand1.ReadRaster(0, 0, srcWidth, srcHeight, dataArray1, srcWidth, srcHeight, 0, 0);
             srcBand2.ReadRaster(0, 0, srcWidth, srcHeight, dataArray2, srcWidth, srcHeight, 0, 0);
             srcBand3.ReadRaster(0, 0, srcWidth, srcHeight, dataArray3, srcWidth, srcHeight, 0, 0);

            //注意,JPG没有实现Create方法来创建
            //Dataset dstDs= drv.Create(dstFileName,srcWidth,srcHeight,bandCount,DataType.GDT_Byte,null);

            //首先创建一个内存的驱动
            string strMemory=@"E:\tempMemory.jpg";
            Driver dryMemory = Gdal.GetDriverByName("MEM");
            //Dataset dsMemory = dryMemory.Create(strMemory, srcWidth, srcHeight,bandCount, DataType.GDT_Byte, null);
            Dataset dsMemory = dryMemory.Create(strMemory, srcWidth, srcHeight, 3, DataType.GDT_Byte, null);

            Band memBand1 = dsMemory.GetRasterBand(1);
            Band memBand2 = dsMemory.GetRasterBand(2);
            Band memBand3 = dsMemory.GetRasterBand(3);


            if (srcType == DataType.GDT_Byte)
            {
                int[] dataArray = new int[srcWidth * srcHeight * bandCount];
               
                srcDs.ReadRaster(0, 0, srcWidth, srcHeight, dataArray, srcWidth, srcHeight, bandCount, bandArray, 0, 0, 0);

                /***********水平镜像实现**************/
                //输出改变前的第200行
                //Debug.WriteLine("改变前");
                //for(int ii=0;ii<srcWidth;++ii)
                //{
                //    Debug.Write(dataArray[200*srcWidth+ii].ToString() + "  ");
                //}
                //Debug.WriteLine("改变后");

                int temp;
             
                for (int i = 0; i < srcHeight; i++)
                {
                    for (int j = 0; j < srcWidth / 2; j++)
                    {
                        temp = dataArray1[i * srcWidth + j];
                        dataArray1[i * srcWidth + j] = dataArray1[i * srcWidth + (srcWidth - 1 - j)];
                        dataArray1[i * srcWidth + (srcWidth - 1 - j)] = temp;
                    }
                }

                for (int i = 0; i < srcHeight; i++)
                {
                    for (int j = 0; j < srcWidth / 2; j++)
                    {
                        temp = dataArray2[i * srcWidth + j];
                        dataArray2[i * srcWidth + j] = dataArray2[i * srcWidth + (srcWidth - 1 - j)];
                        dataArray2[i * srcWidth + (srcWidth - 1 - j)] = temp;
                    }
                }

                for (int i = 0; i < srcHeight; i++)
                {
                    for (int j = 0; j < srcWidth / 2; j++)
                    {
                        temp = dataArray3[i * srcWidth + j];
                        dataArray3[i * srcWidth + j] = dataArray3[i * srcWidth + (srcWidth - 1 - j)];
                        dataArray3[i * srcWidth + (srcWidth - 1 - j)] = temp;
                    }
                }

                memBand1.WriteRaster(0, 0, srcWidth, srcHeight, dataArray1, srcWidth, srcHeight, 0, 0);
                memBand2.WriteRaster(0, 0, srcWidth, srcHeight, dataArray2, srcWidth, srcHeight, 0, 0);
                memBand3.WriteRaster(0, 0, srcWidth, srcHeight, dataArray3, srcWidth, srcHeight, 0, 0);

            }
            
            Driver drvJPG = Gdal.GetDriverByName("JPEG");
            string dstFileName = @"E:\right_result_gyy.jpg";

            drvJPG.CreateCopy(dstFileName, dsMemory, 1, null, null, null);

            //最后释放资源
            srcDs.Dispose();
            dsMemory.Dispose();

            MessageBox.Show("水平镜像:success");
        }
原图像1:

图像1水平镜像效果:

原图像2:


图像2水平镜像效果:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值