Skip to main content

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!

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.


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 (Am0] < 255 && Ao1] < 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


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"

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.


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


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!


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 (Am0] < 255 && Ao1] < 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!


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


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