人工智能(语言识别&图像识别)

macOS中编译libtiff库并在Xcode中测试

2019-10-14  本文已影响0人  向水一生

源码编译

首先从官网上下载源码,地址:http://download.osgeo.org/libtiff/,这里我使用4.0.10的版本。

Screen Shot 2019-10-14 at 2.54.15 PM.png

解压后,配置好cmake,编译方法参考OpenCV的编译过程(https://www.cnblogs.com/you-siki/p/OpenCV4-MacOS.html),执行完sudo make install后,注意到文件的安装位置如下图所示,头文件被安装至/usr/local/inlcude,dylib初安装在/usr/local/lib

Screen Shot 2019-10-14 at 3.01.46 PM 1.png

在Xcode中测试libtiff库

image.png
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <random>
namespace libtiff {
    #include <tiffio.h>
    #include <tiff.h>
}

void tiff_test()
{
    libtiff::TIFF *image;
    uint32_t width = 0, height = 0;
    if((image = libtiff::TIFFOpen("/Users/dest/Documents/1010/brake/Tiff/brake1_height.tif",  "r")) == NULL)
    {
        cout << "not a tiff" << endl;
        exit(1);
    } else {
        cout << "tiff loaded" << endl;
    }
    
    libtiff::TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
    libtiff::TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
    cout << "tiff width:" << width << endl;
    cout << "tiff length:" << height << endl;
}

int main(int argc, const char * argv[]) {
    tiff_test();
    waitKey();
    return 0;
}

注意,上述代码中tiff相关的头文件放入了namespace libtiff中,这是因为OpenCV与libtiff的类型定义存在冲突,如果不定义libtiff的命名空间,会出现类似Typedef redefinition with different types ('int64_t(aka 'long long') vs 'long')这样的错误信息。

上述代码正常运行的输出应与下图类似:


image.png
上一篇下一篇

猜你喜欢

热点阅读