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

栅格重分类方法很多,在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 dataset
        IRasterEdit 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);
}
发布了18 篇原创文章 · 获赞 10 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41135605/article/details/88361559