Skip to main content
Question

Raster: get as an attribute the number of pixels with a determined value.

  • September 22, 2023
  • 9 replies
  • 109 views

Hi everyone.

I am trying to obtain a csv file with some information about several raster files. One of the attributes I need to get is the number of pixels with a determined value (in my case I need, among others, two attributes for each raster: the number of pixels with value=0 and with value=255). Is there any transformer for that?

Thanks!

9 replies

redgeographics
Celebrity
Forum|alt.badge.img+49

You can use the RasterCellCoercer to turn the raster pixels into points or polygons, with the band values as attributes. Then you can use a StatisticsCalculator, grouping by band value, to count the number of pixels.


ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • September 22, 2023

One possible way is to convert the pixels with the value you are interested in to 1 and all other values to 0 in an RasterExpressionEvaluator and then use a RasterStatisticsCalculator to sum the values

if (A[0] < 255 && A[1] < 255 && A[2] < 255, 0, 1)

You would need to do this for each value you are interested in, but it's likely to be much quicker than coercing rasters. It does however destroy the rasters so you'd need to remerge with the originals if you wanted to keep them


david_r
Celebrity
  • September 22, 2023

The reply from @Hans van der Maarel​ is the best and easiest for small-ish rasters or where performance isn't too important. For huge rasters, however, it might take a lot of time and memory to coerce all the pixels to individual features. In that case, there's an alternative solution that avoids creating features for each pixel value:

  1. Use the RasterCellValueReplacer to replace all pixel values that you don't want to count to something like 0 (unless you want to count the 0 pixel values, of course)
  2. Use the RasterBandNodataSetter to set the value from the previous step to NoData
  3. Use the RasterStatisticsCalculator, the count of remaining pixels will be found in "value_count"

virtualcitymatt
Celebrity
Forum|alt.badge.img+35

Another option is to convert to a PointCloud (PointCloudCombiner) then use the PointCloudFilter to filter the point cloud based on certain values for a given band (e.g., 0 or 255). Then you can use a PointCloudPropertyExtractor on each filtered pointcloud to get the number of points, the number of points should equal the number of pixels.


hkingsbury
Celebrity
Forum|alt.badge.img+53
  • Celebrity
  • September 24, 2023
virtualcitymatt wrote:

Another option is to convert to a PointCloud (PointCloudCombiner) then use the PointCloudFilter to filter the point cloud based on certain values for a given band (e.g., 0 or 255). Then you can use a PointCloudPropertyExtractor on each filtered pointcloud to get the number of points, the number of points should equal the number of pixels.

In line with what @david_r​ said, @Hans van der Maarel​ is best for small (disk size) rasters. I've had really good success with the pointcloud approach on rasters that are 1000s of Ha with ~30cm pixels


  • Author
  • September 28, 2023
redgeographics wrote:

You can use the RasterCellCoercer to turn the raster pixels into points or polygons, with the band values as attributes. Then you can use a StatisticsCalculator, grouping by band value, to count the number of pixels.

Thanks a lot!


  • Author
  • September 28, 2023
ebygomm wrote:

One possible way is to convert the pixels with the value you are interested in to 1 and all other values to 0 in an RasterExpressionEvaluator and then use a RasterStatisticsCalculator to sum the values

if (A[0] < 255 && A[1] < 255 && A[2] < 255, 0, 1)

You would need to do this for each value you are interested in, but it's likely to be much quicker than coercing rasters. It does however destroy the rasters so you'd need to remerge with the originals if you wanted to keep them

Thanks!


  • Author
  • September 28, 2023
david_r wrote:

The reply from @Hans van der Maarel​ is the best and easiest for small-ish rasters or where performance isn't too important. For huge rasters, however, it might take a lot of time and memory to coerce all the pixels to individual features. In that case, there's an alternative solution that avoids creating features for each pixel value:

  1. Use the RasterCellValueReplacer to replace all pixel values that you don't want to count to something like 0 (unless you want to count the 0 pixel values, of course)
  2. Use the RasterBandNodataSetter to set the value from the previous step to NoData
  3. Use the RasterStatisticsCalculator, the count of remaining pixels will be found in "value_count"

Thanks David! After a few tries I got what I was looking for, using RasterCellValueReplace and RasterStatisticsCalculator, as you said. The workspace is larger than I thought...fme


  • Author
  • September 28, 2023
virtualcitymatt wrote:

Another option is to convert to a PointCloud (PointCloudCombiner) then use the PointCloudFilter to filter the point cloud based on certain values for a given band (e.g., 0 or 255). Then you can use a PointCloudPropertyExtractor on each filtered pointcloud to get the number of points, the number of points should equal the number of pixels.

Thanks a lot!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings