Boundary extraction method of simple raster image based on Python

  • 2021-07-10 20:10:54
  • OfStack

In GIS, the grid attribute has information about the grid itself, and the background (nodata value) is particularly important for identifying the boundary pixels of an image. Our purpose is to put the first pixel in each row and column that is not nodata and the first pixel in nodata for the last time.

For a grid, you can use the RasterToNumpyArray function in ArcPy to convert the grid into an numpy array, and then you can read out the first and last pixels in each column and column as you want.

The following is the core algorithm for extracting boundary pixels from some codes, which is actually a very simple idea (assuming that 0 is nodata value).


a=[[0 for col in range(Raster.width)]for row in range(Raster.height)]
 
for i in range(0,Raster.width):
... for j in range(0,Raster.height):
...  if(myRaster[j][i]!=0 and myRaster[j-1][i]==0):
...    a[j][i]=myRaster[j][i]
...  if(myRaster[j][i]==0 and myRaster[j-1][i]!=0):
...    a[j-1][i]=myRaster[j-1][i]
...    
 
 
for i in range(0,myRaster.height):
... for j in range(0,myRaster.width):
...  if(arr[i][j]!=0 and arr[i][j-1]==0):
...    a[i][j]=arr[i][j]
...  if(arr[i][j]==0 and arr[i][j-1]!=0):
...    a[i][j-1]=arr[i][j-1]

Related articles: