C++写入二进制文件。来自 opencv 矩阵的 uchar* 数据

发布于 2025-01-08 17:49:30 字数 1733 浏览 0 评论 0原文

我有一些数据,n 个元素,uchar* data_ptr。我如何将其写入二进制文件并稍后再次读取?

我尝试了以下方法:

std::ofstream myFile ("data.bin", std::ios::out | std::ios::binary);
myFile.write(data_ptr,100); 

以上无法编译。 uchar 不是 char。

我该怎么写呢。以及如何再次将其读入 uchar* 的内存块中。

我做了以下测试:

cv::Mat test(10,10,CV_8UC1);
cv::randu(test,0,255);
std::cout << test << std::endl;
assert(test.isContinuous());

std::ofstream myFile1 ("data1.bin", std::ios::out | std::ios::binary);
myFile1.write(reinterpret_cast<char*>(test.data),sizeof(uchar)*100);

uchar buf[100];
std::ifstream myFile ("data1.bin", std::ios::in | std::ios::binary);
myFile.read(reinterpret_cast<char*>(buf), sizeof(buf));
cv::Mat test1(10,10,CV_8UC1,buf);
std::cout << test1 << std::endl;

cv::waitKey();

并得到以下输出:

[91, 2, 79, 179, 52, 205, 236, 8, 181, 239;
  26, 248, 207, 218, 45, 183, 158, 101, 102, 18;
  118, 68, 210, 139, 198, 207, 211, 181, 162, 197;
  191, 196, 40, 7, 243, 230, 45, 6, 48, 173;
  242, 125, 175, 90, 63, 90, 22, 112, 221, 167;
  224, 113, 208, 123, 214, 35, 229, 6, 143, 138;
  98, 81, 118, 187, 167, 140, 218, 178, 23, 43;
  133, 154, 150, 76, 101, 8, 38, 238, 84, 47;
  7, 117, 246, 163, 237, 69, 129, 60, 101, 41;
  190, 50, 90, 72, 168, 109, 121, 220, 114, 248]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 127, 2, 0, 0;
  32, 0, 0, 0, 255, 255, 0, 0, 178, 116;
  36, 101, 35, 0, 28, 5, 248, 227, 87, 0;
  43, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 205, 18, 246, 118, 251, 10, 251, 118]

显然这两个矩阵不一样。哪里出了问题,有评论吗?

I have some data, n elements, uchar* data_ptr. How would I write this to a binary file and read it again later?

I tried the following :

std::ofstream myFile ("data.bin", std::ios::out | std::ios::binary);
myFile.write(data_ptr,100); 

Above won't compile. uchar not being char.

How do I write it. And how to I read it into a memory chunk of uchar* again.

I did the following test:

cv::Mat test(10,10,CV_8UC1);
cv::randu(test,0,255);
std::cout << test << std::endl;
assert(test.isContinuous());

std::ofstream myFile1 ("data1.bin", std::ios::out | std::ios::binary);
myFile1.write(reinterpret_cast<char*>(test.data),sizeof(uchar)*100);

uchar buf[100];
std::ifstream myFile ("data1.bin", std::ios::in | std::ios::binary);
myFile.read(reinterpret_cast<char*>(buf), sizeof(buf));
cv::Mat test1(10,10,CV_8UC1,buf);
std::cout << test1 << std::endl;

cv::waitKey();

And got the following output:

[91, 2, 79, 179, 52, 205, 236, 8, 181, 239;
  26, 248, 207, 218, 45, 183, 158, 101, 102, 18;
  118, 68, 210, 139, 198, 207, 211, 181, 162, 197;
  191, 196, 40, 7, 243, 230, 45, 6, 48, 173;
  242, 125, 175, 90, 63, 90, 22, 112, 221, 167;
  224, 113, 208, 123, 214, 35, 229, 6, 143, 138;
  98, 81, 118, 187, 167, 140, 218, 178, 23, 43;
  133, 154, 150, 76, 101, 8, 38, 238, 84, 47;
  7, 117, 246, 163, 237, 69, 129, 60, 101, 41;
  190, 50, 90, 72, 168, 109, 121, 220, 114, 248]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 127, 2, 0, 0;
  32, 0, 0, 0, 255, 255, 0, 0, 178, 116;
  36, 101, 35, 0, 28, 5, 248, 227, 87, 0;
  43, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 205, 18, 246, 118, 251, 10, 251, 118]

Clearly the two matrices are not the same. Any comment on where it went wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间不值得 2025-01-15 17:49:30

要写入,您只需将数据强制转换为 char*

file.write(reinterpret_cast<char*>(data_ptr), 100); // writes 100 bytes

要读取,您需要一个 istream,并且再次需要强制转换:

uchar buf[100];
input.read(reinterpret_cast<char*>(buf), sizeof(buf));

To write, you can simply cast the data to char*:

file.write(reinterpret_cast<char*>(data_ptr), 100); // writes 100 bytes

To read, you need an istream and again a cast is required:

uchar buf[100];
input.read(reinterpret_cast<char*>(buf), sizeof(buf));
挽清梦 2025-01-15 17:49:30

只需投射您的数据即可; ofstream 是基于字符的。在您的情况下,使用reinterpret_cast(data_ptr)一切都会正常。读取时,也只需转换缓冲区(并使用 ifstreamread 方法。)

Just cast your data; ofstream is character based. In your case, use reinterpret_cast<char*>(data_ptr) and everything will work. When reading, just cast the buffer as well (and use the read method of an ifstream.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文