#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>

", 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;

}

运行步骤:

  1. 打开命令提示符
  2. 进入代码所在的文件夹,使用cd命令,例如:cd D:\kjfx\Project9\Project9
  3. 编译代码,使用命令:gcc dem.cpp -ltiff -o dem
  4. 运行代码,使用命令:./dem D:\kjfx\dem.tif
  5. 程序运行后,会输出总填方和总挖方的体积。

代码说明:

  • 该代码使用了 libtiff 库来读取和处理 TIFF 文件。
  • 代码首先打开 TIFF 文件,并获取图像宽度、高度、像素深度和样本数等信息。
  • 然后,代码使用 TIFFReadEncodedStrip 函数读取每个扫描线的数据。
  • 最后,代码根据高程数据计算总填方和总挖方体积。

注意:

  • 代码中的 ROWS、COLS 和 PIXEL_SIZE 变量需要根据实际的 DEM 数据进行调整。
  • 确保 libtiff 库已安装并配置。
  • 代码需要在支持 libtiff 库的环境中运行。
C++ TIFF 图像处理:计算填挖方体积

原文地址: https://www.cveoy.top/t/topic/fVen 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录