使用OpenCV和CUDA编译高斯滤波器代码时出错

时间:2017-04-04 21:41:46

标签: opencv cmake

当我使用Cmake编译此代码时...此错误已向我显示.... 我试图删除CUDA并重新安装它,但它是相同的

#include "iostream"
#include "stdio.h"
#include "stdlib.h"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core/cuda.hpp"
#include "opencv2/cudaimgproc.hpp"
#include "opencv2/cudafilters.hpp" // cv::cuda::Filter
#include "opencv2/cudaarithm.hpp" // cv::cuda::abs orcv::cuda::addWeighted
#include "timer.h"


using namespace std;
using namespace cv;

void processUsingOpenCvCpu(std::string nput_file, std::string output_file);
void processUsingOpenCvGpu(std::string input_file, std::string output_file);
void processUsingCuda(std::string input_file, std::string output_file);

int main(int argc, char **argv) {

const string input_file = argc >= 2 ? argv[1] : "evarest.jpg";
const string output_file_OpenCvCpu = argc >= 3 ? argv[2] : "output_OpenCvCpu.jpg";
const string output_file_OpenCvGpu = argc >= 4 ? argv[3] : "output_OpenCvGpu.jpg";
const string output_file_Cuda = argc >= 5 ? argv[2] : "output_Cuda.jpg";

for (int i=0; i<5; ++i) {
    processUsingOpenCvCpu(input_file, output_file_OpenCvCpu);
    processUsingOpenCvGpu(input_file, output_file_OpenCvGpu);
    //processUsingCuda(input_file, output_file_Cuda);
}
return 0;
}

void processUsingOpenCvGpu(std::string input_file, std::string output_file) {
//Read input image from the disk
Mat input = imread(input_file, CV_LOAD_IMAGE_COLOR);
Mat output;
if(input.empty())
{
    std::cout<<"Image Not Found: "<< input_file << std::endl;
    return;
}

GpuTimer timer;
timer.Start();

// copy the input image from CPU to GPU memory
cuda::GpuMat gpuInput = cuda::GpuMat(input);

// blur the input image to remove the noise
Ptr<cv::cuda::Filter> filter = cv::cuda::createGaussianFilter(gpuInput.type(), gpuInput.type(), Size(3,3), 0);
filter->apply(gpuInput, gpuInput);

// convert it to grayscale (CV_8UC3 -> CV_8UC1)
cv::cuda::GpuMat gpuInput_gray;
cv::cuda::cvtColor( gpuInput, gpuInput_gray, COLOR_RGB2GRAY );

// compute the gradients on both directions x and y
cv::cuda::GpuMat gpuGrad_x, gpuGrad_y;
cv::cuda::GpuMat abs_gpuGrad_x, abs_gpuGrad_y;
int scale = 1;
int ddepth = CV_16S; // use 16 bits unsigned to avoid overflow

// gradient x direction
filter = cv::cuda::createSobelFilter(gpuInput_gray.type(), ddepth, 1, 0, 3, scale, BORDER_DEFAULT);
filter->apply(gpuInput_gray, gpuGrad_x);
cv::cuda::abs(gpuGrad_x, gpuGrad_x);
gpuGrad_x.convertTo(abs_gpuGrad_x, CV_8UC1); // CV_16S -> CV_8U

// gradient y direction
filter = cv::cuda::createSobelFilter(gpuInput_gray.type(), ddepth, 0, 1, 3, scale, BORDER_DEFAULT);
filter->apply(gpuInput_gray, gpuGrad_y);
cv::cuda::abs(gpuGrad_y, gpuGrad_y);
gpuGrad_y.convertTo(abs_gpuGrad_y, CV_8UC1); // CV_16S -> CV_8U

// create the output by adding the absolute gradient images of each x and y direction
cv::cuda::GpuMat gpuOutput;
cv::cuda::addWeighted( abs_gpuGrad_x, 0.5, abs_gpuGrad_y, 0.5, 0, gpuOutput );

// copy the result gradient from GPU to CPU and release GPU memory
gpuOutput.download(output);
gpuOutput.release();
gpuInput.release();
gpuInput_gray.release();
gpuGrad_x.release();
gpuGrad_y.release();
abs_gpuGrad_x.release();
abs_gpuGrad_y.release();

timer.Stop();
printf("OpenCV GPU code ran in: %f msecs.\n", timer.Elapsed());

 //show image
imshow("Image", output);

// wait until user press a key
waitKey(0);

//imwrite(output_file, output);
}

这是错误

[ 50%] Linking CXX executable tut1
/usr/bin/ld: CMakeFiles/tut1.dir/1.cpp.o: undefined reference to symbol 'cudaEventSynchronize'
//usr/lib/x86_64-linux-gnu/libcudart.so.7.5: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/tut1.dir/build.make:122: recipe for target 'tut1' failed
make[2]: *** [tut1] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/tut1.dir/all' failed
make[1]: *** [CMakeFiles/tut1.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

当我删除CUDA时...他没有认识到所有CUDA的功能......但现在错误只出现在cudaEventSynchronize上..

1 个答案:

答案 0 :(得分:0)

重新签入您的代码:

cv::cuda::addWeighted(gpugrad_x, 0.5, gpugrad_y, 0.5, 0, gpuoutput);

FIX:

cv::cuda::addWeighted(gpugrad_x, 0.5, gpugrad_y, 0.5, 0, gpuoutput);

您忘记将double gamma放在addWeighted()函数中; 参见:https://docs.opencv.org/3.4/d8/d34/group__cudaarithm__elem.html#ga2cd14a684ea70c6ab2a63ee90ffe6201

我尝试了部分代码,并且可以正常工作。