首页 > 解决方案 > 在头文件中使用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.

标签: c++opencv

解决方案


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

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

那就是使用cv::前缀,因为using namespace在hpp中无效。


推荐阅读