首页 > 其他 > 详细

[转载]C#下使用GDAL将dem转换为灰度图

时间:2014-02-16 21:53:59      阅读:1594      评论:0      收藏:0      [点我收藏+]

原文链接:http://blog.csdn.net/yiyi31130108/article/details/19151609

bubuko.com,布布扣
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using OSGeo.GDAL;
 6 using System.Drawing;
 7 using System.Drawing.Imaging;
 8 using System.Runtime.InteropServices;
 9 
10 namespace RGeos.Terrain
11 {
12     public class DemHelper
13     {
14         private int width;
15         private int height;
16         private Band band;
17 
18         public void Start()
19         {
20             Gdal.AllRegister();
21         }
22 
23         public void Read(string path)
24         {
25             Dataset ds = Gdal.Open(path, Access.GA_ReadOnly);
26             band = ds.GetRasterBand(1);
27             width = ds.RasterXSize;
28             height = ds.RasterYSize;
29         }
30 
31         public Bitmap MakeGrayScale()
32         {
33             //新建8位灰度图
34             Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
35             BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, width, height),
36                 ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
37 
38             //获取dem信息
39             double min, max, no;
40             int hasvalue;
41             band.GetMaximum(out max, out hasvalue);
42             band.GetMinimum(out min, out hasvalue);
43             band.GetNoDataValue(out no, out hasvalue);
44             double[] data = new double[width * height];
45             band.ReadRaster(0, 0, width, height, data, width, height, 0, 0);
46 
47             //计算图像参数
48             int offset = bmpdata.Stride - bmpdata.Width; //计算每行未用空间字节数
49             IntPtr ptr = bmpdata.Scan0; //获取首地址
50             int scanBytes = bmpdata.Stride * bmpdata.Height; //图像字节数= 扫描字节数 * 高度
51             byte[] grayValues = new byte[scanBytes]; //为图像数据分配内存
52 
53             //为图像数据赋值
54             int posSrc = 0, posScan = 0;
55             for (int i = 0; i < height; i++) //row
56             {
57                 for (int j = 0; j < width; j++) //col
58                 {
59                     double item = data[posSrc++];
60                     if (item == no)
61                     {
62                         grayValues[posScan++] = 0; //黑色
63                     }
64                     else
65                     {
66                         grayValues[posScan++] = (byte)((item - min) * 256 / (max - min));
67                     }
68                 }
69                 //跳过图像数据每行未用空间的字节, length = stride - width * bytePerPixel
70                 posScan += offset;
71             }
72 
73             Marshal.Copy(grayValues, 0, ptr, scanBytes);
74             bitmap.UnlockBits(bmpdata);
75 
76             //修改生成位图的索引表,从伪彩修改为灰度
77             ColorPalette palette;
78             //获取一个Format8bppIndex格式图像的Palette对象
79             using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
80             {
81                 palette = bmp.Palette;
82             }
83             for (int i = 0; i < 256; i++)
84             {
85                 palette.Entries[i] = Color.FromArgb(i, i, i);
86             }
87             //修改生成位图的索引表
88             bitmap.Palette = palette;
89 
90             return bitmap;
91         }
92     }
93 }
View Code

调用

bubuko.com,布布扣
 1  private void tspLoadDEM_Click(object sender, EventArgs e)
 2         {
 3             OpenFileDialog dlg = new OpenFileDialog();
 4             dlg.Title = "";
 5             dlg.Filter = "Img(*.img)|*.img";
 6             if (dlg.ShowDialog() == DialogResult.OK)
 7             {
 8                 string file = dlg.FileName;
 9                 string NameOf = System.IO.Path.GetFileNameWithoutExtension(file);
10                 DemHelper dem = new DemHelper();
11                 dem.Start();
12                 dem.Read(file);
13                 Bitmap bitmap = dem.MakeGrayScale();
14                 Vector3 position = new Vector3(-100f, 0f, 100f);
15                 SimpleRasterShow simRaster = new SimpleRasterShow(NameOf, position, bitmap.Width, bitmap.Height);
16                 simRaster.IsOn = true;
17                 simRaster.RenderPriority = RenderPriority.Custom;
18                 simRaster.bitmap = bitmap;
19                 mSceneControl.CurrentWorld.RenderableObjects.ChildObjects.Add(simRaster);
20             }
21         }
View Code

[转载]C#下使用GDAL将dem转换为灰度图

原文:http://www.cnblogs.com/yhlx125/p/3551194.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!