/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { // Call Read method of the Raster Function Helper object. myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPnt pbSize = new PntClass(); pbSize.SetCoords(pBWidth, pBHeight); //IPixelBlock3 inVlsPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize); //myFunctionHelperCoef.Read(pTlc,null,myFunctionHelperCoef.Raster, (IPixelBlock)inVlsPb); IPixelBlock3 outPixelBlock = (IPixelBlock3)pPixelBlock; System.Array outArr = (System.Array)outPixelBlock.get_PixelData(0); System.Array[] inArr = new System.Array[inrs.RasterInfo.BandCount]; IRasterBandCollection rsBc = (IRasterBandCollection)inrs; for (int b = 0; b < inrs.RasterInfo.BandCount; b++) { IRasterBand rsB = rsBc.Item(b); IRawPixels rPix = (IRawPixels)rsB; IPixelBlock pb = rPix.CreatePixelBlock(pbSize); rPix.Read(pTlc, pb); inArr[b] = (System.Array)pb.get_SafeArray(b); } updateOutArr(outPixelBlock, inArr, outArr); outPixelBlock.set_PixelData(0, outArr); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public override void getOutPutVl(IPixelBlock3 pb, System.Array[] inArr, float vl, int c, int r) { float[] orgArr = new float[bndCnt]; float[] sortArr = new float[bndCnt]; orgArr[0] = vl; sortArr[0] = vl; for (int p = 1; p < bndCnt; p++) { float vl2 = System.Convert.ToSingle(inArr[p].GetValue(c, r)); orgArr[p] = vl2; sortArr[p] = vl2; } System.Array.Sort(sortArr); for (int p = 0; p < bndCnt; p++) { rstPixelType ptyp = pb.get_PixelType(p); float orgVl = orgArr[p]; int indexSort = System.Array.LastIndexOf(sortArr, orgVl); float prc = indexSort / bndCntf; object newVl = rasterUtil.getSafeValue(prc, ptyp); inArr[p].SetValue(newVl, c, r); } //int[] cntLst = new int[100]; //float[] probLst = new float[100]; //float[] orgLst = new float[bndCnt]; //int[] indexvl = new int[bndCnt]; //float min = float.MaxValue; //float max = float.MinValue; //float TCnt = 0; //for (int i = 0; i < coefPb.Planes; i++) //{ // object objVl = coefPb.GetVal(i, c, r); // if (objVl != null) // { // float vl = (float)objVl; // orgLst[i] = vl; // if (vl > max) max = vl; // if (vl < min) min = vl; // TCnt+=1; // } //} //float rng = max - min; //for (int i = 0; i < orgLst.Length; i++) //{ // int indexVl = (int)(99*(orgLst[i] - min) / rng); // indexvl[i] = indexVl; // cntLst[indexVl] = cntLst[indexVl] + 1; //} //int sumCnt = 0; //for (int i = 0; i < cntLst.Length; i++) //{ // sumCnt = sumCnt + cntLst[i]; // probLst[i] = sumCnt/TCnt; //} //for (int i = 0; i < orgLst.Length; i++) //{ // updateArr[i].SetValue(probLst[indexvl[i]], c, r); //} }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { // Call Read method of the Raster Function Helper object. //System.Array noDataValueArr = (System.Array)((IRasterProps)inrsBandsCoef).NoDataValue; //Console.WriteLine("Before Read"); myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); //Console.WriteLine("After Read"); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPnt pbSize = new PntClass(); pbSize.SetCoords(pBWidth, pBHeight); IPixelBlock3 outPb = (IPixelBlock3)inrsBandsCoef.CreatePixelBlock(pbSize);//independent variables inrsBandsCoef.Read(pTlc, (IPixelBlock)outPb); int pBRowIndex = 0; int pBColIndex = 0; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; //System.Array[] pArr = new System.Array[outPb.Planes]; //for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++) //{ // System.Array pixelValues = (System.Array)(outPb.get_PixelData(coefnBand)); // pArr[coefnBand] = pixelValues; //} System.Array pValues = (System.Array)ipPixelBlock.get_PixelData(0);//(System.Array)(td); for (int i = pBRowIndex; i < pBHeight; i++) { for (int k = pBColIndex; k < pBWidth; k++) { double[] xVls = new double[outPb.Planes]; bool ndT = true; for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++) { //float noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(coefnBand)); object pObj = outPb.GetVal(coefnBand, k, i); if (pObj == null) { ndT = false; break; } float pixelValue = Convert.ToSingle(pObj); xVls[coefnBand] = pixelValue; } if (ndT) { int c = cluster.computNew(xVls); pValues.SetValue(c, k, i); } } } ipPixelBlock.set_PixelData(0, pValues); } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of Cluster Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }
/// <summary> /// this method writes values to the current block of pixels of the raster /// </summary> /// <param name="pixelblock3">current block of pixels</param> protected override void DoOperationsWithPixels(IPixelBlock3 pixelblock3) { //Get the pixel array. pixels = (System.Array)pixelblock3.get_PixelData(0); blockwidth = pixelblock3.Width; blockheight = pixelblock3.Height; pixelblock3.Mask(255); object k = null; //iterate through each line in an image for (int i = 0; i < blockwidth; i++) { //iterate through each pixel in the line for (int j = 0; j < blockheight; j++) { k = Convert.ToSingle(blocksWithValues[numOfBlock, i + j * 64]); pixels.SetValue(k, i, j); } } //Set the pixel array to the pixel block. pixelblock3.set_PixelData(0, pixels); //Write back to the raster. tlc = rasterCursor.TopLeft; rasterEdit.Write(tlc, (IPixelBlock)pixelblock3); }
/// <summary> /// this method reads values from the current block of pixels of the raster /// </summary> /// <param name="pixelblock3">current block of pixels</param> protected override void DoOperationsWithPixels(IPixelBlock3 pixelblock3) { blockwidth = pixelblock3.Width; blockheight = pixelblock3.Height; pixelblock3.Mask(255); //Get the pixel array. pixels = (System.Array)pixelblock3.get_PixelData(0); //iterate through each line in an image for (long i = 0; i < blockwidth; i++) { //iterate through each pixel in the line for (long j = 0; j < blockheight; j++) { double val; //Get the pixel value v = pixels.GetValue(i, j); val = Convert.ToDouble(v.ToString()); if (val > 240) { val = 0; //if value is higher than 240, then value is probaly incorrect } //write pixel value to the array allBlocks[numOfBlock, i + j * 64] = val; } } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { //try //{ //System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue; myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; IPnt pbBigSize = new PntClass(); IPnt pbBigLoc = new PntClass(); int pbBigWd = pBWidth + clms - 1; int pbBigHt = pBHeight + rws - 1; int l, t; l = clms / 2; t = rws / 2; //Console.WriteLine("lt = " + (pTlc.X - l).ToString() + ":" + (pTlc.Y - t).ToString()); pbBigSize.SetCoords(pbBigWd, pbBigHt); pbBigLoc.SetCoords((pTlc.X - l), (pTlc.Y - t)); IPixelBlock3 pbBig = (IPixelBlock3)myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize); myFunctionHelperOrig.Read(pbBigLoc, null, myFunctionHelperOrig.Raster, (IPixelBlock)pbBig); updatePixelRectangle(ipPixelBlock, pbBig); //} //catch (Exception e) //{ //Console.WriteLine(e.ToString()); //System.Windows.Forms.MessageBox.Show(e.ToString()); //Console.WriteLine(pTlc.X.ToString() + ":" + pTlc.Y.ToString()); //} }
public static object getBlockUnique(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; HashSet <float> unq = new HashSet <float>(); int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlObj = inArr.GetVal(band, c, r); if (vlObj == null) { continue; } else { float vl = System.Convert.ToSingle(vlObj); unq.Add(vl); } } } outVl = unq.Count; return(outVl); }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { // Call Read method of the Raster Function Helper object. //Console.WriteLine("Before Read"); myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; IRaster2 rs2 = (IRaster2)pRaster; double pX, pY; rs2.PixelToMap(System.Convert.ToInt32(pTlc.X), System.Convert.ToInt32(pTlc.Y), out pX, out pY); //Console.WriteLine("After Read"); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IEnvelope env = new EnvelopeClass(); env.PutCoords(pX, pY - pBHeight, pX + pBWidth, pY); updateWithMergedValues(env, ipPixelBlock); } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of merge Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); IPixelBlock3 pPixelBlock3 = (IPixelBlock3)pPixelBlock; IRaster2 inRs2 = (IRaster2)inrs; IRaster2 outRs2 = (IRaster2)outrs; double mx, my; outRs2.PixelToMap((int)pTlc.X, (int)pTlc.Y, out mx, out my); int clm, rw; inRs2.MapToPixel(mx, my, out clm, out rw); IPnt pntLoc = new PntClass(); pntLoc.SetCoords(clm, rw); IPnt pntSize = new PntClass(); pntSize.SetCoords(pPixelBlock.Width, pPixelBlock.Height); IPixelBlock3 pBIn = (IPixelBlock3)inrs.CreatePixelBlock(pntSize); inrs.Read(pntLoc, (IPixelBlock)pBIn); for (int p = 0; p < pPixelBlock.Planes; p++) { pPixelBlock3.set_PixelData(p, pBIn.get_PixelData(p)); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public static object getBlockMin(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; float outVlF = Single.MaxValue; int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlObj = inArr.GetVal(band, c, r); if (vlObj == null) { continue; } else { float vl = System.Convert.ToSingle(vlObj); if (vl > outVlF) { outVlF = vl; } } } } outVl = outVlF; return(outVl); }
private double[][] pixelBlockToJaggedArray(IPixelBlock3 pb3) { int width = pb3.Width; int height = pb3.Height; int bands = pb3.Planes; //Console.WriteLine("Pixel Block width and height = " + width.ToString() + "; " + height.ToString()); double[][] outArr = new double[width * height][]; for (int r = 0; r < height; r++) { int indexVlr = r * width; int indexVl = indexVlr; for (int c = 0; c < width; c++) { indexVl = indexVlr + c; double[] bndsArr = new double[bands]; for (int p = 0; p < bands; p++) { object objVl = pb3.GetVal(p, c, r); if (objVl != null) { bndsArr[p] = System.Convert.ToDouble(objVl); } } //Console.WriteLine("Index value = " + indexVl.ToString() + "; " + width.ToString() + "; " + height.ToString() +"; "+ r.ToString()+"; " + c.ToString()); outArr[indexVl] = bndsArr; } } return(outArr); }
private object splitArray(int[] vls, IPixelBlock3 pb3) { int width = pb3.Width; int height = pb3.Height; int cnt = 0; System.Array outArr = (System.Array)pb3.get_PixelData(0); rstPixelType rsp = pb3.get_PixelType(0); foreach (int i in vls) { double div = System.Convert.ToDouble(cnt) / width; int r = (int)div; int c = cnt - (r * width); try { object newvl = rasterUtil.getSafeValue(i, rsp); outArr.SetValue(i, c, r); } catch { object newvl = rasterUtil.getSafeValue(900, rsp); outArr.SetValue(900, c, r); } cnt++; } return(outArr); }
public static object getBlockVariance(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; float s = 0; float s2 = 0; float n = 0; int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlObj = inArr.GetVal(band, c, r); if (vlObj == null) { continue; } else { float vl = System.Convert.ToSingle(vlObj); n += 1; s += vl; s2 += vl * vl; } } } outVl = (s2 - ((s * s) / n)) / n; return(outVl); }
public void Write(float?[,] rasterValue, string format) { FileHelper.DeleteFile(_workSpace, _fileName, ".tif", ".tfw", ".tif.aux"); IRasterWorkspace2 rasterWs = OpenRasterWorkspace(); if (rasterWs == null) { throw new NullReferenceException("栅格文件打开失败"); } IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(_fileName + ".tif", format, RasterInfo.OriginPoint, RasterInfo.Width, RasterInfo.Height, RasterInfo.XCellSize, RasterInfo.YCellSize, 1, rstPixelType.PT_FLOAT, RasterInfo.SpatialReference, true); IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset; var rasterBand = rasterBands.Item(0); var rasterProps = (IRasterProps)rasterBand; //Set NoData if necessary. For a multiband image, NoData value needs to be set for each band. rasterProps.NoDataValue = -1; //Create a raster from the dataset. IRaster raster = rasterDataset.CreateDefaultRaster(); //Create a pixel block. IPnt blocksize = new PntClass(); blocksize.SetCoords(RasterInfo.Width, RasterInfo.Height); IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize) as IPixelBlock3; //Populate some pixel values to the pixel block. var pixels = (Array)pixelblock.get_PixelData(0); for (int i = 0; i < RasterInfo.Width; i++) { for (int j = 0; j < RasterInfo.Height; j++) { if (rasterValue[i, j].HasValue) { pixels.SetValue((float)rasterValue[i, j], i, j); } else { pixels.SetValue(-1, i, j); } } } pixelblock.set_PixelData(0, pixels); //Define the location that the upper left corner of the pixel block is to write. IPnt upperLeft = new PntClass(); upperLeft.SetCoords(0, 0); //Write the pixel block. IRasterEdit rasterEdit = (IRasterEdit)raster; rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock); //Release rasterEdit explicitly. Marshal.ReleaseComObject(rasterEdit); }
public static float[,] Raster2Mat(IRasterLayer rasterlayer) //将栅格数据转为二元数组 { IRaster raster = rasterlayer.Raster; IRaster2 raster2 = raster as IRaster2; IRasterProps pRasterProps = (IRasterProps)raster; IPnt pntstart = new DblPntClass(); pntstart.SetCoords(0, 0); IPnt pntend = new DblPntClass(); pntend.SetCoords(pRasterProps.Width, pRasterProps.Height); IPixelBlock3 unionPixelBlock = (IPixelBlock3)raster.CreatePixelBlock(pntend); System.Single[,] floatMat; try { raster.Read(pntstart, (IPixelBlock)unionPixelBlock); floatMat = (System.Single[, ])unionPixelBlock.get_PixelData(0); } catch (Exception e) { raster.Read(pntstart, (IPixelBlock)unionPixelBlock); int[,] intMat = (int[, ])unionPixelBlock.get_PixelData(0); floatMat = new System.Single[pRasterProps.Width, pRasterProps.Height]; Parallel.For(0, pRasterProps.Width, i => { for (int j = 0; j < pRasterProps.Height; j++) { floatMat[i, j] = Convert.ToSingle(intMat[i, j]); } }); } return(floatMat); }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { double vl = 0; float pixelValue = 0f; try { System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue; // Call Read method of the Raster Function Helper object. myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); IPixelBlock3 pb3 = (IPixelBlock3)pPixelBlock; #region Load log object for (int nBand = 0; nBand < pPixelBlock.Planes; nBand++) { float noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(nBand)); System.Array dArr = (System.Array)pb3.get_PixelData(nBand); for (int r = 0; r < pPixelBlock.Height; r++) { for (int c = 0; c < pPixelBlock.Width; c++) { vl = System.Convert.ToDouble(dArr.GetValue(c, r)); if (rasterUtil.isNullData(System.Convert.ToSingle(vl), noDataValue)) { continue; } pixelValue = System.Convert.ToSingle(getFunctionValue(vl)); dArr.SetValue(pixelValue, c, r); } } pb3.set_PixelData(nBand, dArr); //unsafe //{ // System.Array dArr = (System.Array)pb3.get_PixelData(nBand); // int lng = dArr.Length; // fixed (float* dValue = (float[,])dArr) // { // for (int i = 0; i < lng; i++) // { // pixelValue = *(dValue + i); // if (rasterUtil.isNullData(pixelValue, noDataValue)) // { // continue; // } // pixelValue = System.Convert.ToSingle(getFunctionValue(pixelValue)); // *(dValue + i) = pixelValue; // } // pb3.set_PixelData(nBand, dArr); // } //} } #endregion } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method math Function. " + exc.Message, exc); throw myExc; } }
public static void exportRasterData(string parth, IRasterLayer rasterLayer, float[,] rasterMat) //输出栅格数据 { string directory = parth.Substring(0, parth.LastIndexOf("\\")); string name = parth.Substring(parth.LastIndexOf("\\") + 1); IWorkspaceFactory workspaceFac = new RasterWorkspaceFactoryClass(); IRasterWorkspace2 rasterWorkspace2 = workspaceFac.OpenFromFile(directory, 0) as IRasterWorkspace2; IRasterInfo rasterInfo = (rasterLayer.Raster as IRawBlocks).RasterInfo; IPoint originPoint = new Point(); originPoint.PutCoords(rasterInfo.Origin.X, rasterInfo.Origin.Y - (rasterLayer.Raster as IRasterProps).Height * (rasterLayer.Raster as IRasterProps).MeanCellSize().Y); IRasterProps rasterProps = rasterLayer.Raster as IRasterProps; IRasterDataset rasterDataSet = rasterWorkspace2.CreateRasterDataset(name, "IMAGINE Image", originPoint, rasterProps.Width, rasterProps.Height, rasterProps.MeanCellSize().X, rasterProps.MeanCellSize().Y, 1, rstPixelType.PT_FLOAT, rasterProps.SpatialReference, true) as IRasterDataset2; IRaster2 raster2 = rasterDataSet.CreateDefaultRaster() as IRaster2; IPnt pntClass = new Pnt(); pntClass.X = rasterProps.Width; pntClass.Y = rasterProps.Height; IRasterCursor rasterCursor = raster2.CreateCursorEx(pntClass); IRasterCursor inRasterCursor = (rasterLayer.Raster as IRaster2).CreateCursorEx(pntClass); IRasterEdit rasterEdit = raster2 as IRasterEdit; if (rasterEdit.CanEdit()) { IPixelBlock3 pixelBlock3 = rasterCursor.PixelBlock as IPixelBlock3; IPixelBlock3 inPixelBlock3 = inRasterCursor.PixelBlock as IPixelBlock3; System.Array pixels = (System.Array)rasterMat; pixelBlock3.set_PixelData(0, (System.Array)pixels); rasterEdit.Write(rasterCursor.TopLeft, (IPixelBlock)pixelBlock3); System.Runtime.InteropServices.Marshal.ReleaseComObject(pixelBlock3); } rasterEdit.Refresh(); IGeoDataset inDataset = rasterLayer.Raster as IGeoDataset; IGeoDataset outDataset = rasterDataSet as IGeoDataset; IExtractionOp op = new RasterExtractionOpClass(); var outDataset1 = op.Raster(outDataset, inDataset); var clipRaster = (IRaster)outDataset1; ISaveAs pSaveAs = clipRaster as ISaveAs; System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterCursor); System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(raster2); System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterDataSet); System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterWorkspace2); System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFac); if (File.Exists(parth)) { File.Delete(parth); } workspaceFac = new RasterWorkspaceFactoryClass(); IDataset outdataset = pSaveAs.SaveAs(name, workspaceFac.OpenFromFile(directory, 0), "IMAGINE Image"); System.Runtime.InteropServices.Marshal.ReleaseComObject(outdataset); return; }
public static object getBlockStd(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object objVl = getBlockVariance(inArr, band, startColumn, startRows, numCellsInBlock); objVl = System.Convert.ToSingle(Math.Sqrt(System.Convert.ToDouble(objVl))); return(objVl); }
public static object getBlockMedian(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; Dictionary <float, int> unq = new Dictionary <float, int>(); int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; float n = 0; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlObj = inArr.GetVal(band, c, r); if (vlObj == null) { continue; } else { float vl = System.Convert.ToSingle(vlObj); n += 1; int cnt = 0; if (unq.TryGetValue(vl, out cnt)) { unq[vl] = cnt += 1; } else { unq.Add(vl, 1); } } } } float halfCnt = n / 2; float outvl = 0; List <float> sKeys = unq.Keys.ToList(); sKeys.Sort(); int nCnt = 0; foreach (int i in sKeys) { nCnt += unq[i]; if (nCnt >= halfCnt) { outvl = i; break; } } outVl = outvl; return(outVl); }
private void updateWithMergedValues(IEnvelope env, IPixelBlock3 ipPixelBlock) { System.Array[] outPixelValuesArr = new System.Array[ipPixelBlock.Planes]; List <System.Array[]> inPixelValuesArrLst = new List <System.Array[]>(); List <System.Array> inPixelNoDataArrLst = new List <System.Array>(); IPnt pntSize = new PntClass(); pntSize.SetCoords(ipPixelBlock.Width, ipPixelBlock.Height); ISpatialFilter spFlt = new SpatialFilterClass(); spFlt.Geometry = (IGeometry)env; spFlt.GeometryField = ftrCls.ShapeFieldName; spFlt.SpatialRel = esriSpatialRelEnum.esriSpatialRelOverlaps; IFeatureCursor fCur = ftrCls.Search(spFlt, false); int fIndex = ftrCls.FindField("catIndex"); IFeature ftr = fCur.NextFeature(); for (int i = 0; i < ipPixelBlock.Planes; i++) { outPixelValuesArr[i] = (System.Array)ipPixelBlock.get_PixelData(i); } while (ftr != null) { int rsIndex = System.Convert.ToInt32(ftr.get_Value(fIndex)); IRaster rs = inrs[rsIndex]; IPixelBlock inputPb = rs.CreatePixelBlock(pntSize); IRaster2 rs2 = (IRaster2)rs; int pClm, pRw; rs2.MapToPixel(env.XMin, env.YMax, out pClm, out pRw); IPnt tlc = new PntClass(); tlc.SetCoords(pClm, pRw); rs.Read(tlc, inputPb); System.Array[] inPixelValuesArr = new System.Array[inputPb.Planes]; for (int i = 0; i < inputPb.Planes; i++) { inPixelValuesArr[i] = (System.Array)inputPb.get_SafeArray(i); } inPixelNoDataArrLst.Add((System.Array)((IRasterProps)rs).NoDataValue); inPixelValuesArrLst.Add(inPixelValuesArr); ftr = fCur.NextFeature(); } for (int i = 0; i < outPixelValuesArr.Length; i++) { for (int r = 0; r < ipPixelBlock.Height; r++) { for (int c = 0; c < ipPixelBlock.Width; c++) { double vl = getValue(i, c, r, inPixelValuesArrLst, inPixelNoDataArrLst); outPixelValuesArr[i].SetValue(vl, c, r); } } } for (int i = 0; i < outPixelValuesArr.Length; i++) { ipPixelBlock.set_PixelData(i, outPixelValuesArr[i]); } }
public void Filter(IPixelBlock pPixelBlock) { try { IPixelBlock3 pPixelBlock3 = (IPixelBlock3)pPixelBlock; byte[] lookup = new byte[8] { 128, 64, 32, 16, 8, 4, 2, 1 }; //get number of bands int plane = pPixelBlock.Planes; //loop through each band for (int i = 0; i < plane; i++) { //get nodata mask array byte[] outputArray = (byte[])pPixelBlock3.get_NoDataMaskByRef(i); //loop through each pixel in the pixelblock and do calculation for (int x = 0; x < pPixelBlock.Width; x++) { for (int y = 0; y < pPixelBlock.Height; y++) { //get index in the nodata mask byte array int ind = x + y * (pPixelBlock.Width); //get nodata mask byte byte nd = outputArray[ind / 8]; //get pixel value and check if it is nodata object tempVal = pPixelBlock3.GetVal(i, x, y); if (tempVal != null) //not nodata pixel { //convert pixel value to int and compare with nodata range int curVal = Convert.ToInt32(tempVal); if (curVal >= minNodataValue && curVal <= maxNodataValue) { outputArray[ind / 8] = (byte)(nd & ~lookup[ind % 8]); } } } } //set nodata mask array pPixelBlock3.set_NoDataMask(i, outputArray); } } catch (Exception e) { Console.WriteLine(e.Message); } }
private bool writeBlockDataToFile(Point2D ptLeftTop, byte[,] dbData, int[] nSize, IRaster pRaster /*, IRasterEdit rasterEdit*/) { if (pRaster == null) { return(false); } try { //Create a pixel block using the weight and height of the raster dataset. //If the raster dataset is large, a smaller pixel block should be used. //Refer to the topic "How to access pixel data using a raster cursor". int nWidth = nSize[0]; int nHeight = nSize[1]; IPnt blocksize = new PntClass(); blocksize.SetCoords(nWidth, nHeight); IPixelBlock3 pixelblock = pRaster.CreatePixelBlock(blocksize) as IPixelBlock3; //Populate some pixel values to the pixel block. System.Array pixels; pixels = (System.Array)pixelblock.get_PixelData(0); for (int i = 0; i < nWidth; i++) { for (int j = 0; j < nHeight; j++) { pixels.SetValue(dbData[i, j], i, j); } } pixelblock.set_PixelData(0, (System.Array)pixels); //Define the location that the upper left corner of the pixel block is to write. IPnt upperLeft = new PntClass(); upperLeft.SetCoords(ptLeftTop.X, ptLeftTop.Y); //Write the pixel block. IRasterEdit rasterEdit = (IRasterEdit)pRaster; rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock); //System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit); rasterEdit.Refresh(); return(true); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Error: " + ex.Message); return(false); } GC.Collect(); return(true); }
public static object getBlockMode(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; Dictionary <float, int> unq = new Dictionary <float, int>(); int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; float n = 0; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlObj = inArr.GetVal(band, c, r); if (vlObj == null) { continue; } else { float vl = System.Convert.ToSingle(vlObj); n += 1; int cnt = 0; if (unq.TryGetValue(vl, out cnt)) { unq[vl] = cnt += 1; } else { unq.Add(vl, 1); } } } } int maxCnt = unq.Values.Max(); float outvl = 0; foreach (KeyValuePair <float, int> kVp in unq) { float k = kVp.Key; int v = kVp.Value; if (maxCnt == v) { outvl = k; break; } } outVl = outvl; return(outVl); }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue; myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; IPnt pbBigSize = new PntClass(); IPnt pbBigLoc = new PntClass(); int pbBigWd = pBWidth + clms; int pbBigHt = pBHeight + rws; int l, t; l = (clms - 1) / 2; t = (rws - 1) / 2; //Console.WriteLine("lt = " + (pTlc.X - l).ToString() + ":" + (pTlc.Y - t).ToString()); pbBigSize.SetCoords(pbBigWd, pbBigHt); pbBigLoc.SetCoords((pTlc.X - l), (pTlc.Y - t)); IPixelBlock3 pbBig = (IPixelBlock3)myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize); myFunctionHelperOrig.Read(pbBigLoc, null, myFunctionHelperOrig.Raster, (IPixelBlock)pbBig); for (int nBand = 0; nBand < pbBig.Planes; nBand++) { System.Array pixelValues = (System.Array)(ipPixelBlock.get_PixelData(nBand)); rstPixelType rsp = ipPixelBlock.get_PixelType(nBand); //System.Array pixelValuesBig = (System.Array)(pbBig.get_PixelData(nBand)); for (int r = 0; r < pBHeight; r++) { for (int c = 0; c < pBWidth; c++) { object objVl = ipPixelBlock.GetVal(nBand, c, r); if (objVl == null) { continue; } else { float outVl = System.Convert.ToSingle(getTransformedValue(pbBig, c + l, r + t, nBand)); object newVl = rasterUtil.getSafeValue(outVl, rsp); //Console.WriteLine(outVl.ToString()); pixelValues.SetValue(newVl, c, r); } } } ((IPixelBlock3)pPixelBlock).set_PixelData(nBand, pixelValues); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
/// <summary> /// 栅格操作修改栅格的值 /// </summary> /// <param name="pRasterDataset2"></param> public static void ChangeRasterValue(IRasterDataset2 pRasterDataset2) { //设置读取栅格的大小 IRaster2 pRaster2 = pRasterDataset2.CreateFullRaster() as IRaster2; IPnt pPntBlock = new PntClass(); pPntBlock.X = 128; pPntBlock.Y = 128; IRasterCursor pRasterCursor = pRaster2.CreateCursorEx(pPntBlock); IRasterEdit pRasterEdit = pRaster2 as IRasterEdit; if (pRasterEdit.CanEdit()) { //循环波段,长和宽度 IRasterBandCollection pBands = pRasterDataset2 as IRasterBandCollection; IPixelBlock3 pPixelblock3 = null; int pBlockwidth = 0; int pBlockheight = 0; System.Array pixels; object pValue; long pBandCount = pBands.Count; // IRasterProps pRasterProps = pRaster2 as IRasterProps; object nodata = pRasterProps.NoDataValue; do { pPixelblock3 = pRasterCursor.PixelBlock as IPixelBlock3; pBlockwidth = pPixelblock3.Width; pBlockheight = pPixelblock3.Height; for (int k = 0; k < pBandCount; k++) { pixels = (System.Array)pPixelblock3.get_PixelData(k); for (int i = 0; i < pBlockwidth; i++) { for (int j = 0; j < pBlockheight; j++) { pValue = pixels.GetValue(i, j); int value = Convert.ToInt32(pValue); if (Convert.ToInt32(pValue) != 3) { pixels.SetValue(Convert.ToByte(0), i, j); } } } pPixelblock3.set_PixelData(k, pixels); } pPntBlock = pRasterCursor.TopLeft; pRasterEdit.Write(pPntBlock, (IPixelBlock)pPixelblock3); } while (pRasterCursor.Next()); System.Runtime.InteropServices.Marshal.ReleaseComObject(pRasterEdit); } }
private object calcMeanShift(IPixelBlock3 vPb, IPixelBlock3 pb3) { double[][] jaArr = pixelBlockToJaggedArray(vPb); int bands = vPb.Planes; UniformKernel kernel = new UniformKernel(); //GaussianKernel kernel = new GaussianKernel(bands); MeanShift ms = new MeanShift(bands, kernel, radius); int[] vls = ms.Compute(jaArr, 0.05, 10); NumClusters = ms.Clusters.Count; Console.WriteLine(NumClusters); return(splitArray(vls, pb3)); }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { //System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue; myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; IPnt pbBigSize = new PntClass(); IPnt pbBigLoc = new PntClass(); int pbBigWd = pBWidth + clms; int pbBigHt = pBHeight + rws; int l, t; l = clms / 2; t = rws / 2; pbBigSize.SetCoords(pbBigWd, pbBigHt); pbBigLoc.SetCoords((pTlc.X - l), (pTlc.Y - t)); IPixelBlock3 pbBig = (IPixelBlock3)myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize); myFunctionHelperOrig.Read(pbBigLoc, null, myFunctionHelperOrig.Raster, (IPixelBlock)pbBig); noDataValue = float.MinValue;//System.Convert.ToSingle(noDataValueArr.GetValue(0)); for (int nBand = 0; nBand < ipPixelBlock.Planes; nBand++) { rstPixelType pbt = ipPixelBlock.get_PixelType(nBand); System.Array pixelValues = (System.Array)(ipPixelBlock.get_PixelData(nBand)); for (int r = 0; r < pBHeight; r++) { for (int c = 0; c < pBWidth; c++) { object inVlobj = ipPixelBlock.GetVal(nBand, c, r); if (inVlobj == null) { continue; } else { updateGLCMDic(pbBig, c, r, nBand); float outVl = System.Convert.ToSingle(getTransformedValue(countDic)); object newVl = rasterUtil.getSafeValue(outVl, pbt); pixelValues.SetValue(newVl, c, r); } } } ((IPixelBlock3)pPixelBlock).set_PixelData(nBand, pixelValues); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { // Call Read method of the Raster Function Helper object. myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPnt pbSize = new PntClass(); pbSize.SetCoords(pBWidth, pBHeight); IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array[] inArr = new System.Array[inrsBands.RasterInfo.BandCount]; IRasterBandCollection rsBc = (IRasterBandCollection)inrsBands; for (int p = 0; p < inArr.Length; p++) { IRasterBand rsB = rsBc.Item(p); IRawPixels rP = (IRawPixels)rsB; IPixelBlock pb = rP.CreatePixelBlock(pbSize); IPixelBlock3 pb3 = (IPixelBlock3)pb; rP.Read(pTlc, pb); inArr[p] = (System.Array)pb3.get_PixelData(0); } System.Array outArr = (System.Array)pPixelBlock.get_SafeArray(0); rstPixelType rsPt = pPixelBlock.get_PixelType(0); for (int r = 0; r < ipPixelBlock.Height; r++) { for (int c = 0; c < ipPixelBlock.Width; c++) { object outVlObj = ipPixelBlock.GetVal(0, c, r); if (outVlObj != null) { float outVl; if (getOutPutVl(inArr, c, r, out outVl)) { object newVl = rasterUtil.getSafeValue(outVl, rsPt);//convertVl(outVl,rsPt); outArr.SetValue(newVl, c, r); } } } } ipPixelBlock.set_PixelData(0, outArr); } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of localMean Function. " + exc.Message, exc); Console.Write(exc.ToString()); throw myExc; } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); //System.Windows.Forms.MessageBox.Show("Read resized raster"); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; IPnt pbBigSize = new PntClass(); IPnt pbBigLoc = new PntClass(); int pbBigWd = pBWidth * cells; int pbBigHt = pBHeight * cells; pbBigSize.SetCoords(pbBigWd, pbBigHt); pbBigLoc.SetCoords((pTlc.X * cells), (pTlc.Y * cells)); IPixelBlock pbOrig = myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize); //System.Windows.Forms.MessageBox.Show("Read original Raster"); myFunctionHelperOrig.Read(pbBigLoc, null, myFunctionHelperOrig.Raster, pbOrig); IPixelBlock3 pbBig = (IPixelBlock3)pbOrig; //int cnt = 0; for (int nBand = 0; nBand < ipPixelBlock.Planes; nBand++) { //Console.WriteLine(ipPixelBlock.get_PixelType(nBand).ToString()); //object noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(nBand)); //System.Array pixelValuesBig = (System.Array)(pbBig.get_PixelData(nBand)); rstPixelType pTy = pbBig.get_PixelType(nBand); System.Array pixelValues = (System.Array)(ipPixelBlock.get_PixelData(nBand)); for (int r = 0; r < pBHeight; r++) { for (int c = 0; c < pBWidth; c++) { object objVl = ipPixelBlock.GetVal(nBand, c, r); if (objVl != null) { object outVl = getTransformedValue(pbBig, nBand, c, r, cells); object newVl = rasterUtil.getSafeValue(outVl, pTy); //System.Windows.Forms.MessageBox.Show(outVl.ToString()); pixelValues.SetValue(newVl, c, r); } } } ipPixelBlock.set_PixelData(nBand, pixelValues); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { // Call Read method of the Raster Function Helper object. myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPnt pbSize = new PntClass(); pbSize.SetCoords(pBWidth, pBHeight); IPixelBlock3 outPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize); myFunctionHelperCoef.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)outPb); int pBRowIndex = 0; int pBColIndex = 0; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array outArr = (System.Array)ipPixelBlock.get_PixelData(0); rstPixelType pty = ipPixelBlock.get_PixelType(0); double[] vArr = new double[outPb.Planes]; for (int i = pBRowIndex; i < pBHeight; i++) { for (int k = pBColIndex; k < pBWidth; k++) { bool checkVl = true; for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++) { object vlObj = outPb.GetVal(0, k, i); if (vlObj == null) { checkVl = false; break; } vArr[coefnBand] = System.Convert.ToDouble(vlObj); } if (checkVl) { double tVl = glm.computeNew(vArr); object newVl = rasterUtil.getSafeValue(tVl, pty); outArr.SetValue(newVl, k, i); } } } ipPixelBlock.set_PixelData(0, outArr); } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of GLM Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }
public static object getBlockEntropy(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; Dictionary<float, int> unq = new Dictionary<float, int>(); int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; float n = 0; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlObj = inArr.GetVal(band, c, r); if (vlObj == null) { continue; } else { float vl = System.Convert.ToSingle(vlObj); n += 1; int cnt = 0; if (unq.TryGetValue(vl, out cnt)) { unq[vl] = cnt += 1; } else { unq.Add(vl, 1); } } } } float outvl = 0; foreach (int i in unq.Values) { float prob = System.Convert.ToSingle(i) / n; outvl += prob * System.Convert.ToSingle(Math.Log(prob)); } outVl= -1 * outvl; return outVl; }
public void Bind(object pArgument) { if (pArgument is PixelBlockToRasterFunctionArguments) { PixelBlockToRasterFunctionArguments args = (PixelBlockToRasterFunctionArguments)pArgument; inrs = args.InRaster; vPb = (IPixelBlock3)args.ValuePixelBlock; tlLoc = args.TopLeft; myFunctionHelper.Bind(inrs); myRasterInfo = myFunctionHelper.RasterInfo; myPixeltype = myRasterInfo.PixelType; myValidFlag = true; } else { throw new System.Exception("Incorrect arguments object. Expected: PixelBlockToRasterArguments"); } }
private void updateWithMergedValues(IEnvelope env, IPixelBlock3 ipPixelBlock) { System.Array[] outPixelValuesArr = new System.Array[ipPixelBlock.Planes]; IPnt pntSize = new PntClass(); pntSize.SetCoords(ipPixelBlock.Width, ipPixelBlock.Height); ISpatialFilter spFlt = new SpatialFilterClass(); spFlt.Geometry = (IGeometry)env; spFlt.GeometryField = ftrCls.ShapeFieldName; spFlt.SpatialRel = esriSpatialRelEnum.esriSpatialRelIndexIntersects; IFeatureCursor fCur = ftrCls.Search(spFlt, false); ////int fIndex = ftrCls.FindField("catIndex"); IFeature ftr = fCur.NextFeature(); for (int i = 0; i < ipPixelBlock.Planes; i++) { outPixelValuesArr[i] = (System.Array)ipPixelBlock.get_PixelData(i); } //IQueryFilter qf = new QueryFilterClass(); //string s1 = "(XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")"; //string s2 = "(XMIN > " + env.XMin.ToString() + " AND XMIN < " + env.XMax.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")"; //string s3 = "(XMAX < " + env.XMax.ToString() + " AND XMIN > " + env.XMin.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")"; //string s4 = "(YMIN > " + env.YMin.ToString() + " AND YMIN < " + env.YMax.ToString() + " AND XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + ")"; //string s5 = "(YMAX < " + env.YMax.ToString() + " AND YMIN > " + env.YMin.ToString() + " AND XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + ")"; //qf.WhereClause = s1 + " OR " + s2 + " OR " + s3 + " OR " + s4 + " OR " + s5; //ICursor cur = tbl.Search(qf, false); //IRow rw = cur.NextRow(); List<IPixelBlock> inPbValue = new List<IPixelBlock>(); //while (rw != null) //{ // int rsIndex = System.Convert.ToInt32(rw.get_Value(catIndex)); // IRaster rs = inrs[rsIndex]; // IPixelBlock inputPb = rs.CreatePixelBlock(pntSize); // IRaster2 rs2 = (IRaster2)rs; // int pClm, pRw; // rs2.MapToPixel(env.XMin, env.YMax, out pClm, out pRw); // IPnt tlc = new PntClass(); // tlc.SetCoords(pClm, pRw); // rs.Read(tlc, inputPb); // inPbValue.Add(inputPb); // rw = cur.NextRow(); //} while (ftr != null) { int rsIndex = System.Convert.ToInt32(ftr.get_Value(catIndex)); IRaster rs = inrs[rsIndex]; IPixelBlock inputPb = rs.CreatePixelBlock(pntSize); IRaster2 rs2 = (IRaster2)rs; int pClm, pRw; rs2.MapToPixel(env.XMin, env.YMax, out pClm, out pRw); IPnt tlc = new PntClass(); tlc.SetCoords(pClm, pRw); rs.Read(tlc, inputPb); inPbValue.Add(inputPb); ftr = fCur.NextFeature(); } for (int i = 0; i < ipPixelBlock.Planes; i++) { for (int r = 0; r < ipPixelBlock.Height; r++) { for (int c = 0; c < ipPixelBlock.Width; c++) { object vl = getValue(i, c, r, inPbValue); outPixelValuesArr[i].SetValue(vl,c,r); } } } for (int i = 0; i < outPixelValuesArr.Length; i++) { ipPixelBlock.set_PixelData(i, outPixelValuesArr[i]); } }
public static Object getBlockMax(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; float outVlF = Single.MinValue; int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlObj = inArr.GetVal(band, c, r); if (vlObj == null) { continue; } else { float vl = System.Convert.ToSingle(vlObj); if (vl > outVlF) outVlF = vl; } } } outVl = outVlF; return outVl; }
public static object getBlockMedian(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; Dictionary<float, int> unq = new Dictionary<float, int>(); int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; float n = 0; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlObj = inArr.GetVal(band, c, r); if (vlObj == null) { continue; } else { float vl = System.Convert.ToSingle(vlObj); n += 1; int cnt = 0; if (unq.TryGetValue(vl, out cnt)) { unq[vl] = cnt += 1; } else { unq.Add(vl, 1); } } } } float halfCnt = n / 2; float outvl = 0; List<float> sKeys = unq.Keys.ToList(); sKeys.Sort(); int nCnt = 0; foreach (int i in sKeys) { nCnt += unq[i]; if (nCnt >= halfCnt) { outvl = i; break; } } outVl = outvl; return outVl; }
public static object getBlockMode(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; Dictionary<float, int> unq = new Dictionary<float, int>(); int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; float n = 0; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlObj = inArr.GetVal(band, c, r); if (vlObj == null) { continue; } else { float vl = System.Convert.ToSingle(vlObj); n += 1; int cnt = 0; if (unq.TryGetValue(vl, out cnt)) { unq[vl] = cnt += 1; } else { unq.Add(vl, 1); } } } } int maxCnt = unq.Values.Max(); float outvl = 0; foreach (KeyValuePair<float, int> kVp in unq) { float k = kVp.Key; int v = kVp.Value; if (maxCnt == v) { outvl = k; break; } } outVl = outvl; return outVl; }
private object calcMeanShift(IPixelBlock3 vPb,IPixelBlock3 pb3) { double[][] jaArr = pixelBlockToJaggedArray(vPb); int bands = vPb.Planes; UniformKernel kernel = new UniformKernel(); //GaussianKernel kernel = new GaussianKernel(bands); MeanShift ms = new MeanShift(bands, kernel, radius); int[] vls = ms.Compute(jaArr, 0.05, 10); NumClusters = ms.Clusters.Count; Console.WriteLine(NumClusters); return splitArray(vls, pb3); }
public abstract void getOutPutVl(IPixelBlock3 pb, System.Array[] outArr, float vl, int c, int r);
private void lookAtNeighbor(int vl, IPixelBlock3 inputPb, System.Array outputPixelValues, int c, int r, List<int[]> rwClmCheckLst) { int nVl = System.Convert.ToInt32(outputPixelValues.GetValue(c, r)); if (nVl == 0) { object nClusVlobj = inputPb.GetVal(0, c, r); if (nClusVlobj == null) outputPixelValues.SetValue(nClusVlobj, c, r); else { int nClusVl = System.Convert.ToInt32(nClusVlobj); if (nClusVl == vl) { outputPixelValues.SetValue(regionCounter, c, r); rwClmCheckLst.Add(new int[] { c, r }); } } } }
public static object getBlockUnique(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; HashSet<float> unq = new HashSet<float>(); int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlObj = inArr.GetVal(band, c, r); if (vlObj == null) { continue; } else { float vl = System.Convert.ToSingle(vlObj); unq.Add(vl); } } } outVl = unq.Count; return outVl; }
public static object getBlockVariance(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; float s = 0; float s2 = 0; float n = 0; int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlObj = inArr.GetVal(band, c, r); if (vlObj == null) { continue; } else { float vl = System.Convert.ToSingle(vlObj); n += 1; s += vl; s2 += vl * vl; } } } outVl = (s2 - ((s * s) / n)) / n; return outVl; }
private void updateWithMergedValues(IEnvelope env, IPixelBlock3 ipPixelBlock) { System.Array[] outPixelValuesArr = new System.Array[ipPixelBlock.Planes]; List<System.Array[]> inPixelValuesArrLst = new List<System.Array[]>(); List<System.Array> inPixelNoDataArrLst = new List<System.Array>(); IPnt pntSize = new PntClass(); pntSize.SetCoords(ipPixelBlock.Width, ipPixelBlock.Height); ISpatialFilter spFlt = new SpatialFilterClass(); spFlt.Geometry = (IGeometry)env; spFlt.GeometryField = ftrCls.ShapeFieldName; spFlt.SpatialRel = esriSpatialRelEnum.esriSpatialRelOverlaps; IFeatureCursor fCur = ftrCls.Search(spFlt, false); int fIndex = ftrCls.FindField("catIndex"); IFeature ftr = fCur.NextFeature(); for (int i = 0; i < ipPixelBlock.Planes; i++) { outPixelValuesArr[i] = (System.Array)ipPixelBlock.get_PixelData(i); } while (ftr != null) { int rsIndex = System.Convert.ToInt32(ftr.get_Value(fIndex)); IRaster rs = inrs[rsIndex]; IPixelBlock inputPb = rs.CreatePixelBlock(pntSize); IRaster2 rs2 = (IRaster2)rs; int pClm, pRw; rs2.MapToPixel(env.XMin, env.YMax, out pClm, out pRw); IPnt tlc = new PntClass(); tlc.SetCoords(pClm, pRw); rs.Read(tlc, inputPb); System.Array[] inPixelValuesArr = new System.Array[inputPb.Planes]; for (int i = 0; i < inputPb.Planes; i++) { inPixelValuesArr[i] = (System.Array)inputPb.get_SafeArray(i); } inPixelNoDataArrLst.Add((System.Array)((IRasterProps)rs).NoDataValue); inPixelValuesArrLst.Add(inPixelValuesArr); ftr = fCur.NextFeature(); } for (int i = 0; i < outPixelValuesArr.Length; i++) { for (int r = 0; r < ipPixelBlock.Height; r++) { for (int c = 0; c < ipPixelBlock.Width; c++) { double vl = getValue(i, c, r, inPixelValuesArrLst, inPixelNoDataArrLst); outPixelValuesArr[i].SetValue(vl,c,r); } } } for (int i = 0; i < outPixelValuesArr.Length; i++) { ipPixelBlock.set_PixelData(i, outPixelValuesArr[i]); } }
public override object getTransformedValue(IPixelBlock3 bigArr, int band, int startClms, int startRws, int cells) { return blockHelperStats.getBlockAsm(bigArr, band, startClms, startRws, cells); }
public abstract object getTransformedValue(IPixelBlock3 bigPb, int startClm, int startRw, int nBand);
private void updateGLCMDic(IPixelBlock3 pbBig, int startClm, int startRw, int nBand) { foreach (coordPair[] xy in iter) { coordPair sub = xy[0]; coordPair add = xy[1]; #region subtract from dictionary try { int c = sub.X + startClm; int r = sub.Y + startRw; int sc = sub.NX + startClm; int sr = sub.NY + startRw; object objVl1 = pbBig.GetVal(nBand, c, r); object objVl2 = pbBig.GetVal(nBand, sc, sr); if (objVl1 == null || objVl2 == null) { continue; } else { int cnt = 0; string pair1 = objVl1.ToString() + ":" + objVl2.ToString(); string pair2 = objVl2.ToString() + ":" + objVl1.ToString(); if (countDic.TryGetValue(pair1, out cnt)) { cnt = cnt - 1; if (cnt == 0) { countDic.Remove(pair1); } else { countDic[pair1] = cnt; } } else { } if (countDic.TryGetValue(pair2, out cnt)) { cnt = cnt - 1; if (cnt == 0) { countDic.Remove(pair2); } else { countDic[pair2] = cnt; } } else { } } } catch(Exception e) { Console.WriteLine(e.ToString()); } #endregion #region add values to dicitonary try { int c = add.X + startClm; int r = add.Y + startRw; int sc = add.NX + startClm; int sr = add.NY + startRw; object objVl1 = pbBig.GetVal(nBand, c, r); object objVl2 = pbBig.GetVal(nBand, sc, sr); if (objVl1 == null || objVl2 == null) { continue; } else { int cnt = 0; string pair1 = objVl1.ToString() + ":" + objVl2.ToString(); string pair2 = objVl2.ToString() + ":" + objVl1.ToString(); if (countDic.TryGetValue(pair1, out cnt)) { countDic[pair1] = cnt + 1; } else { countDic.Add(pair1, 1); } if (countDic.TryGetValue(pair2, out cnt)) { countDic[pair2] = cnt + 1; } else { countDic.Add(pair2, 1); } } } catch(Exception e) { Console.WriteLine(e.ToString()); } #endregion } }
private void checkNeighbors(int vl, IPixelBlock3 inputPb, System.Array outputPixelValues, int c, int r, List<int[]> rwClmCheckLst) { int lastWidthcell = inputPb.Width; int lastHeightcell = inputPb.Height; int clmPlus = c+1; int clmMinus = c-1; int rwPlus = r+1; int rwMinus = r-1; if (clmPlus < lastWidthcell) { lookAtNeighbor(vl, inputPb, outputPixelValues, clmPlus, r, rwClmCheckLst); } else { } if (clmMinus >= 0) { lookAtNeighbor(vl, inputPb, outputPixelValues, clmMinus, r, rwClmCheckLst); } else { } if (rwPlus < lastHeightcell) { lookAtNeighbor(vl, inputPb, outputPixelValues, c, rwPlus, rwClmCheckLst); } else { } if (rwMinus >= 0) { lookAtNeighbor(vl, inputPb, outputPixelValues, c, rwMinus, rwClmCheckLst); } else { } }
private void createRegions(IPixelBlock3 inputPb, System.Array outputPixelValues,int sClms = 0, int sRws = 0) { int clms = inputPb.Width; int rws = inputPb.Height; for (int r = sRws; r < rws; r++) { for (int c = sClms; c < clms; c++) { object vlObj = inputPb.GetVal(0, c, r); if (vlObj == null) outputPixelValues.SetValue(vlObj,c,r); else { int vlObj2 = System.Convert.ToInt32(outputPixelValues.GetValue(c,r)); if (vlObj2 == 0) { outputPixelValues.SetValue(regionCounter, c, r); int vl = System.Convert.ToInt32(vlObj); List<int[]> rwClmCheckLst = new List<int[]>(); checkNeighbors(vl, inputPb, outputPixelValues, c, r, rwClmCheckLst); while (rwClmCheckLst.Count > 0) { int[] clmRw = rwClmCheckLst[0]; checkNeighbors(vl, inputPb, outputPixelValues, clmRw[0], clmRw[1], rwClmCheckLst); rwClmCheckLst.RemoveAt(0); } regionCounter++; } else { continue; } } } } }
private void updateOutArr(IPixelBlock3 outPb, System.Array[] inArr, System.Array outArr) { int bands = inArr.Length; string[] sArr = new string[bands]; System.Array[] vlArr = new System.Array[bands]; rstPixelType pTy = outPb.get_PixelType(0); for (int r = 0; r < outPb.Height; r++) { for (int c = 0; c < outPb.Width; c++) { bool ch = true; for (int p = 0; p < bands; p++) { object inObj = inArr[p].GetValue(c, r); if (inObj == null) { ch = false; break; } else { sArr[p] = inObj.ToString(); } } if (ch) { int vl = uniqueCounter; string sVls = String.Join(",", sArr); if (!uniqueDic.TryGetValue(sVls, out vl)) { uniqueCounter += 1; vl = uniqueCounter; uniqueDic.Add(sVls, vl); } object newVl = rasterUtil.getSafeValue(vl, pTy); outArr.SetValue(newVl, c, r); } } } }
public abstract void updatePixelRectangle(IPixelBlock3 pbIn, IPixelBlock3 pbInBig);
public static object getBlockSum(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object outVl = 0; float outVlF = 0; int stC = startColumn * numCellsInBlock; int stR = startRows * numCellsInBlock; int height = stR + numCellsInBlock; int width = stC + numCellsInBlock; for (int r = stR; r < height; r++) { for (int c = stC; c < width; c++) { object vlobj = inArr.GetVal(band, c, r); float vl = 0; if (vlobj == null) { vl = 0; } else { vl = System.Convert.ToSingle(vlobj); } outVlF = outVlF + vl; } } outVl = outVlF; return outVl; }
private double[][] pixelBlockToJaggedArray(IPixelBlock3 pb3) { int width = pb3.Width; int height = pb3.Height; int bands = pb3.Planes; //Console.WriteLine("Pixel Block width and height = " + width.ToString() + "; " + height.ToString()); double[][] outArr = new double[width*height][]; for (int r = 0; r < height; r++) { int indexVlr = r*width; int indexVl = indexVlr; for (int c = 0; c < width; c++) { indexVl = indexVlr + c; double[] bndsArr = new double[bands]; for (int p = 0; p < bands; p++) { object objVl = pb3.GetVal(p, c, r); if (objVl != null) { bndsArr[p] = System.Convert.ToDouble(objVl); } } //Console.WriteLine("Index value = " + indexVl.ToString() + "; " + width.ToString() + "; " + height.ToString() +"; "+ r.ToString()+"; " + c.ToString()); outArr[indexVl] = bndsArr; } } return outArr; }
public static object getBlockStd(IPixelBlock3 inArr, int band, int startColumn, int startRows, int numCellsInBlock) { object objVl = getBlockVariance(inArr, band, startColumn, startRows, numCellsInBlock); objVl= System.Convert.ToSingle(Math.Sqrt(System.Convert.ToDouble(objVl))); return objVl; }
private object splitArray(int[] vls, IPixelBlock3 pb3) { int width = pb3.Width; int height = pb3.Height; int cnt = 0; System.Array outArr = (System.Array)pb3.get_PixelData(0); rstPixelType rsp = pb3.get_PixelType(0); foreach (int i in vls) { double div = System.Convert.ToDouble(cnt) / width; int r = (int)div; int c = cnt - (r * width); try { object newvl = rasterUtil.getSafeValue(i, rsp); outArr.SetValue(i, c, r); } catch { object newvl = rasterUtil.getSafeValue(900, rsp); outArr.SetValue(900, c, r); } cnt++; } return outArr; }
public abstract object getTransformedValue(IPixelBlock3 bigArr,int band, int startClms,int startRws,int cells);