我想把一张RGB图像(.jpg)保存为二进制文件(.bin),并使用python和c++获得相同的保存数据(在.bin文件中)。
下面是我用Python和C++将图像保存到bin文件中的代码,但我在比较两个.bin文件时得到了不同的结果。
Python
image = cv2.imread('image.jpg')
filename1 = "/image.bin" # save data as bin file
bin_file = image.astype('float32').tofile(filename1)
byte_list = []
with open(filename1, "rb") as f:
while (byte := f.read(1)):
byte_list.append(byte)
int IMAGE_SIZE = 224;
void matwrite(const string& filename, const Mat& mat)
ofstream fs(filename, fstream::binary);
// Header
int type = mat.type();
int channels = mat.channels();
fs.write((char*)&mat.rows, sizeof(int)); // rows
fs.write((char*)&mat.cols, sizeof(int)); // cols
fs.write((char*)&type, sizeof(int)); // type
fs.write((char*)&channels, sizeof(int)); // channels
// Data
if (mat.isContinuous())
fs.write(mat.ptr<char>(0), (mat.dataend - mat.datastart));
int rowsz = CV_ELEM_SIZE(type) * mat.cols;
for (int r = 0; r < mat.rows; ++r)
fs.write(mat.ptr<char>(r), rowsz);
int main()
// Save data
cv::Mat noisyImg = cv::imread("image.jpg");
//randu(m, 0, 5);
matwrite("bin_file.bin", data);
return 0 ;
我所寻找的是将c++和python中的RGB图像保存到一个二进制文件(.bin)中,用同样的方法获得保存在两个bin文件中的匹配数据(来自python和C++)。