0

I am trying to allocate space for an opencv mat ptr on my Gpu. After allocating when i make some changes in the pixel data it is not reflecting in the final output after copying back onto a host matrix. Below is my code.

    __global__ void test(Mat *m,int rows,int cols)//float *d_out,float *d_in)
{   int col = threadIdx.x;
int row = blockIdx.x;

m->data[row*3*cols+cols*3]=0;
m->data[row*3*cols+cols*3+1]=0;
m->data[row*3*cols+cols*3+2]=0;

//float f = d_in[idx];
//d_out[idx] = f*f*f;
}

 int main( )
{
   Mat m=imread("Ori/(19).jpg");

   Mat *darr;

   const size_t sz = sizeof(m);

   cudaMalloc((void**)&darr, sz);

   cudaMemcpy(darr, &m, sz, cudaMemcpyHostToDevice);




    test<<<m.rows,m.cols>>>(darr,m.rows,m.cols);

    Mat* x=new Mat(m);       

    cudaMemcpy(&x, darr, sz, cudaMemcpyDeviceToHost);

    ///the following line should give 0 as output

    cout<<(int)x->data[0]<<endl;        

    cudaFree(darr);


    return 0;
  }

I intend to black out all the pixels on the image buffer on GPU and then copy it back. But the output shows that the original matrix remains unchanged.

Aditya Raman
  • 309
  • 8
  • 19
  • 1
    although I have no experience with the combination of openCV and CUDA, cv::Mat is only the header of the image matrix, so copying the matrix header is not what you want to do. You want to copy m.data with a size of #rows*sizeOfARow (which is not always equal to the number of columns*sizeOfElement). And OpenCV has some functionality to work with CUDA, but I didnt use these yet, so I can't help there, but there might be a function to copy Matrices to GPU memory!! – Micka Mar 12 '14 at 14:23
  • 1
    Your code has quite a few issues or potential issues that I can see. This [question/answer](http://stackoverflow.com/questions/22315903/cuda-median-filter-implementation-does-not-produce-desired-results) gives a more-or-less complete example of writing a cuda kernel that operates on an OpenCV image. Any time you're having trouble with cuda code, you should start by doing [proper cuda error checking](http://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api) – Robert Crovella Mar 12 '14 at 14:36
  • 3
    I wrote a [blog post](http://www.programmerfish.com/how-to-write-a-custom-cuda-kernel-with-opencv-as-host-library/) a few months ago, which describes how to use OpenCV with custom CUDA code. An [example code](http://www.programmerfish.com/wp-content/uploads/2013/09/cuda_opencv_example.zip) is also available at the end. I recommend you take a look at it. – sgarizvi Mar 12 '14 at 20:45
  • @sgar91 Thanks to you for the awesome piece of help. – Aditya Raman Mar 13 '14 at 06:30
  • @Micka Yes I had some misconceptions. Now I am trying pointer access methods. – Aditya Raman Mar 13 '14 at 06:31

0 Answers0