c++ - 编译错误 cv::gpu

标签 c++ opencv

我在 Ubuntu 12.04 上使用带有 CUDA 的 OpenCV 主分支 (3.0.0.dev),并尝试使用 gpu 代码编译以下 opencv:

#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/gpu/gpu.hpp"

using namespace cv;

int main (int argc, char* argv[])
{
    try
    {
        cv::Mat src_host = cv::imread("file.png", CV_LOAD_IMAGE_GRAYSCALE);
        cv::gpu::GpuMat dst, src;
        src.upload(src_host);

        cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);

        cv::Mat result_host = dst;
        cv::imshow("Result", result_host);
        cv::waitKey();
    }
    catch(const cv::Exception& ex)
    {
        std::cout << "Error: " << ex.what() << std::endl;
    }
    return 0;
}

编译命令为:

g++ testgpu.cpp -o test `pkg-config --cflags --libs opencv` -lopencv_gpu

它有以下编译错误:

testgpu.cpp: In function ‘int main(int, char**)’:
testgpu.cpp:13:51: error: ‘CV_LOAD_IMAGE_GRAYSCALE’ was not declared in this scope
         cv::Mat src_host = cv::imread("file.png", CV_LOAD_IMAGE_GRAYSCALE);
                                                   ^
testgpu.cpp:17:52: error: ‘CV_THRESH_BINARY’ was not declared in this scope
         cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);
                                                    ^
testgpu.cpp:19:31: error: conversion from ‘cv::gpu::GpuMat’ to non-scalar type ‘cv::Mat’ requested
         cv::Mat result_host = dst;
                           ^

是OpenCV的安装有问题,还是Opencv 3.0.0的API变了?

最佳答案

gpu 模块在 OpenCV 3.0 中被重新设计。它被拆分成几个模块,重命名为 cudagpu:: 命名空间重命名为 cuda::。 OpenCV 3.0 的正确代码:

#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/cudaarithm.hpp"

using namespace cv;

int main (int argc, char* argv[])
{
    try
    {
        cv::Mat src_host = cv::imread("file.png", cv::IMREAD_GRAYSCALE);
        cv::cuda::GpuMat dst, src;
        src.upload(src_host);
        cv::cuda::threshold(src, dst, 128.0, 255.0, cv::THRESH_BINARY);
        cv::Mat result_host(dst);
        cv::imshow("Result", result_host);
        cv::waitKey();
    }
    catch(const cv::Exception& ex)
    {
        std::cout << "Error: " << ex.what() << std::endl;
    }
    return 0;
}

关于c++ - 编译错误 cv::gpu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19368244/

相关文章:

python - 3D 平面拟合方法(C++ 与 Python)

opencv - 编译 PCL 以与 OpenCV 一起使用

c++ - 针对 `.rodata' 重定位 R_X86_64_32S ... 在 64 位平台上编译时

c++ - 将 C 字符串转换为 unsigned char 指针

c++ - libstdc++.so.6 重定位错误

python - OpenCV的dilate与scipy、matlab不同

C++ 错误 C2040 : 'ip' : 'const char *' differs in levels of indirection from 'std::string' ?

c++ - 使用 Xcode 在 Mac 上用 C++ 生成随机数时遇到问题

python - 检测角度并在 Python 中旋转图像

python - 如何使用 opencv 检测视频摄像机中的黑色?