我正在尝试使用 CUDA 创建负像,它使用与 CPU 计算相同的功能。
这是主要课程。
int main(int argc, char** argv)
{
IplImage* image_input = cvLoadImage("test.jpg", CV_LOAD_IMAGE_UNCHANGED);
IplImage* image_output = cvCreateImage(cvGetSize(image_input),
IPL_DEPTH_8U,image_input->nChannels);
unsigned char *h_out = (unsigned char*)image_output->imageData;
unsigned char *h_in = (unsigned char*)image_input->imageData;
width = image_input->width;
height = image_input->height;
widthStep = image_input->widthStep;
channels = image_input->nChannels;
negatif_parallel(h_in, h_out, width, height, widthStep, channels);
cvShowImage("Original", image_input);
cvShowImage("CPU", image_output);
waitKey(0);
cvReleaseImage(&image_input);
cvReleaseImage(&image_output);
}
这是 CUDA 类
__global__ void kernel ( unsigned char *d_in ,unsigned char* d_out, int width , int height, int widthStep, int channels) {
int x = blockIdx . x * blockDim . x + threadIdx . x ;
int y = blockIdx . y * blockDim . y + threadIdx . y ;
int s;
if( x < width && y < height){
int i = y;
int j = x;
for(int k=0;k<channels;k++){
s = d_in[i*widthStep + j*channels + k];
s = 255-d_in[i*widthStep + j*channels + k];
d_out[i*widthStep + j*channels + k]=s;
}
}
}
extern "C" void negatif_parallel( unsigned char* h_in, unsigned char* h_out, int width, int height, int widthStep,int channels){
unsigned char* d_in;
unsigned char* d_out;
cudaMalloc((void**) &d_in, width*height);
cudaMalloc((void**) &d_out, width*height);
cudaMemcpy(d_in, h_in, width*height*sizeof( unsigned char), cudaMemcpyHostToDevice);
dim3 block (16,16);
dim3 grid (width/16, height/16);
kernel<<<grid,block>>>(d_in, d_out, width, height, widthStep, channels);
cudaMemcpy(h_out, d_out, width*height*sizeof( unsigned char), cudaMemcpyDeviceToHost);
cudaFree(d_in);
cudaFree(d_out);
}
使用 CPU 计算完成后,负片图像成功。但是在使用CUDA的时候,负片不成功,只是出现了空白的白色图像。我的代码有什么问题?T_T