访问TIFF图像

Access TIFF image

本文关键字:图像 TIFF 访问      更新时间:2023-10-16

我正在尝试读取TIFF图像以执行处理。理想的情况是能够在OpenCV结构中导入此图像,但即使以不同的方式访问它也很棒。

如果我在图像上运行tiffinfo,我会得到

TIFF Directory at offset 0x2bb00 (178944)
  Subfile Type: (0 = 0x0)
  Image Width: 208 Image Length: 213
  Resolution: 1, 1
  Bits/Sample: 32
  Sample Format: IEEE floating point
  Compression Scheme: None
  Photometric Interpretation: min-is-black
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 1
  Rows/Strip: 1
  Planar Configuration: single image plane 

我想访问单像素值。该图像为灰度级,其中包含的数据范围从0.0到10372.471680。

我对LibTIFF、Magick++进行了一些尝试,但无法访问单个像素值(我试图在像素上进行循环并在屏幕上打印这些值)。

这是我尝试使用的一段代码,我从一个在线示例中得到:

#include "tiffio.h"
#include "stdio.h"
int main()
{
    TIFF* tif = TIFFOpen("test.tif", "r");
    if (tif) {
        uint32 imagelength;
        tsize_t scanline;
        tdata_t buf;
        uint32 row;
        uint32 col;
        TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
        scanline = TIFFScanlineSize(tif);
        buf = _TIFFmalloc(scanline);
        for (row = 0; row < imagelength; row++)
        {
            int n = TIFFReadScanline(tif, buf, row, 0);
        if(n==-1){
            printf("Error");
            return 0;
        }
            for (col = 0; col < scanline; col++)
                printf("%fn", buf[col]);
            printf("n");
        }
        printf("ScanLineSize: %dn",scanline);
        _TIFFfree(buf);
        TIFFClose(tif);
    }
}

我用编译它

gcc测试。c-ltiff-o测试

当我运行它时,我得到

test.c: In function ‘main’:
test.c:24: warning: dereferencing ‘void *’ pointer
test.c:24: error: invalid use of void expression

有什么提示吗?谢谢你抽出时间。

查看函数_TIFFalloc()的文档。如果它的工作方式与标准malloc类似,它将返回一个void指针,如果第24行的buf[col]语句能够正常工作,则需要将该指针强制转换为特定类型。

您必须使用修复此问题

tdata_t *buf;
buf =(tdata_t*) _TIFFmalloc(scanline);