使用C语言和LibTIFF计算TIFF图像的填挖方量
#include <stdio.h> #include <stdlib.h> #include <tiffio.h>
#define ROWS 327 #define COLS 486 #define PIXEL_SIZE 5
int main(int argc, char* argv[]) { TIFF *tif; float *data; uint32 width, height; uint16 bits_per_sample, samples_per_pixel; tsize_t strip_size; unsigned long long total_fill_volume = 0, total_cut_volume = 0; int i, j;
if (argc < 2) {
fprintf(stderr, 'Usage: %s <input file>\n', argv[0]);
exit(1);
}
tif = TIFFOpen(argv[1], 'r');
if (!tif) {
fprintf(stderr, 'Error opening TIFF file\n');
exit(1);
}
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
if (width != COLS || height != ROWS || bits_per_sample != 32 || samples_per_pixel != 1) {
fprintf(stderr, 'Invalid TIFF format\n');
exit(1);
}
strip_size = TIFFStripSize(tif);
data = (float*) _TIFFmalloc(strip_size);
if (!data) {
fprintf(stderr, 'Error allocating memory\n');
exit(1);
}
for (i = 0; i < height; i++) {
TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, i, 0), data, strip_size);
for (j = 0; j < width; j++) {
float elevation = data[j];
if (elevation > 0) {
total_fill_volume += (PIXEL_SIZE * PIXEL_SIZE * elevation);
} else {
total_cut_volume += (PIXEL_SIZE * PIXEL_SIZE * (-elevation));
}
}
}
printf('Total fill volume: %llu cubic meters\n', total_fill_volume);
printf('Total cut volume: %llu cubic meters\n', total_cut_volume);
_TIFFfree(data);
TIFFClose(tif);
return 0;
}
如何在 Visual Studio 中运行代码并获取填挖方结果:
- 创建项目: 在 Visual Studio 中创建一个新的控制台应用程序项目。
- 添加代码: 将上述代码复制到项目中的
main.c文件中。 - 复制 TIFF 文件: 将需要处理的 TIFF 文件复制到项目文件夹中。
- 配置包含目录:
- 打开“解决方案资源管理器”。
- 右键单击项目名称,选择“属性”。
- 在“属性”窗口中,选择“C/C++” -> “常规”。
- 在“附加包含目录”中添加
tiffio.h所在的文件夹路径。
- 配置库目录和依赖项:
- 在“属性”窗口中,选择“链接器” -> “常规”。
- 在“附加库目录”中添加
libtiff库所在的文件夹路径。 - 在“链接器” -> “输入” -> “附加依赖项”中添加
libtiff.lib。
- 运行程序: 按下 F5 键运行程序。填方和挖方的结果将显示在控制台中。
原文地址: https://www.cveoy.top/t/topic/fVhX 著作权归作者所有。请勿转载和采集!