1

I'm loading a .png file using OpenCV and I want to extract its blue intensity values using thrust library.

My code goes like this:

  1. Loading an image using OpenCV IplImage pointer
  2. Copying the image data into thrust::device_vector
  3. Extracting the blue intensity values from the device vector inside a structure using thrust library.

Now I have a problem in extracting Blue Intensity values from the device vector.

  • I did this code in cuda already now converting it using thrust library.
  • I fetch blue intensity values inside this function.
  • I want to know how to call this struct FetchBlueValues from the main function.

Code:

#define ImageWidth 14
#define ImageHeight 10

thrust::device_vector<int> BinaryImage(ImageWidth*ImageHeight);
thrust::device_vector<int> ImageVector(ImageWidth*ImageHeight*3);

struct FetchBlueValues
{
    __host__ __device__ void operator() ()
    {
        int index = 0 ;
        for(int i=0; i<= ImageHeight*ImageWidth*3 ; i = i+3)
        {
            BinaryImage[index]= ImageVector[i];
            index++;
        }
    }
};

void main()
{
    src = cvLoadImage("../Input/test.png", CV_LOAD_IMAGE_COLOR);

    unsigned char *raw_ptr,*out_ptr;
    raw_ptr = (unsigned char*) src->imageData;

    thrust::device_ptr<unsigned char> dev_ptr = thrust::device_malloc<unsigned char>(ImageHeight*src->widthStep);

    thrust::copy(raw_ptr,raw_ptr+(src->widthStep*ImageHeight),dev_ptr);
    int index=0;
    for(int j=0;j<ImageHeight;j++)
    {
        for(int i=0;i<ImageWidth;i++)
        {
            ImageVector[index] = (int) dev_ptr[ (j*src->widthStep) + (i*src->nChannels) + 0 ];
            ImageVector[index+1] = (int) dev_ptr[ (j*src->widthStep) + (i*src->nChannels) + 1 ];
            ImageVector[index+2] = (int) dev_ptr[ (j*src->widthStep) + (i*src->nChannels) + 2 ];

            index +=3 ;
        }
    }

}
timss
  • 9,982
  • 4
  • 34
  • 56
user1891682
  • 85
  • 1
  • 7
  • 2
    There are a number of problems with your code. Have you read the [thrust quickstart](https://github.com/thrust/thrust/wiki/Quick-Start-Guide) (as I previously suggested to you?) There are examples (such as the saxpy functor) given in there (and many other places) of using a user-defined functor in thrust code. [Here is an example](http://stackoverflow.com/questions/15989178/cuda-thrust-reduce-by-key-on-only-some-values-in-an-array-based-off-values-in/15995193#15995193) I wrote that may be along the lines of what you are trying to do. However, your functor probably doesn't need a loop in it – Robert Crovella May 28 '13 at 15:56
  • 2
    You could also consider avoiding a functor and data extraction and just use a [strided range access mechanism](https://github.com/thrust/thrust/blob/master/examples/strided_range.cu) instead. – Robert Crovella May 28 '13 at 16:41
  • Ya thank you.. I tried strided range access mechanism in my code and it works... But is it running inside device? – user1891682 May 29 '13 at 10:47
  • Yes, if you are operating on a `device_vector`, then it is running inside the device. For example, in the sample code I linked, the `thrust::fill` operation is running on the device (using the `strided_range` iterator to access specific elements). – Robert Crovella May 29 '13 at 12:27
  • Ya ok thanks... My code is working fine. – user1891682 May 30 '13 at 05:50

1 Answers1

1

Since the image is stored in pixel format, and each pixel includes distinct colors, there is a natural "stride" in accessing the individual color components of each pixel. In this case, it appears that the color components of a pixel are stored in three successive int quantities per pixel, so the access stride for a given color component would be three.

An example strided range access iterator methodology is covered here.

Robert Crovella
  • 143,785
  • 11
  • 213
  • 257