读写float32类型的cv::Mat bin文件

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);
  }
}

你可能感兴趣的:(vscode,ide,编辑器)