CV_8UC1 to CV_32FC1 conversion in OpenCV

c, c++, image-processing, opencv

Solution

cvConvertScale() can do what you want.

Assuming you have a cvMat* r1 of type 8UC1 and a cvMat* r2 of type 32FC1 and of the same size then

cvConvertScale(r1, r2, 1.0 / 255.0, 0.0);

Since the destination type is taken from r2, it cannot be the same cvMat as r1.

Problem

the way we convert the image type from `CV_8UC1` to `CV_32FC1` in OpenCV with C++ API is ``` r1.convertTo(r1, CV_32FC1, 1.0/255.0); ``` where `r1` is the matrix declared as `cv::Mat`. Can anybody tell me how to convert from `CV_8UC1` to `CV_32FC1` type in OpenCV with C API? If `r2` is a matrix described as `CvMat* r2`.

Original source