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 ; } } } 
5
  • 2
    There are a number of problems with your code. Have you read the thrust quickstart (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 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 Commented May 28, 2013 at 15:56
  • 2
    You could also consider avoiding a functor and data extraction and just use a strided range access mechanism instead. Commented May 28, 2013 at 16:41
  • Ya thank you.. I tried strided range access mechanism in my code and it works... But is it running inside device? Commented May 29, 2013 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). Commented May 29, 2013 at 12:27
  • Ya ok thanks... My code is working fine. Commented May 30, 2013 at 5:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.