how to transfer image data into float in Cuda ?

hi, everyone, I am just new to Cuda.

I loaded the same image (pgm format )first by using Cuda function like below :

float * indata2 = (float*)malloc(640*480*sizeof(float));

unsigned int dataW2, dataH2;

CUT_SAFE_CALL(cutLoadPGMf("test.pgm", &indata2, &dataW2, &dataH2));

// show the image data (region: pixel x 200 to 205, pixel y 200 to 205 ) for camparing later

for (int y=200;y<205;y++) {

      for (int x=200;x<205;x++)

   printf("%.2f ", indata2[y*480+x]);

      printf("\n");

    }

result are :

   0.67 0.65 0.66 0.65 0.65

   0.40 0.38 0.34 0.35 0.47

   0.31 0.31 0.31 0.32 0.30

   0.26 0.58 0.82 0.75 0.35

   0.67 0.67 0.66 0.66 0.64

then I load the same image by using openCV and show the imagedata for the same region. And the imagedata in openCV is saved in char format. see below :

typedef struct _IplImage

{

int  nSize;         /* sizeof(IplImage) */

int  ID;            /* version (=0)*/

int  nChannels;     /* Most of OpenCV functions support 1,2,3 or 4 channels */

int  alphaChannel;  /* ignored by OpenCV */

int  depth;         /* pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U,

                        IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported */

char colorModel[4]; /* ignored by OpenCV */

char channelSeq[4]; /* ditto */

int  dataOrder;     /* 0 - interleaved color channels, 1 - separate color channels.

                        cvCreateImage can only create interleaved images */

int  origin;        /* 0 - top-left origin,

                        1 - bottom-left origin (Windows bitmaps style) */

int  align;         /* Alignment of image rows (4 or 8).

                        OpenCV ignores it and uses widthStep instead */

int  width;         /* image width in pixels */

int  height;        /* image height in pixels */

struct _IplROI *roi;/* image ROI. when it is not NULL, this specifies image region to process */

struct _IplImage *maskROI; /* must be NULL in OpenCV */

void  *imageId;     /* ditto */

struct _IplTileInfo *tileInfo; /* ditto */

int  imageSize;     /* image data size in bytes

                        (=image->height*image->widthStep

                        in case of interleaved data)*/

char *imageData;  /* pointer to aligned image data */

int  widthStep;   /* size of aligned image row in bytes */

int  BorderMode[4]; /* border completion mode, ignored by OpenCV */

int  BorderConst[4]; /* ditto */

char *imageDataOrigin; /* pointer to a very origin of image data

                          (not necessarily aligned) -

                          it is needed for correct image deallocation */

}

IplImage;

IplImage* cvimg2 = cvLoadImage( "test.pgm", 1 );

// show the imagedata loaded by opencv (the same region 200 to 205)

for (int y=200;y<205;y++) {

      for (int x=200;x<205;x++)

   printf("%d ", (uchar)(cvimg2->imageData[y*480+x]));

// here because the format of  imagedata in openCV is char so transfer to uchar here

so it can be show from 0 to 255       

      printf("\n");

    }

result are :

   136 137 137 137 136

   85 83 83 83 85

   178 174 174 174 171

   118 118 118 118 119

   135 136 136 136 137
// transfer the char or uchar into float and them show the results

float * indata = (float*)malloc(640*480*sizeof(float)); 

for (int j=0;j<640*480;j++)

  	{

  	indata[j]=((float)((uchar)(cvimg2->imageData[j])))/255;

                }

    for (int y=200;y<205;y++) {

      for (int x=200;x<205;x++)          

   printf("%.2f ", indata[y*480+x]);          

      printf("\n");

    }

result are :

   0.53 0.54 0.54 0.54 0.53

   0.33 0.33 0.33 0.33 0.33

   0.70 0.68 0.68 0.68 0.67

   0.46 0.46 0.46 0.46 0.47

   0.53 0.53 0.53 0.53 0.54

BUT why are the results is different from the red one showed before ???

Why the imagedata in uchar 0 to 255 didn’t match the imagedata in float 0.0f to 1.0f ??

how to transfer the char or uchar imagedata to the matched imagedata in float format ? So that I can handle the image with Cuda further.

PLS help ME !

After this function :
CUT_SAFE_CALL(cutLoadPGMf(“test.pgm”, &indata2, &dataW2, &dataH2));
As we known that, the imagedata is represented by 0 to 255.
why the imagedata be changed (from 0 to 255) to 0.0 to 1.0 ?

thx

indata2 is of type float.

This must be loading the char data from test.pgm into the float array indata2:

cutLoadPGMf(“test.pgm”, &indata2, &dataW2, &dataH2)

I would assume that somewhere in cutLoadPGMf, the char values are being converted to float.

Those results starting with 0.67 at the top seem wrong, and the bottom ones, starting with 0.53, seem more correct to me. I’m not certain though. I do know, however, that 136 / 255 = 0.53333…