I'm trying to save an OpenCV mat containing image pixels to a txt file (that will be imported to a VHDL testbench afterwards for further processing).
I need the file to contain only pixel values without any other information and I need it to be in hexadecimal. Also without any separators such as commas, semicolons, etc..
what I came up with up till now is:
cv::Mat srcImage;
srcImage = imread("image.jpg", 1);
if (srcImage.empty())
{
cout << "ERROR : Image cannot be loaded..!!" << endl;
return -1;
}
cv::FileStorage file("data.txt", cv::FileStorage::WRITE);
file <<"srcImage"<<srcImage;
The output text file contains all the stuff which I don't need. I tried all the combinations in order to write the hex values but failed...
Can anyone help, please?
Thanks
You can't do this using a cv::FileStorage
. You can just save the data as you need to a file using std::ofstream
:
#include <opencv2\opencv.hpp>
#include <iostream>
#include <iomanip>
using namespace std;
using namespace cv;
int main()
{
Mat3b img(3, 4, Vec3b(15,14,12));
randu(img, Scalar(0, 0, 0), Scalar(256, 256, 256));
// Save to Hex ASCII
ofstream out("data.txt");
for (int r = 0; r < img.rows; ++r) {
for (int c = 0; c < img.cols; ++c) {
const Vec3b& v = img(r, c);
out << hex << uppercase << setw(2) << setfill('0') << int(v[0]);
out << hex << uppercase << setw(2) << setfill('0') << int(v[1]);
out << hex << uppercase << setw(2) << setfill('0') << int(v[2]);
}
}
return 0;
}