0

When I use cvSet2D() to set the pixel value of an image like this:

Iplimage* image=cvLoadImage("....");
CvScalar scalar(0.1415, 0, 0, 0);
cvSet2D(image, 0,0,scalar);

double pixelValue=cvGet2D(image, 0, 0).val[0];

pixelValue turns out to be 0. Why? Can anybody give me an interpretation?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
freeboy1015
  • 2,227
  • 2
  • 18
  • 15

1 Answers1

3

You are dealing with an 8 bit image (3-channel the way you load it, without any flags), which means that for each channel your values are in the range [0, 255]. So even though you assign a float value, you can only have uchar and thus your 0.1415 becomes 0.

If you had a float CvMat, then you could assign float values. For example:

CvMat* matrix = cvCreateMat(10, 10, CV_32F);
CvScalar scalar(0.1415, 0, 0, 0);
cvSet2D(matrix, 5, 5, scalar);
double pixelValue = cvGet2D(matrix, 5, 5).val[0];  // Now pixelValue = 0.1415

If you want to work with float images, check this question and this post.

Community
  • 1
  • 1
Sassa
  • 3,294
  • 2
  • 28
  • 42
  • 1
    And remember, when working with colored images `.val[0]` refers to the blue channel since OpenCV default pixel format is BGR. – karlphillip Sep 25 '12 at 17:46