Reputation: 16265
I have created a Mat in openCV as follows:
cv::Mat m = cv::Mat(10,10, CV_32FC1);
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
m.at<float>(i,j) = 1;
}
}
and saving it to disk by:
imwrite("out.png", m);
I am now trying to read it as follows:
cv::Mat m = imread("out.png", CV_LOAD_IMAGE_UNCHANGED);
but the data in m seems to be completely random, when accessed like m.at<float>(5,5)
for example.
How can I read back in the data that is written to it? For example, in matlab I can do:
m = imread("out.png")
and it gives me the correct matrix of 1's
Thanks
Upvotes: 1
Views: 1374
Reputation: 11367
See imwrite
The function imwrite saves the image to the specified file. The image format is chosen based on the filename extension (see imread() for the list of extensions). Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function. If the format, depth or channel order is different, use Mat::convertTo() , and cvtColor() to convert it before saving. Or, use the universal XML I/O functions to save the image to XML or YAML format.
Upvotes: 4