0

I'm translating some code developed in MATLAB to C++ using OpenCV. I'm loading an image in the RGB format like:

Mat img; 
img = imread('path to directory', CV_LOAD_IMAGE_COLOR);

I just want to convert this image to double precision as im2double() does it in MATLAB. Is there any function which allows to do it? Can I use the convertTo() function?

I tried to do:

img.convertTo(img, CV_64FC3, 255.0);

But it's not working. Could you help me?

UPDATE: Thank you for your answers. What I'm doing now is something like

int i, k;
Mat img, img_dbprec;

img = imread('path to directory', CV_LOAD_IMAGE_COLOR); //Loads the image in the RGB format
img.convertTo(img_dbprec, CV_64FC3,1.0/255.0);//Convert image to double precision
for(i=0; i<img_dbprec.rows; i++){
   for(k=0; k<img_dbprec.cols; k++){
       cout << (double) img_dbprec.data[img_dbprec.step[0]*i + img_dbprec.step[1]* k + 0] << " ";
       cout << (double) img_dbprec.data[img_dbprec.step[0]*i + img_dbprec.step[1]* k + 1] << " ";
       cout << (double) img_dbprec.data[img_dbprec.step[0]*i + img_dbprec.step[1]* k + 2] << endl;
    }
}

My output is (I will just put the final values):

...

150 150 150
21 21 21
85 85 85
214 213 213
22 22 22
22 22 22
214 213 213
85 85 85
21 21 21

which is not what I want. Can you tell me what I'm doing wrong?

Shai
  • 111,146
  • 38
  • 238
  • 371
  • 1
    can you be more specific regarding "not working"? – Shai Mar 24 '14 at 14:44
  • what happens (in terms of output) if you skip the scale factor (`1.0/255.0`) completely? what is the range of the pixels' values then? – Shai Mar 25 '14 at 08:51
  • if I skip the scale factor, I get the same values as in MATLAB (range 0-255) when I read the image. Doing this operation (convertTo), I get this values which are still in the range 0-255 instead of range 0.0-1.0 – user3455534 Mar 25 '14 at 13:28
  • so, there is no difference if you are using scale `1.0/255.0` or `1.0`? – Shai Mar 25 '14 at 14:08
  • ah no! I didn't understand your question. I tought you said to omit the convertTo function. If I change the scale factor to 1.0 I still get integer values in the range 0-255 but they are different. Now I have a lot of 0's. – user3455534 Mar 25 '14 at 14:29
  • by the way, if I choose the scale factor as 1.0/255.0, I get different values form the ones I get if I choose 1/255.0 or 1.0/255. Shouldn't be the same? – user3455534 Mar 25 '14 at 14:36
  • weird. AFAIK should be the same... how different? – Shai Mar 25 '14 at 14:38
  • @Shai, thank you for your help. Actually, the way you said is working. I was not printing the values in the correct way. Using img_dbprec.at(i,k)[j] (where i are rows, k are columns and j are channels) I get the correct values. – user3455534 Mar 25 '14 at 15:11
  • Great. I'm happy it worked for you. – Shai Mar 25 '14 at 15:16

2 Answers2

4

It seems like your scale factor is inverted. Try:

Mat dimg;
img.convertTo( dimg, CV_64FC3, 1.0/255.0 );
Shai
  • 111,146
  • 38
  • 238
  • 371
0

Your approach is correct. however, the 255.0 you have inserted, is not the max value of the RGB matrix, but a scaling factor, according to documentation. So, you probably multiply all pixel values by 255, which takes them out of the [0, 255] range. Also, (depends on the version you are using) it would be safer to create a new destination value

Mat doubleImg;
img.convertTo(doubleImage, CV_64FC3);
blue_note
  • 27,712
  • 9
  • 72
  • 90