C++处理tiff图片--libtiff库(附读写图片代码)
2021/7/28 20:07:11
本文主要是介绍C++处理tiff图片--libtiff库(附读写图片代码),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
环境
VS2017
libtiff 4.0.9
配置
上官网下载合适的版本后
打开控制台 切换当前目录到tiff-x.x.x,找到vs2017的vcvars64.bat脚本
控制台中执行 vcvars64.bat
然后 执行 nmake /f makefile.vc
本人操作如下
d:\tiff-4.0.9> D:\VS2017\VC\Auxiliary\Build\vcvars64.bat d:\tiff-4.0.9> nmake /f makefile.vc
编译好的tifflib.dll tifflib.lib 以及头文件都在libtiff 目录中
链接器-输入加入:libtiff.lib
读写图片代码
各函数参数和用法强烈建议google!!
常见函数文档
#include <iostream> #include <vector> #include "libtiff/tiff.h" #include "libtiff/tiffio.h" using namespace std; void open_Fun2(const char* file, uint16 **buffer, int *size) { TIFF *tif = TIFFOpen(file, "r"); //使用TIFFOpen函数以只读形式打开图像。 if (tif == nullptr) { cout << "读入图像路径错误,请重新确认"; return; } int width, height; //-------------获取单帧图像的长高 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height); //TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &channel); //------------ 获取图片帧数 int nTotalFrame = TIFFNumberOfDirectories(tif); printf("width: %d\n", width); printf("Slice: %d\n", nTotalFrame); printf("height: %d\n", height); //---------------获取每一帧的像素点数目 //int stripSize = TIFFStripSize(tif); int stripSize = width * height; printf("stripSize: %d\n", stripSize); *buffer = new uint16[nTotalFrame*stripSize]; int N_size = 0; for (int s = 0; s < nTotalFrame; s++) { for (int row = 0; row < height; row++) { TIFFReadScanline(tif, (&(*buffer)[N_size] + row * int(width)), row); //---按行读取 } N_size += width * height; TIFFReadDirectory(tif); } TIFFClose(tif); size[0] = width; size[1] = height; size[2] = nTotalFrame; } void saveTiff(const char *path, uint16 *buffer, int *size) { int width = size[0]; int height = size[1]; int slice = size[2]; TIFF* out = TIFFOpen(path, "a"); if (out) { int N_size = 0; size_t nCur = 0; //UChar den = (sizeof(T) == 1) ? 1 : 4; do { TIFFSetField(out, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE); TIFFSetField(out, TIFFTAG_PAGENUMBER, slice); TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32)width); TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32)height); //TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, 2); /*TIFFSetField(out, TIFFTAG_YRESOLUTION, 196.0f); TIFFSetField(out, TIFFTAG_XRESOLUTION, 204.0f);*/ TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); // TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 16); //根据图像位深填不同的值 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1); TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_NONE); TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, height); for (int m = 0; m < height; m++) { TIFFWriteScanline(out, &buffer[N_size + width * m], m, 0); } //TIFFWriteEncodedStrip(out, 0, &buffer[N_size], width * height); //另一种写入方法 ++nCur; N_size = N_size + width * height; } while (TIFFWriteDirectory(out) && nCur < slice); TIFFClose(out); cout << "save over"; } } int main() { //const char* fileName = "C:\\Users\\lxy\\Desktop\\model-and-data\\test\\720-noprepro-02_neutrophil_0.25power_zoom4.0_30Hz_lowSNR.tif"; const char* fileName2 = "C:\\Users\\lxy\\Desktop\\model-and-data\\test\\tiffwrite_try.tif"; const int Nsample = 512 * 512 * 100; uint16 *Three_tiff_buffer=new uint16 [Nsample]; for (int i = 0; i < Nsample; i++) { Three_tiff_buffer[i] = rand() % 65535; } int size_tiff[3] = { 512,512,100 }; saveTiff(fileName2, Three_tiff_buffer, size_tiff); /*open_Fun2(fileName, &Three_tiff_buffer, size_tiff);*/ return 0; }
环境配置参考链接:https://blog.csdn.net/nwpulei/article/details/7473669
参考代码链接:https://blog.csdn.net/u013921430/article/details/79758906
都是写的非常认真的参考资料
这篇关于C++处理tiff图片--libtiff库(附读写图片代码)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-15在使用平台私钥进行解密时提示 "私钥解密失败" 错误信息是什么原因?-icode9专业技术文章分享
- 2024-11-15Layui框架有哪些方式引入?-icode9专业技术文章分享
- 2024-11-15Layui框架中有哪些减少对全局环境的污染方法?-icode9专业技术文章分享
- 2024-11-15laydate怎么关闭自动的日期格式校验功能?-icode9专业技术文章分享
- 2024-11-15laydate怎么取消初始日期校验?-icode9专业技术文章分享
- 2024-11-15SendGrid 的邮件发送时,怎么设置回复邮箱?-icode9专业技术文章分享
- 2024-11-15使用 SendGrid API 发送邮件后获取到唯一的请求 ID?-icode9专业技术文章分享
- 2024-11-15mailgun 发送邮件 tags标签最多有多少个?-icode9专业技术文章分享
- 2024-11-15mailgun 发送邮件 怎么批量发送给多个人?-icode9专业技术文章分享
- 2024-11-15如何搭建web开发环境并实现 web项目在浏览器中访问?-icode9专业技术文章分享