C++: Load Raw Data and Convert to Mat
作者:XD / 发表: 2022年9月24日 06:01 / 更新: 2022年9月24日 06:03 / 编程笔记 / 阅读量:1332
C++: Load Raw Data and Convert to Float Pointer
#include < fstream>
#include < iostream>
#include < opencv.hpp>
using namespace std;
using namespace cv;
int main() {
string file_path = "./test.raw";
ifstream fin;
fin.open(file_path, std::ios::binary);
if (!fin) {
cerr << "open failed: " << file_path << endl;
return -1;
}
fin.seekg(0, fin.end);
int length = fin.tellg();
fin.seekg(0, fin.beg);
char* buffer = new char[length];
fin.read(buffer, length);
// convert to float pointer
float *tmp = (float *)buffer;
// convert to mat
int rows = 32;
int cols = 128;
Mat m(rows, cols, CV_8UC3);
Vec3b p;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
p[0] = tmp[0 * rows * cols + i * cols + j];
p[1] = tmp[1 * rows * cols + i * cols + j];
p[2] = tmp[2 * rows * cols + i * cols + j];
m.at(i,j) = p;
}
}
cout << "============M:=================" << endl << m << endl;
return 0;
}