C++库会反转图像颜色



我对cuda编程很陌生,我试图用c++cuda和overcv反转图像的颜色,但我已经尽了一切可能解决一个又一个错误,直到我走到了死胡同。请帮忙。环境设置为x64版本,我已经包含了库路径和cuda和open cv的包含,以及链接器输入中所需的所有库。

#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "C:UsersUSERsourcereposcudatry2cudatry2Inversion_CUDA.h"
using namespace std;
using namespace cv;
int main() {
Mat Input_Image = imread("C:\Users\USER\source\repos\cudatry2\cudatry2\Test_Image.png");
cout << "Height: " << Input_Image.rows << ", Width: " << Input_Image.rows << ", Channels: " << Input_Image.channels() << endl;
Image_Inversion_CUDA(Input_Image.data, Input_Image.rows, Input_Image.rows, Input_Image.channels());
imwrite("Inverted_Image.png", Input_Image);
system("pause");
return 0;
}

这是我的cpp文件

#include "C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.1includecuda_runtime.h"
#include "C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.1includedevice_launch_parameters.h"
#include "C:UsersUSERsourcereposcudatry2cudatry2Inversion_CUDA.h"
__global__ void Inversion_CUDA(unsigned char* Image, int Channels);
void Image_Inversion_CUDA(unsigned char* Input_Image, int Height, int Width, int Channels){
unsigned char* Dev_Input_Image = NULL;
//allocate the memory in gpu
cudaMalloc((void**)&Dev_Input_Image, Height * Width * Channels);
//copy data from CPU to GPU
cudaMemcpy(Dev_Input_Image, Input_Image, Height * Width * Channels, cudaMemcpyHostToDevice);
dim3 Grid_Image(Width, Height);
Inversion_CUDA << <Grid_Image, 1 >> >(Dev_Input_Image, Channels);
//copy processed data back to cpu from gpu
cudaMemcpy(Input_Image, Dev_Input_Image, Height * Width * Channels, cudaMemcpyDeviceToHost);
//free gpu mempry
cudaFree(Dev_Input_Image);
}
__global__ void Inversion_CUDA(unsigned char* Image, int Channels){
int x = blockIdx.x;
int y = blockIdx.y;
int idx = (x + y * gridDim.x) * Channels;
for (int i = 0; i < Channels; i++){
Image[idx + i] = 255 - Image[idx + i];
}
}

这是我的cuda文件

#ifndef _Inversion_CUDA_
#define _Inversion_CUDA_
void Image_Inversion_CUDA(unsigned char* Input_Image, int Height, int Width, int Channels);
#endif

这是头文件请帮帮我,我绝望了

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2001 unresolved external symbol "void __cdecl Image_Inversion_CUDA(unsigned char *,int,int,int)" (?Image_Inversion_CUDA@@YAXPEAEHHH@Z)   cudatry2    C:UsersUSERsourcereposcudatry2cudatry2Image_Inversion_CUDA.obj   1   
Severity    Code    Description Project File    Line    Suppression State
Error   LNK1120 1 unresolved externals  cudatry2    C:UsersUSERsourcereposcudatry2x64Releasecudatry2.exe    1   

这就是我遇到的错误,我不知道发生了什么

visual studio库中缺少CUDA库和includes。此外,我使用调试了CUDA错误

printf("error code: %sn",cudaGetErrorString(cudaGetLastError()));

并且在项目属性中->CUDA C/C++->设备->代码生成,我不得不添加以下内容:

compute_52,sm_52;compute_35,sm_35;compute_37,sm_37;compute_50,sm_50;compute_60,sm_60;compute_61,sm_61;compute_70,sm_70;compute_75,sm_75;compute_80,sm_80

问题是您的函数Inversion_CUDA((,Image_Inversion_UDA((以_CUDA结尾。扩展只需将函数名称更改为任何不以_CUDA结尾的名称,您就可以了。