Hi,
I have Tesla K20C card. I follow steps as below to display gray=scale image (resolution 512x512 pixels) from GPU memory:
- Read an image using OpenCV
- Write the image data to GPU memory
- Read back image data from GPU memory back to CPU memory
- Display the image in CPU by using OpenCV.
Problem Faced: The output image displayed at Step 4 is fully black.Can someone suggest the reason for this issue.
Code:
#include
#include <cuda_runtime.h>
#include <opencv2/opencv.hpp>
#include “opencv2/highgui/highgui.hpp”
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
const int width = 640, height = 480;
IplImage* img = cvLoadImage(“PIC00001.tif”, CV_LOAD_IMAGE_GRAYSCALE);
IplImage* out_img;
cvNamedWindow(“Input”);
cvShowImage(“Input”,img);
const int bytes = img->widthStep * img->height;
unsigned char *dSrc;
cudaMalloc((void **)&dSrc,bytes * sizeof(unsigned char));
cudaMemcpy(dSrc,img->imageData,bytes* sizeof(unsigned char),cudaMemcpyHostToDevice);
cudaMemcpy(out_img->imageData,dSrc,bytes* sizeof(unsigned char),cudaMemcpyDeviceToHost);
cudaFree(dSrc);
cvNamedWindow("Output");
cvShowImage("Output",out_img);
cvReleaseImage(&img);
cvWaitKey();
return 0;
}