首页 文章

如何在libtiff中加载灰度tiff并获得像素强度数组?

提问于
浏览
6

我想要更多地了解图像,而且我遇到了很多麻烦 . 从使用matlab,我有使用imread('test.tif')的经验,并获得一个漂亮的行与列的矩阵,其中每个像素的强度为整数 . 因此,720 x 250图像将提供720 x 250矩阵,其中每个单元格包含像素的强度,范围为0-255(取决于数据类型) . 所以,0是黑色,255是白色 .

它非常简单,非常有意义 . 现在我试图使用libtiff,我真的很挣扎 . 我想做同样的事情 - 访问那些像素,我只是无法得到它 .

我有以下代码:

int main(int argc, char *argv[]){
  TIFF* tif = TIFFOpen( argv[1], "r");
    FILE *fp = fopen("test2.txt", "w+");

  if (tif) {
      int * buf;
      tstrip_t strip;
      uint32* bc;
      uint32 stripsize;
  TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &bc);
  stripsize = bc[0];
  buf   = _TIFFmalloc(stripsize);
  for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++ ) {
      if( bc[strip] > stripsize) {
          buf = _TIFFrealloc(buf, bc[strip]);
          stripsize = bc[strip];
      }
      TIFFReadRawStrip(tif, strip, buf, bc[strip]);
  }
  int i;
  for (i=0; i<stripsize; i++) {
      if ( i % 960 ==0 )
          fprintf(fp, "\n");
      fprintf(fp,"%d ",  buf[i]);
  }
  _TIFFfree(buf);
  TIFFClose(tif);
  }
  exit(0);
}

但是我得到了完全没有意义的结果 - 只是完全没有数字 . 没有像我在matlab中加载图像时看到的数字 .

我怎样才能简单地访问像素值,并查看它们?

非常感谢 .

3 回答

  • 2

    我想你应该读Using The TIFF Library article . 它包含足够的信息来开始使用libtiff .

    以下是一些读取每个样本的图像扫描线和打印值的代码 .

    main()
    {
        TIFF* tif = TIFFOpen("myfile.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++)
            {
                TIFFReadScanline(tif, buf, row);
                for (col = 0; col < scanline; col++)
                    printf("%d ", buf[col]);
    
                printf("\n");
            }
            _TIFFfree(buf);
            TIFFClose(tif);
        }
    }
    
  • 6

    关于这篇文章,我认为使用TIFFRGBAImage方法会更好,因为事实证明TIFF文件可能是不同格式之一:平铺,基于扫描线和面向条带 . 这是同一篇文章中的一个例子 .

    TIFF* tif = TIFFOpen(argv[1], "r");
    if (tif) {
        uint32 w, h;
        size_t npixels;
        uint32* raster;
    
        TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
        TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
        npixels = w * h;
        raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
        if (raster != NULL) {
            if (TIFFReadRGBAImage(tif, w, h, raster, 0)) {
                ...process raster data...
            }
            _TIFFfree(raster);
        }
        TIFFClose(tif);
    }
    
  • 2

    raster是一个uint32数组(最大值= 0xffffffff)但您正在尝试读取一个16位数组(最大值0xffff) . 你会遇到32位到16位的转换问题 . 读取扫描线方法是更好的方法 . 这样您就可以将void * buf转换为uint16 *并访问像素值 .

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <inttypes.h>
    #include "tiffio.h"
    
    
    using namespace std;
    
    
    void printArray(uint16 * array, uint16 width);
    int main()
    {
    
    
        TIFF* tif = TIFFOpen("16bit_grayscale_image.tif", "r");
         if (tif) {
        uint32 imagelength,height;
        tdata_t buf;
        uint32 row;
        uint32 config;
    
        TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
        TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
        buf = _TIFFmalloc(TIFFScanlineSize(tif));
    
    
            uint16 s, nsamples;
            uint16* data;
            TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamples);
            for (s = 0; s < nsamples; s++)
            {
                for (row = 0; row < imagelength; row++)
                    {
                    TIFFReadScanline(tif, buf, row, s);
                    data=(uint16*)buf;
                    printArray(data,imagelength);
                    }
                    // printArray(data,imagelength,height);
            }
    
    
        _TIFFfree(buf);
        TIFFClose(tif);
        }
        exit(0);
    }
    
    
    
    void printArray(uint16 * array, uint16 width)
    {
        uint32 i;
        for (i=0;i<width;i++)
        {
            printf("%u ", array[i]);
        }
            printf("\n");
    
    
    }
    

相关问题