Creating a binary mask for pixels within each block of a raster.

154
5
Jump to solution
08-13-2022 09:57 AM
Saisai
by
New Contributor II

Hi all,

I am trying to create a binary mask for a single raster band in such a way that >75% of the pixels in each 3x3 block of the raster are regarded as valid pixels (1) and 25% or less are regarded as invalid (0).

Saisai_0-1660409481960.png

Much like the above throughout the raster. My thought was to create a numpy mask array and then multiply that by an array of the raster itself but I'm not sure how to create a masked array with such specific details.

Any help would be greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
BrennanSmith1
New Contributor III
  • Reclassify your 10m raster so every data cell has a value of 1.
  • Run Block Statistics on this reclassified raster, with a 3x3 rectangle snapped to your 30m raster and a sum statistic. This result tells you the number of 10m cells within each 30m cell.
  • Run Set Null on this block statistic result to remove all cells with a value less than 7.
  • Run Extract By Mask on your 30m raster using the result of the set null.

View solution in original post

5 Replies
BrennanSmith1
New Contributor III

Within each 3x3 block of cells, what determines the <25% of cells to mask? Would this be random, or based on their value, or always mask the two black cells in your example?

0 Kudos
Saisai
by
New Contributor II

Hi,

Let me give some background on why I need the mask, I have 2 images the first being a 30m land cover image (whose pixels have been filtered earlier based on some conditions), and the 2nd a 10m sentinel image (single band) of the same region.

I need to extract pixels of the land cover image that are covered by 75-100% of the 10m image pixels, for instance there are (9) 10m pixels in each 30m land cover pixel in other areas especially around the edges the coverage may not be 100%, so the condition is to extract each land cover pixel there must be no less than (7) 10m pixels covering them.

I had thought of creating a mask of the 10m pixel image in the format described above which would help in extracting or filtering the 30m land cover pixels.

Thanks.

0 Kudos
BrennanSmith1
New Contributor III
  • Reclassify your 10m raster so every data cell has a value of 1.
  • Run Block Statistics on this reclassified raster, with a 3x3 rectangle snapped to your 30m raster and a sum statistic. This result tells you the number of 10m cells within each 30m cell.
  • Run Set Null on this block statistic result to remove all cells with a value less than 7.
  • Run Extract By Mask on your 30m raster using the result of the set null.
Saisai
by
New Contributor II

Thank a lot Brennan, your solution was spot on.

Do you think it's possible to have a result on the number of cells that isn't a whole number?

Let say 7.5 or 8.5, that is remove all cells that are less than 7.5.

0 Kudos
BrennanSmith1
New Contributor III

Because this is a simplified approach with stacked, snapped rasters that works by counting the number of small cells inside the larger cells, it's limited to whole numbers.  Another approach would be to create a polygon feature class (probably Create Fishnet) that exactly matches each cell of your 30m raster, then you could run Zonal Stats on those polygons with your 10m raster (after reclassifying it to be either 1 or 0), where the zonal average would be in the range 0-1 and then you could use a threshold of 0.75 to select only the polygons you want and then extract your 30m raster to that selection.

0 Kudos