C++ cuda invert image colors

Viewed 462

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. Environment is set to release x64, i have included the lib paths and the includes of both cuda and open cv plus all the libraries needed in the linker input.

#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;
}

this is my cpp file

#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];
    }
}

this is my cuda file

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

this is the headers file Please help i'm desperate

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   

this is the error i'm getting and i don't know what's happening

2 Answers

There were CUDA libraries and includes missing in visual studio libraries in addition I debugged the cuda error using

printf("error code: %s\n",cudaGetErrorString(cudaGetLastError()));

and in project properties -> CUDA C/C++ -> device -> code generation, I had to add the following:

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

The problem is your functions Inversion_CUDA(), Image_Inversion_CUDA() ends with _CUDA extension just change the function names to anything which doesn't end with _CUDA and you will be fine.

Related