可以使用LibTIFF与C ++获得图像的浮点值吗? [英] Can LibTIFF be used with C++ to get the float values of images?

查看:549
本文介绍了可以使用LibTIFF与C ++获得图像的浮点值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了


I read here that LibTIFF can display floating point TIFFs. However, I would like to load an image, then get the float values as an array.

Is this possible to do using LibTIFF?

EDIT: I am using RHEL 6.

EDIT: https://www.dropbox.com/s/f15fkkj5dg5ojzt/map.tif?dl=0

Sorry about the wrong link!

Link to an example TIFF.

解决方案

If you want to do it with pure libTIFF, your code might look something like this - note that I have not done much error checking so as not to confuse the reader of the code - but you should check that the image is of type float, and you should check the results of memory allocations and you probably shouldn't use malloc() like I do but rather the new C++ methods of memory allocation - but the concept is hopefully clear and the code generates the same answers as my CImg version...

#include "tiffio.h"
#include <cstdio>
#include <iostream>

using namespace std;

int main()
{

  TIFF* tiff = TIFFOpen("image.tif","r");
  if (!tiff) {
    cerr << "Failed to open image" << endl;
    exit(1);
  }

  uint32 width, height;
  tsize_t scanlength;

  // Read dimensions of image
  if (TIFFGetField(tiff,TIFFTAG_IMAGEWIDTH,&width) != 1) {
    cerr << "Failed to read width" << endl;
    exit(1);
  }
  if (TIFFGetField(tiff,TIFFTAG_IMAGELENGTH, &height) != 1) {
    cerr << "Failed to read height" << endl;
    exit(1);
  }

  scanlength = TIFFScanlineSize(tiff);

  // Make space for image in memory
  float** image= (float**)malloc(sizeof (float*)*height);

  cout << "Dimensions: " << width << "x" << height << endl;
  cout << "Line buffer length (bytes): " << scanlength << endl;

  // Read image data allocating space for each line as we get it
  for (uint32 y = 0; y < height; y++) {
    image[y]=(float*)malloc(scanlength);
    TIFFReadScanline(tiff,image[y],y);
    cout << "Line(" << y << "): " << image[y][0] << "," << image[y][1] << "," << image[y][2] << endl;
  }
  TIFFClose(tiff);

}

Sample Output

Dimensions: 512x256
Line buffer length (bytes): 6144
Line(0): 3.91318e-06,0.232721,128
Line(1): 0.24209,1.06866,128
Line(2): 0.185419,2.45852,128
Line(3): 0.141297,3.06488,128
Line(4): 0.346642,4.35358,128
...
...

By the way...

I converted your image to a regular JPEG using ImageMagick in the Terminal at the command line as follows:

convert map.tif[0] -auto-level result.jpg

这篇关于可以使用LibTIFF与C ++获得图像的浮点值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆