c++ - 在头文件中使用opencv类型实现未定义的标识符
问题描述
以下代码可以毫无问题地构建和运行(只需使用或不使用 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);
那就是使用cv::
前缀,因为using namespace
在hpp中无效。
推荐阅读
- python - 在计算中使用列值和行值生成给定大小的方形 Numpy 数组
- r - ggplot 扩展函数在散点图中绘制叠加均值
- python - Keras 如何计算每个 epoch 的模型精度?
- django - Nginx 将子目录添加到 proxy_pass 响应?
- git - Rebase git tree 保持恢复引用
- powershell - 关于我为什么收到此错误的任何想法?任何帮助表示赞赏。谢谢
- android - Android 应用内结算 - 购买未到达我的服务器
- next.js - Cypress/Next.js 使用 --watch 选项进行 E2E 测试效果很好,但使用 --headless 失败
- ansible - 使用ansible依次运行4个命令1的最干净的方法是什么
- mapping - 是否有任何工具可以根据先前制作的映射合并 skos?