// opencv_bp_test.cpp: 定义控制台应用程序的入口点。
//vs2017
//资源链接
//https://download.csdn.net/download/hao_san_520/10754444
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <opencv2/opencv.hpp>
#include <vector>
#include <array>
#include <cstdlib>
using namespace std;
using namespace cv;
bool Mat_save_by_binary(cv::Mat image, string filename);
bool Mat_save_by_binary(vector<Mat> Mat_vect, string filename);
bool Mat_read_binary(cv::Mat &image, string filename);
void pre_deal(string filename)//opencv图像处理
{
vector<Mat> data;
ifstream readflie(filename);
string label, name, line;
stringstream buffer;
while (!readflie.good())
{
string x = "error !!!!1";
throw runtime_error(x);
}
while (readflie.good())
{
getline(readflie, line);
buffer << line;
buffer >> name;
buffer >> label;
buffer.seekg(0, ios::end);
Mat img = imread(name, 0)/255;
data.push_back(img);
}
Mat_save_by_binary(data, filename + ".binary");
readflie.close();
}
bool Mat_save_by_binary(vector<Mat> Mat_vect, string filename)//整体写入
{
if (filename.empty()) return false;
const char * filenamechar = filename.c_str();
FILE *fpw;
errno_t er = fopen_s(&fpw, filenamechar, "ab");
if (fpw == NULL)
{
fclose(fpw);
return false;
}
for (int j = 0; j < Mat_vect.size(); j++)
{
cout << j<<endl;
int channl = Mat_vect[j].channels();
int rows = Mat_vect[j].rows;
int cols = Mat_vect[j].cols;
fwrite(&channl, sizeof(char), 1, fpw);
fwrite(&rows, sizeof(char), 4, fpw);
fwrite(&rows, sizeof(char), 4, fpw);
char *bp = (char *)Mat_vect[j].data;
if (channl == 3)
{
for (int i = 0; i < rows * cols; i++)
{
fwrite(&bp[i * 3], 1, 1, fpw);
fwrite(&bp[i * 3 + 1], 1, 1, fpw);
fwrite(&bp[i * 3 + 2], 1, 1, fpw);
}
}
else if (channl == 1)
{
for (int i = 0; i < rows * cols; i++)
{
fwrite(&bp[i], 1, 1, fpw);
}
}
else
return false;
}
fclose(fpw);
return true;
}
bool Mat_save_by_binary(cv::Mat image, string filename)//单个写入
{
if (filename.empty()) return false;
const char * filenamechar = filename.c_str();
FILE *fpw;
errno_t er = fopen_s(&fpw, filenamechar, "ab");
if (fpw == NULL)
{
fclose(fpw);
return false;
}
int channl = image.channels();
int rows = image.rows;
int cols = image.cols;
fwrite(&channl, sizeof(char), 1, fpw);
fwrite(&rows, sizeof(char), 4, fpw);
fwrite(&rows, sizeof(char), 4, fpw);
char *bp = (char *)image.data;
if (channl == 3)
{
for (int i = 0; i < rows * cols; i++)
{
fwrite(&bp[i * 3], 1, 1, fpw);
fwrite(&bp[i * 3 + 1], 1, 1, fpw);
fwrite(&bp[i * 3 + 2], 1, 1, fpw);
}
}
else if(channl == 1)
{
for (int i = 0; i < rows * cols; i++)
{
fwrite(&bp[i], 1, 1, fpw);
}
}
else
return false;
fclose(fpw);
return true;
}
bool Mat_read_binary(vector<Mat> &img_vec, string filename)//整体读出
{
if (filename.empty())
return false;
const char * filenamechar = filename.c_str();
FILE * fpr;
errno_t er = fopen_s(&fpr, filenamechar, "rb");
if (fpr == NULL)
{
fclose(fpr);
return false;
}
int channl(0);
int rows(0);
int cols(0);
fread(&channl, 1, 1, fpr);
fread(&rows, 1, 4, fpr);
fread(&cols, 1, 4, fpr);
while (!feof(fpr))
{
if (channl == 3)
{
Mat image = Mat::zeros(rows, cols, CV_8UC3);
char *bp = (char *)image.data;
for (int i = 0; i < rows * cols; i++)
{
fread(&bp[i * 3], 1, 1, fpr);
fread(&bp[i * 3 + 1], 1, 1, fpr);
fread(&bp[i * 3 + 2], 1, 1, fpr);
}
img_vec.push_back(image);
}
else if (channl == 1)
{
Mat image = Mat::zeros(rows, cols, CV_8UC1);
char *bp = (char *)image.data;
for (int i = 0; i < rows * cols; i++)
{
fread(&bp[i], 1, 1, fpr);
}
img_vec.push_back(image);
}
else
return false;
}
fclose(fpr);
return true;
}
bool Mat_read_binary(cv::Mat &image, string filename, long int offset)//单个读出
{
if (filename.empty())
return false;
const char * filenamechar = filename.c_str();
FILE * fpr;
errno_t er = fopen_s(&fpr, filenamechar, "rb");
if (fpr == NULL)
{
fclose(fpr);
return false;
}
int channl(0);
int rows(0);
int cols(0);
fread(&channl, 1, 1, fpr);
fread(&rows, 1, 4, fpr);
fread(&cols, 1, 4, fpr);
offset *= rows * cols + 9;
fseek(fpr, offset, SEEK_SET);
if (channl == 3)
{
image = Mat::zeros(rows, cols, CV_8UC3);
char *bp = (char *)image.data;
for (int i = 0; i < rows * cols; i++)
{
fread(&bp[i * 3], 1, 1, fpr);
fread(&bp[i * 3 + 1], 1, 1, fpr);
fread(&bp[i * 3 + 2], 1, 1, fpr);
}
}
else if (channl == 1)
{
image = Mat::zeros(rows, cols, CV_8UC1);
char *bp = (char *)image.data;
for (int i = 0; i < rows * cols; i++)
{
fread(&bp[i], 1, 1, fpr);
}
}
else
return false;
fclose(fpr);
return true;
}
int main()//主函数
{
//pre_deal("test.txt");
vector<Mat> x;
Mat img;
long int i(0);
cout<<Mat_read_binary(x, string("test.txt.binary"))<<false;
for (int i = 0; i < x.size(); i++)
{
cout << x[i] << endl << i << endl;
cout << "^(* ̄(oo) ̄)^" << endl;
}
}