读写float32类型的cv::Mat bin文件
  kyUAQhuCVsdE 2023年11月02日 39 0


void read_float32_bin(const char* filePath, cv::Mat& mat) {
  FILE* pFile = fopen(filePath, "rb");
  if (pFile) {
    int mat_bytes = mat.cols * mat.rows * mat.channels();
    float* mat_data = (float*)(mat.data);
    for (int i = 0; i < mat_bytes; i++) {
      float value = 0.f;
      fread(&value, 4, 1, pFile);
      *(mat_data + i) = (float)value;
    }
    fclose(pFile);
  } else {
    printf("cannot open file %s\n", filePath);
  }
}

void write_float32_bin(const char* filePath, const cv::Mat& mat) {
  FILE* pFile = fopen(filePath, "wb");
  if (pFile) {
    int mat_bytes = mat.cols * mat.rows * mat.channels();
    float* mat_data = (float*)(mat.data);
    for (int i = 0; i < mat_bytes; i++) {
      fwrite((mat_data + i), 4, 1, pFile);
    }
    fclose(pFile);
  } else {
    printf("can not write file %s\n", filePath);
  }
}


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  anLrwkgbyYZS   2023年12月30日   28   0   0 i++iosi++ioscici
  anLrwkgbyYZS   2023年12月30日   32   0   0 ideciciMaxideMax
kyUAQhuCVsdE