寻求可提取特定像素占比并转为属性的转换器:TIFF转CSV需求
解决方案
1. FME(与现有工具兼容)
既然你已经在使用RasterStatisticsCalculator,FME的RasterPixelCounter转换器完全匹配你的需求:
- 直接读取TIFF文件,原生支持多波段处理
- 可指定目标像素值(黑像素对应0值)
- 开启
Per Band Statistics后,会输出每个波段的目标像素占比(自动计算为:目标像素数/总像素数) - 能通过
AttributeMerger和你现有RasterStatisticsCalculator生成的属性合并,最后用CSVWriter输出到CSV文件
2. GDAL命令行(轻量无GUI)
如果偏好命令行工具,GDAL结合简单脚本就能实现:
- 用
gdalinfo -stats input.tif获取每个波段的总像素数和0值像素数 - 用awk或Python脚本计算占比,再和你已有的统计属性整合为CSV
示例命令(提取0值像素数):
gdalinfo -stats -hist input.tif | grep "0," | awk '{print $2}'
占比计算公式:(0值像素数 / 总像素数) * 100,最后将所有属性整理写入CSV即可。
3. Python脚本(自定义灵活)
用rasterio库可以实现完全自定义的统计逻辑,适合批量处理场景:
import rasterio import csv # 读取TIFF文件 with rasterio.open('input.tif') as src: # 初始化结果字典,先填入已有统计属性 result = { 'filename': src.name, # 替换为你用RasterStatisticsCalculator生成的属性,例如mean_b1、max_b2等 'mean_b1': 123.45, 'max_b2': 255 } # 遍历每个波段计算黑像素占比 for band_idx in range(1, src.count + 1): band_data = src.read(band_idx) total_pixels = band_data.size black_pixel_count = (band_data == 0).sum() black_ratio = (black_pixel_count / total_pixels) * 100 result[f'black_ratio_b{band_idx}'] = round(black_ratio, 4) # 写入CSV with open('output.csv', 'w', newline='') as csv_file: writer = csv.DictWriter(csv_file, fieldnames=result.keys()) writer.writeheader() writer.writerow(result)
内容的提问来源于stack exchange,提问作者Jack_ Sk
相关产品推荐
相关产品推荐

