#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

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

"); exit(1); }

strip_size = TIFFStripSize(tif);
data = (float*) _TIFFmalloc(strip_size);
if (!data) {
    fprintf(stderr, "Error allocating memory

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

", total_fill_volume); printf("Total cut volume: %llu cubic meters ", total_cut_volume);

_TIFFfree(data);
TIFFClose(tif);

return 0;

}

在 VS 中运行程序并获取填方挖方结果的步骤:

  1. 确保你已安装 Visual Studio 和 LibTIFF 库。
  2. 在 Visual Studio 中创建一个新的 C++ 项目。
  3. 将上述代码复制到你的项目中。
  4. 将 TIFF 文件放到项目文件夹中。
  5. 在项目属性中设置包含目录和库目录,指向 LibTIFF 库的安装目录。
  6. 编译并运行程序。

程序将读取 TIFF 文件,计算填方挖方体积并输出到控制台。

注意:

  • 你需要根据自己的实际情况调整 ROWS, COLSPIXEL_SIZE 的值。
  • 你可以使用 TIFFGetField 函数获取 TIFF 文件的元数据,例如宽度、高度等。
  • 如果你遇到错误,请检查你的代码,确保路径和设置正确。
  • 为了方便理解,代码中使用了简化的计算方法,实际应用中可能需要更精确的算法。
C++ 计算 TIFF 图像填方挖方体积

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

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