在头文件中使用opencv类型来实现未定义的标识符

using opencv type in header file achieve to undefined identifier

本文关键字:实现 未定义 标识符 类型 opencv 文件      更新时间:2023-10-16

下面的代码构建和运行都没有问题(只需调整带有或不带有cuda的图像大小(

//#include <opencv2/core.hpp>
//#include <opencv2/imgcodecs.hpp>
//#include <opencv2/highgui.hpp>
#include "opencv2/opencv.hpp"
#include <opencv2/core/utils/logger.hpp>
#include <iostream>
//#include "openCVTest.hpp"
using namespace cv;
using namespace std;
void mainTransform(InputArray src, OutputArray dest, double m) {
resize(src, dest, Size(0, 0), m, m, INTER_CUBIC);
}
int main(int argc, char** argv) {
utils::logging::setLogLevel(utils::logging::LOG_LEVEL_WARNING);
const string keys =
"{help h usage ?| |[image] [-cd=cudaIndex]}"
"{@image        | |image to display * 1.5}"
"{cdi           |0|cuda device index, -1 for cpu}";
CommandLineParser parser(argc, argv, keys);
if (parser.has("help")) {
parser.printMessage();
return 0;
}
int cudaDeviceIndex = parser.get<int>("cdi");
string imageName = parser.get<string>("@image");
if (!parser.check()) {
parser.printErrors();
return 0;
}
const char* WIN_NAME = "Display window";
cuda::DeviceInfo cudaDeviceInfo;
int cedc = cuda::getCudaEnabledDeviceCount();
if (cedc == 0 || cudaDeviceIndex >= cedc || cudaDeviceIndex < 0) {
cout << "no cuda device " << cudaDeviceIndex << " max is " << cedc - 1 << endl;
cudaDeviceIndex = -1;
}
else {
cuda::setDevice(cudaDeviceIndex);
cudaDeviceInfo = cuda::DeviceInfo(cudaDeviceIndex);
cout << cedc << " cuda device(s), using index 0: " << cudaDeviceInfo.name() << endl;
}   
if (imageName == "") {
return 0;
}
namedWindow(WIN_NAME, WINDOW_AUTOSIZE);
Mat image, dest;
image = imread(imageName, IMREAD_COLOR); 
if (image.empty()) {
cout << "Could not open or find the image" << std::endl;
return -1;
}
double m = 1.5;
if (cudaDeviceIndex >= 0) {
cout << "from gpu" << std::endl;
cuda::GpuMat gpuImage, gpuDest;
gpuImage.upload(image);
cuda::resize(gpuImage, gpuDest, Size(0, 0), m, m, INTER_CUBIC);
gpuDest.download(dest);
}
else {
cout << "from cpu" << std::endl;
mainTransform(image, dest, m);
resize(image, dest, Size(0, 0), m, m, INTER_CUBIC);
}
imshow(WIN_NAME, dest); 
waitKey(0);
return 0;       
}

现在,如果我改为:

//#include <opencv2/core.hpp>
//#include <opencv2/imgcodecs.hpp>
//#include <opencv2/highgui.hpp>
#include "opencv2/opencv.hpp"
#include <opencv2/core/utils/logger.hpp>
#include <iostream>
#include "openCVTest.hpp"

带有openCVTest.hpp:

#ifndef _openCVTest_h
#define _openCVTest_h
#include "opencv2/opencv.hpp"
void mainTransform(InputArray src, OutputArray dest, double m);
#endif

我得到了一个C2065错误:在openCVTest.hpp.的第6行中未定义标识符

我禁用了预编译头。

我的目标是将mainTransform的定义转移到main之下。

解决方案是,在hpp文件中:

void mainTransform(cv::InputArray src, cv::OutputArray dest, double m);

即使用CCD_ 3前缀,因为CCD_。