C#结合GDAL使用DataSet的ReadRaster和WriteRaster方法实现水平镜像
本处使用DataSet的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格式不支持updateDataset srcDs = Gdal.Open(openFileName, Access.GA_ReadOnly);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;}//注意,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);if (srcType == DataType.GDT_Byte){int[] dataArray = new int[srcWidth * srcHeight * bandCount];//ReadRaster最后三个参数全是0,默认按照波段顺序存储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("改变后");/***********镜像方法1——通过************/int bandTemp,temp;for (int b = 0; b < bandCount; ++b )//循环3个波段{// srcWidth * srcWidth*0 到 srcWidth * srcWidth*1-1 ————R波段// srcWidth * srcWidth*1 到 srcWidth * srcWidth*2-1 ————G波段// srcWidth * srcWidth*2 到 srcWidth * srcWidth*3-1 ————R波段bandTemp = b * srcWidth * srcHeight;for (int i = 0; i < srcHeight; i++){for (int j = 0; j < srcWidth / 2; j++){temp = dataArray[bandTemp + i * srcWidth + j];dataArray[bandTemp + i * srcWidth + j] = dataArray[bandTemp + i * srcWidth + (srcWidth - 1 - j)];dataArray[bandTemp + i * srcWidth + (srcWidth - 1 - j)] = temp;}}}/***********镜像方法1************//***********镜像方法2:这样和镜像方法3一样,是将图片按照斜上45度进行了镜像************///因为没有安装每个波段,每一行数据进行交换,而是将整个数组进行了反转//int[] newArray = new int[srcWidth * srcHeight * bandCount];//for (int mm = 0; mm <= srcWidth * srcHeight - 1; ++mm)//{// newArray[mm] = dataArray[srcWidth * srcHeight - 1 - mm];//}//for (int nn = srcWidth * srcHeight; nn <= srcWidth * srcHeight * 2 - 1; ++nn)//{// newArray[nn] = dataArray[srcWidth * srcHeight * 2 - 1 - nn];//}//for (int mn = srcWidth * srcHeight * 2; mn < srcWidth * srcHeight * 3 - 1; ++mn)//{// newArray[mn] = dataArray[srcWidth * srcHeight * 3 - 1 - mn];//}/***********镜像方法2************//***********镜像方法2改进——未完成************///int tempTwo;//for (int mm = 0; mm <= srcWidth * srcHeight*3 - 1; ++mm)//{// if(mm<=srcHeight*srcWidth-1)// {// for (int ix = 0; ix < srcHeight; ++ix)// {// for (int iix = 0; iix < srcWidth / 2; ++iix)// {// tempTwo = dataArray[ix * srcWidth + iix];// dataArray[ix * srcWidth + iix] = dataArray[ix * srcWidth + srcWidth - 1 - iix];// dataArray[ix * srcWidth + srcWidth - 1 - iix] = tempTwo;// }// }// }// else if(mm<=srcWidth*srcHeight*2-1)// {// int nn = srcHeight * srcWidth;// for (int iy = 0; iy < srcHeight; ++iy)// {// for (int iiy = 0; iiy < srcWidth / 2; ++iiy)// {// tempTwo = dataArray[nn + iy * srcWidth + iiy];// dataArray[nn + iy * srcWidth + iiy] = dataArray[nn + iy * srcWidth + srcWidth - 1 - iiy];// dataArray[nn + iy * srcWidth + srcWidth - 1 - iiy] = tempTwo;// }// }// }// else if(mm<=srcWidth*srcHeight*3-1)//最后一个跳出for// {// int mn = srcWidth * srcHeight * 2;// for (int iz = 0; iz < srcHeight; ++iz)// {// for (int iiz = 0; iiz < srcWidth / 2; ++iiz)// {// tempTwo = dataArray[mn + iz * srcWidth + iiz];// dataArray[mn + iz * srcWidth + iiz] = dataArray[mn + iz * srcWidth + srcWidth - 1 - iiz];// dataArray[mn + iz * srcWidth + srcWidth - 1 - iiz] = tempTwo;// }// }// }//}//for (int nn = srcWidth * srcHeight; nn <= srcWidth * srcHeight * 2 - 1; ++nn)//{// for (int iy = 0; iy < srcHeight; ++iy)// {// for (int iiy = 0; iiy < srcWidth / 2; ++iiy)// {// tempTwo = dataArray[nn+iy * srcWidth + iiy];// dataArray[nn + iy * srcWidth + iiy] = dataArray[nn + iy * srcWidth + srcWidth - 1 - iiy];// dataArray[nn + iy * srcWidth + srcWidth - 1 - iiy] = tempTwo;// }// }//}//for (int mn = srcWidth * srcHeight * 2; mn < srcWidth * srcHeight * 3 - 1; ++mn)//{// for (int iz = 0; iz < srcHeight; ++iz)// {// for (int iiz = 0; iiz < srcWidth / 2; ++iiz)// {// tempTwo = dataArray[mn+iz * srcWidth + iiz];// dataArray[mn + iz * srcWidth + iiz] = dataArray[mn + iz * srcWidth + srcWidth - 1 - iiz];// dataArray[mn + iz * srcWidth + srcWidth - 1 - iiz] = tempTwo;// }// }//}/***********镜像方法2改进************//***********镜像方法3************/Array.Reverse(dataArray);/***********镜像方法3************///输出改变后的第200行//for (int jj = 0; jj <srcWidth; ++jj)//{// Debug.Write(dataArray[200 * srcWidth + jj].ToString() + " ");//}//将更新数值的数据重新写入图像//镜像方法1dsMemory.WriteRaster(0, 0, srcWidth, srcHeight, dataArray, srcWidth, srcHeight, bandCount, bandArray, 0, 0, 0);//镜像方法2//dsMemory.WriteRaster(0, 0, srcWidth, srcHeight, newArray, srcWidth, srcHeight, bandCount, bandArray, 0, 0, 0);//镜像方法2改进//dsMemory.WriteRaster(0, 0, srcWidth, srcHeight, dataArray, srcWidth, srcHeight, bandCount, bandArray, 0, 0, 0);//镜像方法3//int[] newBandArray = new int[] { 3,2,1};//dsMemory.WriteRaster(0, 0, srcWidth, srcHeight, dataArray, srcWidth, srcHeight, bandCount, newBandArray, 0, 0, 0);dsMemory.FlushCache();}Driver drvJPG = Gdal.GetDriverByName("JPEG");string dstFileName = @"E:\3result_ozil1.jpg";drvJPG.CreateCopy(dstFileName, dsMemory, 1, null, null, null);//最后释放资源srcDs.Dispose();dsMemory.Dispose();MessageBox.Show("水平镜像:success");}
高圆圆原图 :
高圆圆镜像效果:
厄齐尔原图:
厄齐尔镜像效果:
注意,若使用未改进的镜像方法2和方法3,则会实现沿着45度角镜像,因为没有安装每个波段,每一行数据进行交换,而是将整个数组进行了反转:
效果分别是:
和
C#结合GDAL使用DataSet的ReadRaster和WriteRaster方法实现水平镜像相关推荐
- Anaconda环境GDAL库基于whl文件的配置方法
本文介绍在Anaconda环境下,基于.whl文件安装Python中高级地理数据处理库GDAL的方法. 在文章Anaconda下Python中GDAL模块的下载与安装方法(https://bl ...
- 使用C#版本GDAL读取复数图像
GDAL的C#版本虽然在很多算法接口没有导出,但是在读写数据中的接口基本上都是完全导出了.使用ReadRaster和WriteRaster方法来进行读写,同时对这两个方法进行了重载,对于常用的数据类型 ...
- 使用gdal和java对TIF格式正射影像进行拉普拉斯锐化
拉普拉斯锐化算法是读取目标像素上下左右四个像素值,将上下左右四个像素值分别减去目标像素值,再将结果之和加上目标像素值作为目标像素最后的值,也就是说当目标像素与周围像素差值较大,那么计算后会进一步拉大差 ...
- python 读取geotiff_科学网—利用python GDAL库读写geotiff格式的遥感影像方法 - 张伟的博文...
(1)利用python GDAL库读写geotiff格式的遥感影像方法,具有很好的参考价值,不错! from osgeo import gdal import numpy as np def read ...
- Java使用GDAL
在使用Java调用GDAL之前,先说明一下编译.在编译的时候,需要JRE.Ant和SWIG这三个东西,没有的先下载这两个东西,下载完之后,安装,ANT和SWIG直接解压就行,不用安装.接下来用记事本打 ...
- python的gdal库说明_GDAL库学习笔记(一): GDAL库介绍
可能你不玩GIS,不懂这个库到底有什么用,或者和python有什么关系.但是你要玩GIS,RS,你就应当知道这个库的价值.就算你不玩GIS,我想这个库对你也应该有致命的吸引力.为什么?看下面的介绍吧! ...
- python读取tiff影像_科学网—利用python GDAL库读写geotiff格式的遥感影像方法 - 张伟的博文...
(1)利用python GDAL库读写geotiff格式的遥感影像方法,具有很好的参考价值,不错! from osgeo import gdal import numpy as np def read ...
- java 使用gdal_Java使用GDAL
在使用Java调用GDAL之前,先说明一下编译.在编译的时候,需要JRE.Ant和SWIG这三个东西,没有的先下载这两个东西,下载完之后,安装,ANT和SWIG直接解压就行,不用安装.接下来用记事本打 ...
- GDAL通过地理坐标获取所在位置高程值(数据源为DEM)
参考:C#应用GDAL通过传入范围获取tif数据的最大高程值及其坐标 public static double GetElevation(OSGeo.GDAL.Dataset ds, double d ...
最新文章
- Android 中的GC资料网站
- 不是python中用于开发用户界面的第三方库-模拟试卷C
- 解决AD 不能打开DDB文件的解决方案
- 按键精灵 getcursorpos没有用_给你们想要的一键输出II按键精灵脚本开发教程
- c++ 多重背包状态转移方程_串讲:控制理论:全状态反馈控制(FSFB)
- 写博客一周我有哪些收获
- exit()与_exit()的区别
- 什么是JavaConfig
- 深入浅出Dotnet Core的项目结构变化
- Leedcode4-sort listnode 归并排序
- Bootstrap Magic – 轻松创建自己的 Bootstrap 主题
- python课程索引-0222
- 防碰撞算法c语言,RFID防碰撞 二进制树形搜索算法 C语言实现
- Debian 7 Wheezy 安装 VirtualBox
- java成员变量覆盖_java-成员变量的属性与成员函数的覆盖
- Spark编程核心抽象—RDD
- JAVA藏宝阁游戏交易系统计算机毕业设计Mybatis+系统+数据库+调试部署
- android实现天气预报App(0)
- zlog日志系统开发中遇到的问题(2)
- 关于Eclipse的使用入门
热门文章
- 超星项目最终答辩总结2023.5.18
- 【sql】SQL19 查找所有员工的last_name和first_name以及对应的dept_name
- 中级软件设计师备考---多媒体技术
- 最近邻和K近邻及其优化算法LSH(局部敏感哈希,Locality Sensitive Hashing) Kd-Tree
- 编译原理之算符优先分析语法程序
- 网站逆向分析-vue打包后的网站数据获取
- visual studio 2010版本安装以及初步使用
- 固态硬盘接口分类和速度
- 400亿美元的全球超算赛道 中国力量超速发展
- ADVANCE.AI的海外KYC认证服务准确率超过99%