繁体   English   中英

C++ cuda 反转图像 colors

[英]C++ cuda invert image colors

I am very new to cuda programming and im trying to invert colors of an image with c++ cuda and overcv but i have done everything possible solving error after error until ive reached a dead end please help. 环境设置为发布 x64,我已经包含了 lib 路径和 cuda 和 open cv 以及 linker 输入中所需的所有库。

#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   

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

有 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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM