I just started learning OpenCV. I want to delete one channel using CUDA kernel and then visualize how it affected the original image. But the program doesn't work, no idea why. It just shows black window :( Here is the code:
#include "opencv2\opencv.hpp"
#include <cuda.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>
#include <device_functions.h>
using namespace cv;
__global__ void imgProc(unsigned char *in, unsigned char * out)
{
int i = threadIdx.x + blockIdx.x * blockDim.x;
out[i] =in[i];
out[i+1] = in[i+1];
out[i + 2] = 0; //deleting one channel
}
int main()
{
Mat file1 = imread("sw.jpg", CV_LOAD_IMAGE_COLOR);
unsigned char *input = (unsigned char*)(file1.data);
unsigned char *dev_input, *dev_output;
unsigned char *output = (unsigned char*)malloc(file1.cols*file1.rows * 3 * sizeof(char));
cudaMalloc((void**)&dev_input, file1.cols*file1.rows * 3 * sizeof(char));
cudaMalloc((void**)&dev_output, file1.cols*file1.rows * 3 * sizeof(char));
cudaMemcpy(dev_input, input, file1.cols*file1.rows * 3 * sizeof(char), cudaMemcpyHostToDevice);
imgProc << <file1.cols, file1.rows >> > (dev_input, dev_output);
cudaMemcpy(output, dev_output, file1.cols*file1.rows * 3 * sizeof(char), cudaMemcpyDeviceToHost);
Mat file3 = Mat(file1.rows,file1.cols, CV_8UC3,output);
namedWindow("Modified", CV_WINDOW_FREERATIO);
imshow("Modified", file3);
namedWindow("Original", CV_WINDOW_FREERATIO);
imshow("Original", file1);
cudaFree(dev_input);
cudaFree(dev_output);
free(output);
waitKey();
return 0;
}