我尝试将自己的内核实现为中值过滤器,就像这个伪代码一样:
//main.cpp
#include "opencv2/opencv.hpp"
cv::Mat inputMat = cv::imread()
cudaMedianCaller (inputMat, kernelMat)
//medianFilter.h
#include "opencv2/opencv.hpp"
cudaMedianCaller (const cv::Mat& inputMat, cv::Mat& kernelMat);
//medianFilter.cu
cudaMedianCaller (const cv::Mat& inputMat, cv::Mat& kernelMat)
{
kernelMedianFilter<<< , >>> (uchar3* d_inputMat, uchar* d_kernelMat)
}
__global__ void kernelMedianFilter (uchar3* d_inputMat, uchar* d_kernelMat)
{
}
我收到编译错误:
C1083:无法打开包含文件:&#39; opencv2 / opencv.hpp&#39; :没有这样的文件或 目录
我知道.cu文件用nvcc编译,无法编译OpenCV头文件。
1)如何将OpenCV3.1.0库包含在.cu文件中?
答案 0 :(得分:3)
您不需要在.cu文件中包含OpenCV。您需要一个带有原始指针和基本数据类型作为参数的调用者API。
的main.cpp
#include "opencv2/opencv.hpp"
#include "medianFilter.h"
int main() {
cv::Mat inputMat = cv::imread();
.....
cudaMedianCaller (d_inputMat, d_kernelMat);
.....
return 0;
}
medianFilter.h
cudaMedianCaller (uchar3* d_inputMat, uchar* d_kernelMat);
medianFilter.cu
cudaMedianCaller (uchar3* d_inputMat, uchar* d_kernelMat)
{
kernelMedianFilter<<< , >>> (uchar3* d_inputMat, uchar* d_kernelMat)
}
__global__ void kernelMedianFilter (uchar3* d_inputMat, uchar* _kernelMat)
{
}
答案 1 :(得分:0)
尽管建议使用其他好的答案建议将C ++和CUDA分开,但还有另一种方法可以在.cu
文件中包含OpenCV容器:
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(test LANGUAGES CXX CUDA)
find_package(OpenCV 3.0 REQUIRED)
# compile the target
add_executable(test_app main.cpp medianFilter.cu)
target_link_libraries(test_app PRIVATE cudart ${OpenCV_LIBS})
main.cpp
#include "opencv2/opencv.hpp"
#include "medianFilter.h"
int main()
{
// input data
cv::Mat inputMat(cv::Size(128, 128), CV_8UC3, cv::Scalar(100));
cv::Mat kernelMat(cv::Size(16, 16), CV_8UC1, cv::Scalar(1));
// call CUDA
cudaMedianCaller(inputMat, kernelMat);
return 0;
}
medianFilter.cu
#include "medianFilter.h"
__global__ void kernelMedianFilter(uchar3* d_inputMat, uchar* d_kernelMat)
{
return;
}
void cudaMedianCaller(const cv::Mat& inputMat, cv::Mat& kernelMat)
{
// allocate device pointers
uchar3 *d_inputMat;
uchar *d_kernelMat;
cudaMalloc(&d_inputMat, inputMat.total() * sizeof(uchar3));
cudaMalloc(&d_kernelMat, kernelMat.total() * sizeof(uchar));
// copy from host to device
cudaMemcpy(d_inputMat, inputMat.ptr<uchar3>(0), inputMat.total() * sizeof(uchar3), cudaMemcpyHostToDevice);
cudaMemcpy(d_kernelMat, kernelMat.ptr<uchar>(0), kernelMat.total() * sizeof(uchar), cudaMemcpyHostToDevice);
// call CUDA kernel
kernelMedianFilter <<<1, 1>>> (d_inputMat, d_kernelMat);
// free
cudaFree(d_inputMat);
cudaFree(d_kernelMat);
}
medianFilter.h
#include "opencv2/opencv.hpp"
void cudaMedianCaller (const cv::Mat& inputMat, cv::Mat& kernelMat);
要运行二进制应用程序,您可能需要将一些必需的.dll
复制到二进制文件夹。对我来说,我从opencv_core343.dll
复制了C:\Program Files\OpenCV\x64\vc15\bin
到存在test_app.exe
的文件夹中。