Hi, I'm trying to read and write the data array of a cv::Mat only as a binary file. I saved the array like below.
FILE *file = fopen(file_name, "wb");
fread(mat.data, sizeof(char), height * width * 3, file); // RGB image
fclose(file);
Then I read it like below.
FILE *file2 = fopen(file_name, "rb");
fread(data, sizeof(char), height * width * 3, file2);
fclose(file2);
cv::Mat image(height, width, CV_8UC3, data);
It's fine when I check the result like this.
std::cout << data[height * width - 1] << image.at<cv::Vec3b>(height - 1, width - 1) << std::endl;
The last value of the "data" array and the red value of the last pixel of the cv::Mat image was the same. ex) 37[41, 32, 37] However, after I write the cv::Mat image again, the some pixel values are...destoyed like below. ex) [127, 0, 0]
cv::imwrite(path, image);
cv::Mat test = cv::imread(path);
std::cout << test.at<cv::Vec3b>((height - 1, width - 1)) << std::endl;
The most of the pixels are OK but usually some of the last pixels are destroyed. Did I something wrong? Also, I read the binary file and save it as a image file using python cv2, that was OK. What should I do in C++?