Reputation: 561
I save a cv::Mat as a CSV file which works fine, but when I go to load it and convert it back into a cv::Mat something is being corrupted. When I print out the contents of the cv::Mat they are different from that of the csv file. And when I show the Mat object I get a wide white window.
cv::imwrite("cameraFrame1.jpg", frame);
outFile.open("cameraFrame1.csv");
outFile << cv::format(frame, "CSV") << std::endl;
outFile.close();
...
CvMLData mlData;
mlData.read_csv("cameraFrame1.csv");
const CvMat* tmp = mlData.get_values();
cv::Mat img(tmp, true);
tmp->CvMat::~CvMat();
std::cout << "img: " << img << std::endl;
cv::namedWindow("img");
cv::imshow("img", img);
cv::waitKey(0);
Edit:
I am not at all attached to this method, if there is a better way to save an image to CSV and then read it in as such I will gladly move to that. The point of this is to save the image as raw data, most likely in a database, which could then be opened again in matlab, python, c++ etc.
Upvotes: 3
Views: 12476
Reputation: 561
I needed to set both the image depth and channels, which is the cv::Mat type in c++, as well as the dimensions
cv::imwrite("cameraFrame1.jpg", frame);
outFile.open("cameraFrame1.csv");
outFile << cv::format(frame, "CSV") << std::endl;
outFile.close();
...
CvMLData mlData;
mlData.read_csv("cameraFrame1.csv");
const CvMat* tmp = mlData.get_values();
cv::Mat img(tmp, true);
// set the image type
img.convertTo(img, CV_8UC3);
// set the image size
cv::resize(img, img, cv::Size(320, 256));
tmp->CvMat::~CvMat();
std::cout << "img: " << img << std::endl;
cv::namedWindow("img");
cv::imshow("img", img);
cv::waitKey(0);
Upvotes: 3