-2

Here is the my snippet of code;

Npp8u * imageHost;  
typedef unsigned char Npp8u;
...
for (int i=0;i<nHeight;++i)
    {
        for (int j=0;j<nWidth;++j)
        {
            printf("number_befre : %u\n",imageHost[i*nWidth+j] );
            imageHost[i*nWidth+j] = imageHost[i*nWidth+j]-Npp8u(min);
            imageHost[i*nWidth+j] = imageHost[i*nWidth+j]/(max-min);
            printf("number : %u\n",imageHost[i*nWidth+j] );
        }
    }
...

Some values are assigned to max = 202 and min = 0 and extracted from imageHost. I rectified it with debugging but the content of the imageHost is 0 for whole elements. What can I do more to make these statements work with reasonable precision? Is this about the restriction of the data type that I used?

erogol
  • 13,156
  • 33
  • 101
  • 155
  • `Nppu(min)` is not valid C. Are you compiling this as C++? – Alexey Frunze Apr 20 '13 at 13:09
  • sorry code is lacking and I edited – erogol Apr 20 '13 at 13:09
  • this is cuda code actually but the host side – erogol Apr 20 '13 at 13:12
  • it is written at the explanation below of the code – erogol Apr 20 '13 at 13:13
  • What's the type of `imageHost[]` elements? What are their values? Are `max` and `min` equal to 202 and 0 respectively? – Alexey Frunze Apr 20 '13 at 13:15
  • You've said that imageHost[] is zero after this calculation but you haven't explained what values you are expecting instead. Give an example of what "number_befre : ????" is, and what you expect to see for "number : ????" – john Apr 20 '13 at 13:16
  • @AlexeyFrunze is is Npp8u and values are respectively – erogol Apr 20 '13 at 13:18
  • @john since I get the min and max from imageHost I am expecting to see some decimal numbers that are normalized between unit length. I am simply calculating (imageHost-min / max-min) – erogol Apr 20 '13 at 13:20
  • But imageHost is `Npp8u`. If you want decimal numbers you need an array of `double` (or `float`). So yes it's a restriction of the data type you are using. – john Apr 20 '13 at 13:21
  • But I need to have a Npp8u at the end. While I am searching a see the method of scaling factors. Can be a solution ? http://en.wikipedia.org/wiki/Scale_factor_%28computer_science%29 – erogol Apr 20 '13 at 13:23
  • @Erogol That is impossible. Your calculation results in a number between 0.0 and 1.0, but the only numbers that Npp8u can have are integers. So everything is going to be either 0 or 1, nothing in between. You need to rethink your problem. – john Apr 20 '13 at 13:28
  • @Erogol The link you posted is a possibility. I'll edit my answer. – john Apr 20 '13 at 13:31

3 Answers3

2

You are trying to normalize numbers to between 0.0 and 1.0. But the data type you have chosen is not suitable for that as it can only handle integers. You need code something like this

typedef unsigned char Npp8u;
Npp8u * imageHost = ...;
vector<float> normalizedImageHost(nHeight*nWidth)
...
for (int i=0;i<nHeight;++i)
{
    for (int j=0;j<nWidth;++j)
    {
        normalizedImageHost[i*nWidth+j] = (float)(imageHost[i*nWidth+j] - min)/
            (max - min);
    }
}

Another option would be to keep Npp8u but use a scaling factor. For instance we could multiply all the value by the maximum Npp8u value which is 255.

typedef unsigned char Npp8u;
Npp8u * imageHost = ...;
...
for (int i=0;i<nHeight;++i)
{
    for (int j=0;j<nWidth;++j)
    {
        imageHost[i*nWidth+j] = (Npp8u)((255.0*(imageHost[i*nWidth+j] - min))/
            (max - min));
    }
}
john
  • 85,011
  • 4
  • 57
  • 81
0

IF imageHost[i*nWidth+j] is less than max-min then

imageHost[i*nWidth+j]/(max-min)

will be zero. Because unsigned char can not store real values between [0.0 .. 1.0]

For better accuracy you should use floating-point arithmetic.

masoud
  • 55,379
  • 16
  • 141
  • 208
  • imageHost is in same type and max and min values are extracted from. – erogol Apr 20 '13 at 13:17
  • it is not possible to get 0 for all the values algebraically because of the nature of the calculation that I perform but it might be 0 due to the data type that I used and I am expecting to hear the solution not the problem. Thanks anyway – erogol Apr 20 '13 at 13:22
  • please write comments for such things – erogol Apr 20 '13 at 14:16
0

i guess your problem is imageHost memory. check its memory.(post previous lines if it possible) or do this and debug program for first line variable value (breakpoint on second line):

Npp8u  x=Npp8u(min);
imageHost[i*nWidth+j] = imageHost[i*nWidth+j]-x;

you get memory for imageHost (do this):

Npp8u ** imageHost=new *Npp8u*[nHeight];
for(int i=0;i<nHeight;i++)
     imageHost[i]=new Npp8u[nWidth];
mojtaba
  • 339
  • 1
  • 4
  • 17