尝试使用libtiff将一个16位的灰度tif图像转为OpenCV中对应的Mat格式并显示出来,参考代码如下:
1 void tiff_test()
2 {
3 libtiff::TIFF *image;
4 uint32_t width = 0, height = 0;
5 uint16_t ncn = 0;
6 uint16_t bitsPer = 0;
7 uint16_t *pData;
8 if((image = libtiff::TIFFOpen("/Users/Ko/brake/Tiff/brake1_height.tif", "r")) == NULL)
9 {
10 cout << "not a tiff" << endl;
11 exit(1);
12 } else {
13 cout << "tiff loaded" << endl;
14 }
15
16 libtiff::TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
17 libtiff::TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
18 libtiff::TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &ncn);
19 libtiff::TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bitsPer);
20 pData = (uint16_t *)libtiff::_TIFFmalloc(width * height * bitsPer);
21 if(pData != NULL)
22 {
23
24 }
25 cout << "tiff width:" << width << endl;
26 cout << "tiff length:" << height << endl;
27 cout << ncn << endl;
28 cout << "bitsPer:" << bitsPer << endl;
29 cout << "scanlinesize:" << libtiff::TIFFScanlineSize(image) << endl;
30 for(int i = 0; i < height; i++)
31 {
32 libtiff::TIFFReadScanline(image, pData + i * width, i);
33 }
34 Mat M(height, width, CV_16UC1, Scalar(0));
35 for(int i = 0; i < height; i++)
36 {
37 for(int j = 0; j < width; j++)
38 {
39 M.at<uint16_t>(i,j) = pData[j + i * width];
40 }
41 }
42 imshow("result", M);
43 libtiff::_TIFFfree(pData);
44 libtiff::TIFFClose(image);
45 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)