/// <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()); } }
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); }
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 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); }
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); }
/// <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); #region Load log object int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; Pnt pbSize = new PntClass(); pbSize.SetCoords(pPixelBlock.Width, pPixelBlock.Height); IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array outPutArr = (System.Array)ipPixelBlock.get_PixelData(0); IPixelBlock3 CoefPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize); myFunctionHelperCoef.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)CoefPb); for (int r = 0; r < ipPixelBlock.Height; r++) { for (int c = 0; c < ipPixelBlock.Width; c++) { object vlObj = CoefPb.GetVal(0, c, r); if (vlObj != null) { if ((float)vlObj > 0) { vlObj = CoefPb.GetVal(1, c, r); } else { vlObj = CoefPb.GetVal(2, c, r); } if (vlObj != null) { outPutArr.SetValue(vlObj, c, r); } } } } ipPixelBlock.set_PixelData(0, outPutArr); #endregion } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of con Function. " + exc.Message, exc); throw myExc; } }
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 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); } }
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> /// 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 { 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 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 { // 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 getBlockAsm(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 * prob; } outVl = outvl; return(outVl); }
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; }
/// <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); rstPixelType pbT = pb3.get_PixelType(nBand); for (int r = 0; r < pPixelBlock.Height; r++) { for (int c = 0; c < pPixelBlock.Width; c++) { object objVl = pb3.GetVal(nBand, c, r); if (objVl == null) { continue; } else { //vl = System.Convert.ToSingle(objVl); //pixelValue = (float)getFunctionValue(vl); double objVl2 = getFunctionValue(System.Convert.ToDouble(objVl)); object newVl = rasterUtil.getSafeValue(objVl2, pbT); dArr.SetValue(newVl, c, r); } } } 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; } }
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 }); } } } }
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; } } } } }
/// <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); IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array[] outArr = new System.Array[bndCnt]; int pbHeight = ipPixelBlock.Height; int pbWidth = ipPixelBlock.Width; for (int p = 0; p < ipPixelBlock.Planes; p++) { outArr[p] = (System.Array)ipPixelBlock.get_PixelData(p); } for (int r = 0; r < pbHeight; r++) { for (int c = 0; c < pbWidth; c++) { object vlObj = ipPixelBlock.GetVal(0, c, r); if (vlObj != null) { float vl = System.Convert.ToSingle(vlObj); getOutPutVl(ipPixelBlock, outArr, vl, c, r); } } } for (int p = 0; p < ipPixelBlock.Planes; p++) { ipPixelBlock.set_PixelData(p, outArr[p]); } } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of localMean Function. " + exc.Message, exc); throw myExc; } }
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; } } } } }
/// <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)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);//independent variables myFunctionHelperCoef.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)outPb); int pBRowIndex = 0; int pBColIndex = 0; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; //System.Array[] pArr = new System.Array[outPb.Planes]; System.Array[] cArr = new System.Array[ipPixelBlock.Planes]; //for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++) //{ // System.Array pixelValues = (System.Array)(outPb.get_PixelData(coefnBand)); // pArr[coefnBand] = pixelValues; //} for (int outBand = 0; outBand < ipPixelBlock.Planes; outBand++) { //float[,] td = new float[ipPixelBlock.Width, ipPixelBlock.Height]; System.Array pixelValues = (System.Array)ipPixelBlock.get_PixelData(outBand);//(System.Array)(td); cArr[outBand] = pixelValues; } 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) { double[] pp = pca.computNew(xVls); for (int p = 0; p < pp.Length; p++) { rstPixelType pty = ipPixelBlock.get_PixelType(p); double pVl = pp[p]; object newVl = rasterUtil.getSafeValue(pVl, pty); cArr[p].SetValue(newVl, k, i); } } else { for (int p = 0; p < ipPixelBlock.Planes; p++) { cArr[p].SetValue(0, k, i); } } } } for (int i = 0; i < ipPixelBlock.Planes; i++) { ipPixelBlock.set_PixelData(i, cArr[i]); } } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of pca Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }
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 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 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 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; }
private string getSummaryValue(string iteration, int fire, int Period, IFunctionRasterDataset final10, IFunctionRasterDataset final50, IFunctionRasterDataset arivZones) { IRasterFunctionHelper arFH = new RasterFunctionHelperClass(); IRasterFunctionHelper f10FH = new RasterFunctionHelperClass(); IRasterFunctionHelper f50FH = new RasterFunctionHelperClass(); arFH.Bind(arivZones); f10FH.Bind(final10); f50FH.Bind(final50); Dictionary <int, double[]> vlDic = new Dictionary <int, double[]>(); IRasterCursor rsCur = ((IRaster2)arFH.Raster).CreateCursorEx(null); IPnt pnt = new PntClass(); pnt.X = rsCur.PixelBlock.Width; pnt.Y = rsCur.PixelBlock.Height; IRasterCursor rsCur2 = ((IRaster2)f10FH.Raster).CreateCursorEx(null); IRasterCursor rsCur3 = ((IRaster2)f50FH.Raster).CreateCursorEx(null); IPixelBlock3 pb = null; IPixelBlock3 pb2 = null; IPixelBlock3 pb3 = null; while (rsCur.Next() && rsCur2.Next() && rsCur3.Next()) { pb = (IPixelBlock3)rsCur.PixelBlock; pb2 = (IPixelBlock3)rsCur2.PixelBlock; pb3 = (IPixelBlock3)rsCur3.PixelBlock; int ht = pb.Height; int wd = pb.Width; for (int h = 0; h < ht; h++) { for (int w = 0; w < wd; w++) { object atobj = pb.GetVal(0, w, h); object f10obj = pb2.GetVal(0, w, h); object f50obj = pb3.GetVal(0, w, h); if (atobj == null || f10obj == null || f50obj == null) { continue; } else { int at = System.Convert.ToInt32(atobj); double f10 = System.Convert.ToDouble(f10obj); double f50 = System.Convert.ToDouble(f50obj); double[] fArr = { 0, 0, 0 }; if (vlDic.TryGetValue(at, out fArr)) { fArr[0] = fArr[0] + f10; fArr[1] = fArr[1] + f50; fArr[2] = fArr[2] + 1; vlDic[at] = fArr; } else { fArr = new double[] { f10, f50, 1 }; vlDic.Add(at, fArr); } } } } } StringBuilder sb = new StringBuilder(); //need to sort to do accumulative; List <int> keySortLst = vlDic.Keys.ToList(); keySortLst.Sort(); double ac10 = 0; double ac50 = 0; foreach (int ky in keySortLst) { double[] vlArr = vlDic[ky]; double s10 = vlArr[0]; double s50 = vlArr[1]; ac10 += s10; ac50 += s50; double cellCnt = vlArr[2]; string newStr = iteration + "," + fire.ToString() + "," + Period.ToString() + "," + s10.ToString() + "," + s50.ToString() + "," + ac10.ToString() + "," + ac50.ToString() + "," + ky.ToString() + "," + cellCnt.ToString(); //Console.WriteLine(newStr); sb.AppendLine(newStr); } return(sb.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. //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)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);//independent variables myFunctionHelperCoef.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)outPb); int pBRowIndex = 0; int pBColIndex = 0; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array[] pArr = new System.Array[ipPixelBlock.Planes]; for (int coefnBand = 0; coefnBand < ipPixelBlock.Planes; coefnBand++) { System.Array pixelValues = (System.Array)(pPixelBlock.get_SafeArray(coefnBand)); pArr[coefnBand] = pixelValues; } for (int i = pBRowIndex; i < pBHeight; i++) { for (int k = pBColIndex; k < pBWidth; k++) { object pObj = outPb.GetVal(0, k, i); if (pObj == null) { continue; } else { string pixelValue = pObj.ToString(); double[] c; if (tDic.TryGetValue(pixelValue, out c)) { for (int v = 0; v < pArr.Length; v++) { double vl = c[v]; object newVl = rasterUtil.getSafeValue(vl, ipPixelBlock.get_PixelType(v)); pArr[v].SetValue(newVl, k, i); } } else { for (int v = 0; v < pArr.Length; v++) { pArr.SetValue(ndVl, k, i); } } } } } for (int i = 0; i < pArr.Length; i++) { ipPixelBlock.set_PixelData(i, pArr[i]); } } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of TTest Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }
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 }); } } } }
/// <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); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPnt pbSize = new PntClass(); pbSize.SetCoords(pBWidth, pBHeight); IPixelBlock3 outPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);//independent variables myFunctionHelperCoef.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)outPb); int pBRowIndex = 0; int pBColIndex = 0; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array[] cArr = new System.Array[ipPixelBlock.Planes]; for (int outBand = 0; outBand < ipPixelBlock.Planes; outBand++) { System.Array pixelValues = (System.Array)ipPixelBlock.get_PixelData(outBand);//(System.Array)(td); cArr[outBand] = pixelValues; } for (int i = pBRowIndex; i < pBHeight; i++) { for (int k = pBColIndex; k < pBWidth; k++) { bool ndT = true; for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++) { object pObj = outPb.GetVal(coefnBand, k, i); if (pObj == null) { ndT = false; break; } double pixelValue = Convert.ToDouble(pObj); xVls[coefnBand] = pixelValue; } if (ndT) { try { double[] pp = lm.computNew(xVls); for (int p = 0; p < pp.Length; p++) { double pVl = pp[p]; object spVl = rasterUtil.getSafeValue(pVl, ipPixelBlock.get_PixelType(p)); cArr[p].SetValue(spVl, k, i); } } catch (Exception e) { Console.WriteLine(e.ToString()); for (int p = 0; p < ipPixelBlock.Planes; p++) { cArr[p].SetValue(0, k, i); } } } else { for (int p = 0; p < ipPixelBlock.Planes; p++) { cArr[p].SetValue(0, k, i); } } } } for (int i = 0; i < ipPixelBlock.Planes; i++) { ipPixelBlock.set_PixelData(i, cArr[i]); } } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of Regression Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }
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; }
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; }
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 resampeling. /// 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); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPnt pbSize = new PntClass(); pbSize.SetCoords(pBWidth, pBHeight); IPixelBlock3 outPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);//independent variables myFunctionHelperCoef.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)outPb); int pBRowIndex = 0; int pBColIndex = 0; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array[] cArr = new System.Array[ipPixelBlock.Planes]; for (int outBand = 0; outBand < ipPixelBlock.Planes; outBand++) { System.Array pixelValues = (System.Array)ipPixelBlock.get_PixelData(outBand); cArr[outBand] = pixelValues; } for (int i = pBRowIndex; i < pBHeight; i++) { for (int k = pBColIndex; k < pBWidth; k++) { double[] expArr = new double[slopes.Length]; double sumExp = 0; int catCnts = 0; bool noDataCheck = true; foreach (double[] IntSlpArr in slopes) { double sumVls = IntSlpArr[0]; for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++) { object coefnVlObj = outPb.GetVal(coefnBand, k, i); //Console.WriteLine("Slope X value = " + pixelValue.ToString()); if (coefnVlObj == null) { noDataCheck = false; break; } double slp = IntSlpArr[coefnBand + 1]; //Console.WriteLine("x = " + pixelValue.ToString() + " slope = " + slp.ToString()); sumVls += System.Convert.ToDouble(coefnVlObj) * slp; } if (noDataCheck) { double expSum = Math.Exp(sumVls); expArr[catCnts] = expSum; sumExp += expSum; catCnts += 1; //Console.WriteLine("sumVls = " + sumVls.ToString()); } else { break; } } if (noDataCheck) { sumExp += 1; double sumProb = 0; catCnts = 1; foreach (double expVl in expArr) { rstPixelType pTy = ipPixelBlock.get_PixelType(catCnts); double prob = expVl / sumExp; //Console.WriteLine("Probability = " + prob.ToString()); sumProb += prob; object newVl = rasterUtil.getSafeValue(prob, pTy); cArr[catCnts].SetValue(newVl, k, i); //Console.WriteLine("Probability = " + cArr[catCnts].GetValue(k,i).ToString()); catCnts += 1; } rstPixelType pTy2 = ipPixelBlock.get_PixelType(0); double lProb = 1 - sumProb; object newVl2 = rasterUtil.getSafeValue(lProb, pTy2); cArr[0].SetValue(newVl2, k, i);//base category } else { for (int b = 0; b < ipPixelBlock.Planes; b++) { cArr[b].SetValue(0, k, i); } } } } for (int i = 0; i < ipPixelBlock.Planes; i++) { ipPixelBlock.set_PixelData(i, cArr[i]); } } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of Regression 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 { // Call Read method of the Raster Function Helper object. myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); //noDataValueArr = (System.Array)((IRasterProps)inrsBandsCoef).NoDataValue;//inrsBandsCoef 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[] 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; //} for (int nBand = 0; nBand < pPixelBlock.Planes; nBand++) { System.Array outArr = (System.Array)ipPixelBlock.get_PixelData(nBand); rstPixelType pty = ipPixelBlock.get_PixelType(nBand); for (int i = pBRowIndex; i < pBHeight; i++) { for (int k = pBColIndex; k < pBWidth; k++) { float[] IntSlpArr = slopes[nBand]; double sumVls = IntSlpArr[0]; bool checkNoData = true; for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++) { double slp = System.Convert.ToDouble(IntSlpArr[coefnBand + 1]); if (slp == 0) { continue; } //double noDataValue = System.Convert.ToDouble(noDataValueArr.GetValue(coefnBand)); object objPvl = outPb.GetVal(coefnBand, k, i); if (objPvl == null) { checkNoData = false; break; } else { double pixelValue = Convert.ToDouble(objPvl); sumVls += pixelValue * slp; } } if (checkNoData) { object nVl = rasterUtil.getSafeValue(sumVls, pty); outArr.SetValue(nVl, k, i); } } } ipPixelBlock.set_PixelData(nBand, outArr); } } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of Tobit Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }
private void ShowRasterInfo(IRasterLayer rLayer) { rtxbRasterInfo.Text = "Boand Number is " + rLayer.BandCount.ToString();// "\r\n" +"Resolution is " + rLayer.DisplayResolutionFactor.ToString() + "\r\n" + "Row number is " + rLayer.RowCount.ToString(); IRaster raster = rLayer.Raster; //Raster Data IRasterBandCollection rBandCon = raster as IRasterBandCollection; int numBands = rBandCon.Count; for (int i = 0; i < numBands; i++) { // IRasterBand rBand = rBandCon.Item(i); IRasterProps rasterData = rBand as IRasterProps; //Raster Height int height = rasterData.Height; //Raster Width int width = rasterData.Width; //Raster Cell Size IPnt cell = rasterData.MeanCellSize(); rstPixelType rType = rasterData.PixelType; MessageBox.Show(rType.ToString()); if (rType == rstPixelType.PT_LONG) { //Error code //Read Raster //Wrong QI //IRaster pRaster = rBand as IRaster; ITable rasterTable = rBand.AttributeTable; IQueryFilter qFilter = new QueryFilter(); for (int j = 0; j < rasterTable.Fields.FieldCount; j++) { IField aField = rasterTable.Fields.get_Field(j); qFilter.AddField(aField.ToString()); } ICursor cursor = rasterTable.Search(qFilter, true); IRow aRow = cursor.NextRow(); if (aRow != null) { //aRow.get_Value(0); MessageBox.Show(cell.X.ToString() + "x" + cell.Y + ":" + aRow.get_Value(0).ToString()); //Read Raster // rBand. } } // Create pixelblock IRawPixels rawPixels = rBand as IRawPixels; //cannot be used as follows //IRawPixels rawPixels = raster as IRawPixels; IPnt pixelBlockOrigin = new DblPnt(); pixelBlockOrigin.SetCoords(0, 0); IPnt pixelBlockSize = new DblPnt(); pixelBlockSize.SetCoords(rasterData.Width, rasterData.Height); IPixelBlock3 pixelBlock3 = (IPixelBlock3)rawPixels.CreatePixelBlock(pixelBlockSize); //Read rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3); //GetVal( int brand, int X, int Y); brand=0 int x = 3600; int y = 800; object cellD = pixelBlock3.GetVal(0, x, y); //MessageBox.Show(cellD.ToString()); rtxbRasterInfo.Text = rtxbRasterInfo.Text + "\r\n" + rBand.Bandname.ToString() + ":" + width.ToString() + "x" + height.ToString() + ":(" + x.ToString() + "," + y.ToString() + ")=" + cellD.ToString(); } }
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 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 } }
/// <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; for (int nBand = 0; nBand < pPixelBlock.Planes; nBand++) { System.Array outArr = (System.Array)ipPixelBlock.get_PixelData(nBand); rstPixelType pty = ipPixelBlock.get_PixelType(nBand); for (int i = pBRowIndex; i < pBHeight; i++) { for (int k = pBColIndex; k < pBWidth; k++) { bool checkNoData = true; int ov = 1; for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++) { double min = mins[coefnBand]; double max = maxs[coefnBand]; object objPvl = outPb.GetVal(coefnBand, k, i); if (objPvl == null) { checkNoData = false; ov = 0; break; } else { double pixelValue = Convert.ToDouble(objPvl); if (pixelValue < min || pixelValue > max) { ov = 0; break; } } } if (checkNoData) { object nVl = rasterUtil.getSafeValue(ov, pty); outArr.SetValue(nVl, k, i); } } } ipPixelBlock.set_PixelData(nBand, outArr); } } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of Domain Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }