C ++ cuda反转图像颜色
Posted
技术标签:
【中文标题】C ++ cuda反转图像颜色【英文标题】:C++ cuda invert image colors 【发布时间】:2021-03-16 13:37:38 【问题描述】:我对 cuda 编程非常陌生,我试图用 c++ cuda 和 overcv 反转图像的颜色,但我已经尽一切可能解决错误,直到我走到死胡同,请帮助。 环境设置为发布 x64,我已经包含了 lib 路径以及 cuda 和 open cv 以及链接器输入中所需的所有库。
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "C:\Users\USER\source\repos\cudatry2\cudatry2\Inversion_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 Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include\cuda_runtime.h"
#include "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include\device_launch_parameters.h"
#include "C:\Users\USER\source\repos\cudatry2\cudatry2\Inversion_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:\Users\USER\source\repos\cudatry2\cudatry2\Image_Inversion_CUDA.obj 1
Severity Code Description Project File Line Suppression State
Error LNK1120 1 unresolved externals cudatry2 C:\Users\USER\source\repos\cudatry2\x64\Release\cudatry2.exe 1
这是我遇到的错误,我不知道发生了什么
【问题讨论】:
请将错误发布为文本而不是屏幕截图 【参考方案1】:有 CUDA 库,包括在 Visual Studio 库中缺失,此外我使用调试 cuda 错误
printf("error code: %s\n",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
【讨论】:
【参考方案2】:问题是您的函数 Inversion_CUDA()、Image_Inversion_CUDA() 以 _CUDA 扩展名结尾,只需将函数名称更改为不以 _CUDA 结尾的任何名称,就可以了。
【讨论】:
以上是关于C ++ cuda反转图像颜色的主要内容,如果未能解决你的问题,请参考以下文章