libtiff库使用
此文章为了记录我在使用libtiff库中的一些问题而写,将不断补充。
libtiff库是读取和写入tiff文件最主要的一个开源库,但文档写的实在不敢恭维。相对资料也是异常稀少。
libtiff库的安装
libtiff库的最新的最新版本可以从http://www.libtiff.org/下载,即可以编译源码也可以采用预先编译好的二进制文件。
本人推荐使用预编译版本,自己编译容易缺少依赖库,同时也可能出现一些莫名其妙的问题。
tiff文件的读写函数:
//获取strip大小
tsize_t TIFFStripSize(TIFF *tif);
//读取strip数据,buf缓冲区可由TIFFStripSize计算,size取-1代表读取整个strip
tsize_t TIFFReadEncodedStrip(TIFF *tif, tstrip_t strip, tdata_t buf, tsize_t size);
将多色tiff文件分割
uint32 imageWidth, imageLength, TileWidth, TileLength, imageRowsPerStrip ;
uint16 imageCompression, imageSamplePerPixel ;
uint16 imagePlanarConfig, imagePhotoMetric, ResolutUnit, Orientation ;
uint16 bps ;
float X_Resolut, Y_Resolut ; TIFF *tif_r, *tif_w ;
unsigned char *buf;
tstrip_t strip ; tif_r = TIFFOpen("image_4plane.tif", "r");
if (!tif_r)
{
error_handler("Open Tiff File Error!");
return -;
}
/* 讀取 TIFF 標籤 */
TIFFGetField(tif_r, TIFFTAG_IMAGEWIDTH, &imageWidth);
TIFFGetField(tif_r, TIFFTAG_IMAGELENGTH, &imageLength); TIFFGetField(tif_r, TIFFTAG_BITSPERSAMPLE, &bps);
TIFFGetField(tif_r, TIFFTAG_COMPRESSION, &imageCompression);
TIFFGetField(tif_r, TIFFTAG_PHOTOMETRIC, &imagePhotoMetric); TIFFGetField(tif_r, TIFFTAG_SAMPLESPERPIXEL, &imageSamplePerPixel);
TIFFGetField(tif_r, TIFFTAG_ROWSPERSTRIP, &imageRowsPerStrip);
if (imageRowsPerStrip != )
{
error_handler("Rows Each Strip Is Not 1!");
return -;
} TIFFGetField(tif_r, TIFFTAG_XRESOLUTION, &X_Resolut);
TIFFGetField(tif_r, TIFFTAG_YRESOLUTION, &Y_Resolut);
TIFFGetField(tif_r, TIFFTAG_RESOLUTIONUNIT, &ResolutUnit); TIFFGetField(tif_r, TIFFTAG_PLANARCONFIG, &imagePlanarConfig);
TIFFGetField(tif_r, TIFFTAG_ORIENTATION, &Orientation); int stripsize = TIFFStripSize(tif_r);
buf = (unsigned char *) malloc(stripsize) ;
if (!buf)
{
error_handler("Allocate Buffer Failed!");
}
Mat ht_img(Size(imageWidth, imageLength),CV_8UC1,Scalar::all());
const int color[] = {,,,,,,};
unsigned char * pRow; for (strip = ; strip < TIFFNumberOfStrips(tif_r); strip++)
{
TIFFReadEncodedStrip(tif_r, strip, buf, (tsize_t) -);
pRow = ht_img.ptr(strip);
for (int i_pixel = ; i_pixel < imageWidth; i_pixel++)
{
pRow[i_pixel] = buf[i_pixel*imageSamplePerPixel + color[]];
}
}
imwrite("strip_out.tiff", ht_img); free(buf);
TIFFClose(tif_r);
printf("Done!\n");
参考文献:
1. libtiff库的使用
http://darkranger.no-ip.org/archives/v5/document/develop/libtiff_tutorial.htm
2. 关于如何判断一个tiff文件是tile或者是strip的说明
http://www.asmail.be/msg0054551721.html
libtiff库使用的更多相关文章
- C++学习笔记23:库
静态库(Archives) 后缀一般为"*.a" 使用两个目标文件创建单一静态库的编译与链接命令:ar cr libtest.a test1.o test2.o 链接器搜索静态库 ...
- 在Window下编译LibGeotiff(含Libtiff)
核心提示:1.GeoTiff简介 GeoTiff是包含地理信息的一种Tiff格式的文件. 1.GeoTiff简介 GeoTiff是包含地理信息的一种Tiff格式的文件.Libgeotiff就是一个操作 ...
- QT库在Windows上的编译
1.从http://www.qtcentre.org/下载QT源代码,注意是源代码.我下载到的是QT4.5.1版,文件名为qt-win-opensource-src-4.5.1.zip: 2.解压,注 ...
- VC++下编译Libgeotiff(含Libtiff)
转自原文Win10+VC++下编译Libgeotiff(含Libtiff)详细图文教程 GeoTiff是包含地理信息的一种Tiff格式的文件.Libgeotiff就是一个操作GeoTiff文件的库.同 ...
- libTIFF 图像读取与保存
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/YhL_Leo/article/details/49848391 1 头文件 libtif ...
- 利用开源程序(ImageMagick+tesseract-ocr)实现图像验证码识别
--------------------------------------------------低调的分割线-------------------------------------------- ...
- 安装Ubuntu双系统系列——64位Ubuntu安装H3C的INode客户端
学校使用的是Inode客户端认证上网的.如果是使用Ubuntu 32位版本,可以完美地安装并能够连接到网站.但是如果安装的是Ubuntu desktop 12.10 amd64版本,则发现之前的&qu ...
- Qt configure 参数不完全说明
只需要加个 -fast参数就ok了.其他参数视自己情况而定,比如你不需要qt3支持可以添加-no-qt3support,或者不需要webkit插件 -no-webkit配置参数选项: 前面是*号的表示 ...
- CCImage
#ifndef __CC_IMAGE_H__ #define __CC_IMAGE_H__ //派生于CCObject #include "cocoa/CCObject.h" // ...
随机推荐
- HDU1051:Wooden Sticks
Problem Description There is a pile of n wooden sticks. The length and weight of each stick are know ...
- nefu 1029 字符串
Description 给定一个字符串,现在要你从这个字符串中找到第一个只出现一次的字符 ,很简单吧! 快敲代码,动作! Input 多组输入,每行一个字符串(小写字母a~z)字符串长度不超过50 O ...
- [code]判断周期串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include<stdio.h> #include<string.h> ...
- linux下mysql root密码忘记修改方法
一.MySQL密码的恢复方法之一 如果忘记了MySQL的root密码,可以用以下方法重新设置:1.切换到root下su root 2. KILL掉系统里的MySQL进程: killall -TERM ...
- POJ 3597 种类数 数学+递推
http://poj.org/problem?id=3597 题目大意:把一个正多边形分成数个三角形或者四边形,问有多少种方案. 思路:http://www.cnblogs.com/Ritchie/p ...
- QML之使用Loader加载QML组件
呵呵,今晚是边看<裸婚时代>边敲代码,那电影看得...!钱真他妈不是个东西. 盼望Meego火起来. QML的Loader元素经常备用来动态加载QML组件.可以使用source属性或者so ...
- Let'sencrypt认证的网站Https配置
推荐使用这个脚本,具体说明里面都有 https://github.com/xdtianyu/scripts/tree/master/le-dns 它是通过调用dns服务商的api更新txt记录实现,无 ...
- MySql 加锁问题
1.设置非自动提交 set autocommit=0; 这时候 for update才会起作用 2.一般用法 set autocommit=0; for update(加锁) ; commit ...
- 让您的Xcode键字如飞
手指在键盘上飞速跳跃,终端上的代码也随着飞舞,是的这确实很酷.优秀的程序员总是这么一群人,他们不拘于现状,不固步自封,他们喜欢新奇的事,他们把自己发挥到极致. 指法攻略 放下您钟爱的鼠标吧,在前行之中 ...
- 8.5 sikuli 集成进eclipse 报错:can't be found on the disk
运行提示can't be found on the disk