spurra
spurra

Reputation: 1025

How does OpenCV convertTo convert?

I load an image in grayscale mode into Mat image. I use image.convertTo(image, CV_32F); to convert the data type to double. I would like to convert the image into a vector<double>, so I iterate through the matrix in the following way:

    int channels = image.channels();
    int nRows = image.rows;
    int nCols = image.cols;
    vector<double> vectorizedMatrix (nRows*nCols);

    if (image.isContinuous()) {
        nCols *= nRows;
        nRows = 1;
    }



    double* pI;
    int k = 0;

    for (int i=0; i<nRows; i++) {
        pI = image.ptr<double>(i);
        for (int j=0;j<nCols;j++) {
            vectorizedMatrix.at(k) = pI[j];
            k++;
        }
    }

    return vectorizedMatrix;

When checking the data I get, I see huge values in the area of 10^10, which cannot be. Am I iterating wrongly through the matrix or does the function convertTo do something I'm not aware of?

Upvotes: 0

Views: 1819

Answers (1)

berak
berak

Reputation: 39796

"I use image.convertTo(image, CV_32F); to convert the data type to double"

no, that will convert to float. if you want double, instead use:

image.convertTo(image, CV_64F);

Upvotes: 1

Related Questions