遍历栅格的方式实现栅格重分类 您所在的位置:网站首页 二值化地图 遍历栅格的方式实现栅格重分类

遍历栅格的方式实现栅格重分类

2023-03-20 06:35| 来源: 网络整理| 查看: 265

栅格重分类方法很多,在AE中有多种方式可以实现,使用地图代数(在RasterModel中实现),或者IReclassOp,或者Geoprocessor的方式都可以,甚至可以遍历栅格来实现,这是最原始的方式,不过也可能是最实用的。这里使用的是最原始的遍历栅格的方式。

public static void StrechDN(IRasterLayer pRasterLayer, int OTSU,int minValue, int maxValue)//修改二值化后的像素值{//获取栅格像素块IRaster pRaster = pRasterLayer.Raster;IRasterProps pRasterProps = (IRasterProps)pRaster; //rasterband;//设置像素块的大小IPnt pBlockSize1 = new PntClass(); pBlockSize1.SetCoords(pRasterProps.Width, pRasterProps.Height);IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pBlockSize1);// raster2.CreateCursorEx(pBlockSize1).PixelBlock;int w = pixelBlock.Width;int h = pixelBlock.Height;//read the first pixel block,左上点坐标 IPnt topleftCorner = new PntClass();topleftCorner.SetCoords(0, 0);//左上角第一个像素点pRaster.Read(topleftCorner, pixelBlock);//从第一个点开始读pixelBlock大小的像素块//modify one pixel value at location (assume the raster has a pixel type of uchar)IPixelBlock3 pixelBlock3 = (IPixelBlock3)pixelBlock;object pValue;System.Array pixels = (System.Array)pixelBlock3.get_PixelData(0);for (int finalX = 0; finalX < pRasterProps.Width; finalX++){for (int finalY = 0; finalY < pRasterProps.Height; finalY++){pValue = pixels.GetValue(finalX, finalY);if (Convert.ToDouble(pValue) < OTSU){pixels.SetValue(minValue, finalX, finalY);}else{pixels.SetValue(maxValue, finalX, finalY);}}} pixelBlock3.set_PixelData(0, (System.Object)pixels);//write the modified pixel block to the raster datasetIRasterEdit rasterEdit = (IRasterEdit)pRaster;rasterEdit.Write(topleftCorner, pixelBlock);rasterEdit.Refresh();//刷新资源System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);//停止编辑 }

将IRasterLayer存储起来的方法

public static void SaveRasterLayerTofile(IRasterLayer pRasterLayer, string fileName, string strFileExtension="TIFF"){IRaster pRaster = pRasterLayer.Raster;IRaster2 pRaster2 = pRaster as IRaster2;ISaveAs pSaveAs = pRaster2 as ISaveAs;pSaveAs.SaveAs(fileName, null, strFileExtension); }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有