我正在尝试使用2017构建一个C++程序。
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
extern "C" {
#include "libtiff/libtiff/tiffio.h"
}
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
string imageName("410.tif"); // start with a default
// If there is an argument, read it in as the name of the image
if (argc > 1)
{
imageName = argv[1];
}
// Open the TIFF file using libtiff
TIFF* tif = TIFFOpen(imageName.c_str(), "r");
// Create a matrix to hold the tif image in
Mat image;
// check the tif is open
if (tif) {
do {
unsigned int width, height;
uint32* raster;
// get the size of the tiff
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
uint npixels = width * height; // get the total number of pixels
raster = (uint32*)_TIFFmalloc(npixels * sizeof(uint32)); // allocate temp memory (must use the tiff library malloc)
if (raster == NULL) // check the raster's memory was allocaed
{
TIFFClose(tif);
cerr << "Could not allocate memory for raster of TIFF image" << endl;
return -1;
}
// Check the tif read to the raster correctly
if (!TIFFReadRGBAImage(tif, width, height, raster, 0))
{
TIFFClose(tif);
cerr << "Could not read raster of TIFF image" << endl;
return -1;
}
image = Mat(width, height, CV_8UC4); // create a new matrix of w x h with 8 bits per channel and 4 channels (RGBA)
// itterate through all the pixels of the tif
for (uint x = 0; x < width; x++)
for (uint y = 0; y < height; y++)
{
uint32& TiffPixel = raster[y*width + x]; // read the current pixel of the TIF
Vec4b& pixel = image.at<Vec4b>(Point(y, x)); // read the current pixel of the matrix
pixel[0] = TIFFGetB(TiffPixel); // Set the pixel values as BGRA
pixel[1] = TIFFGetG(TiffPixel);
pixel[2] = TIFFGetR(TiffPixel);
pixel[3] = TIFFGetA(TiffPixel);
}
_TIFFfree(raster); // release temp memory
// Rotate the image 90 degrees couter clockwise
image = image.t();
flip(image, image, 0);
imshow("TIF Image", image); // show the image
waitKey(0); // wait for anykey before displaying next
} while (TIFFReadDirectory(tif)); // get the next tif
TIFFClose(tif); // close the tif file
}
return 0;
}正如您所看到的,我包括libtiff,但是当我试图编译时,我得到了以下错误:
LNK2019对函数中未解析的外部符号_TIFFmalloc的引用
错误被扩展到"_TIFFfree,TIFFClose,TIFFGetField,TIFFReadDirectory,TIFFReadRGBAImage,TIFFOpen“(我只发布了一个更容易)。
我试着在google上搜索这个问题,我认为这是由链接问题引起的。很多人告诉我,我应该在Visual项目的属性的"input“部分中添加" libtiff.lib”,但是我从官方源代码下载的库中没有libtiff.lib。
我使用的是4.0.4版本的库。
发布于 2018-08-02 15:08:46
TIFF是一个C库,而您的程序在C++中,C++和C有不同的链接ABI。
当您使用C++源代码中的C库时,通常应该这样做:
extern "C" {
#include "libtiff/libtiff/tiffio.h"
}..。向C++发出信号,表示该符号遵循C。
注意事项:许多被广泛使用的C库都在其标头中包含了C++条件预处理检查,以避免这个问题,如下所示:
#ifdef __cplusplus
extern "C" {
#endif
/* Actual header content */
extern int do_something(void);
extern int do_something_else(const char*);
/* ... */
#ifdef __cplusplus
}
#end对于libtiff来说,情况并非如此。
发布于 2018-08-03 10:07:16
使用以下库:http://gnuwin32.sourceforge.net/packages/tiff.htm,我可以正确地包含libtiff,但问题是这个库是64位,openCV是32位。因此,我得到以下错误:
LNK4272库计算机的“x86”类型与目标计算机的“x64”类型发生冲突-- ImageManipulation C:\ Program (x86) \ GnuWin32 \ lib \ libtiff.lib 1
Visual中的平台解决方案是在x64上设置的,因为如果我打开x86,openCV就不能工作。
https://stackoverflow.com/questions/51656596
复制相似问题