Reputation: 55
I am trying to convert an Image data from char to double in C++, for example
Mat img = imread(image_file);
Mat img_double;
img.convertTo(img_double, CV64FC3);
Then I think img_double should store the image data in DOUBLE, and I tried img_double.depth() it shows as 6, which is correct. However, how do I access that DOUBLE data?? img_double.data is still in uchar* type. Can anybody help me? thanks!
Upvotes: 0
Views: 5079
Reputation: 9379
C/C++ uses unsigned char
as the generic data type for a bunch of bytes.
Hence, having an uchar data pointer in cv::Mat
avoids to change the declaration of thsi pointer for each type of image, thus avoiding subclassing cv::Mat
and allowing direct data transfer via memcpy
or pointer affectation.
Casting is then used to return the data type desired by the user of the library.
Note that OpenCV 2.x provides you th etemplated function cv::Mat::ptr<type>(ìnt rowIndex)
to obtain a pointer to th ebeginning of a given row.
For example, a pointer to the beginning of row 6 of a grey image of double data type is done by:
double *rowPtr = my_mat.ptr<double>(6)
Upvotes: 1
Reputation: 18487
You can access data of img_double
in the following manner.
img_double.at<Vec3d>(i, j)[0] -> B
img_double.at<Vec3d>(i, j)[1] -> G
img_double.at<Vec3d>(i, j)[2] -> R
so to access pixel values of img
and double_img
, you can do something like this
printf("%d %d %d\n", img.at<Vec3b>(0, 0)[0], img.at<Vec3b>(0, 0)[1], img.at<Vec3b>(0, 0)[2]);
printf("%f %f %f\n", double_img.at<Vec3d>(0, 0)[0], double_img.at<Vec3d>(0, 0)[1], double_img.at<Vec3d>(0, 0)[2]);
This might be more helpful.
Upvotes: 0
Reputation: 96167
img_data is always uchar (it's bytes - that's all memory is) the image type just keeps track of what type and range you want to think of it as.
Opencv CV is/was a C library at heart so there is no way to have the data actually converted automatically to other types, but you can always just cast it.
Upvotes: 0