-
大小: 6KB文件类型: .cpp金币: 1下载: 0 次发布日期: 2021-05-17
- 语言: C/C++
- 标签: MNIST tensorflow
资源简介
自己改改路径,就能用,就能用就能用就能用就能用就能用就能用就能用
代码片段和文件信息
#include
#include
#include “opencv2/core/core.hpp“
#include “opencv2/highgui/highgui.hpp“
#include “opencv2/imgproc/imgproc.hpp“
using namespace std;
//英特尔处理器和其他低端机用户必须翻转头字节。
int ReverseInt(int i)
{
unsigned char ch1 ch2 ch3 ch4;
ch1 = i & 255;
ch2 = (i >> 8) & 255;
ch3 = (i >> 16) & 255;
ch4 = (i >> 24) & 255;
return((int) ch1 << 24) + ((int)ch2 << 16) + ((int)ch3 << 8) + ch4;
}
//读取训练与测试数据
void read_Mnist(string filename vector &vec)
{
ifstream file (filename ios::binary);
if (file.is_open())
{
int magic_number = 0;
int number_of_images = 0;
int n_rows = 0;
int n_cols = 0;
//从文件中读取sizeof(magic_number) 个字符到 &magic_number
file.read((char*) &magic_number sizeof(magic_number));
magic_number = ReverseInt(magic_number);
//获取训练或测试image的个数number_of_images
file.read((char*) &number_of_imagessizeof(number_of_images));
number_of_images = ReverseInt(number_of_images);
//获取训练或测试图像的高度Heigh
file.read((char*) &n_rows sizeof(n_rows));
n_rows = ReverseInt(n_rows);
//获取训练或测试图像的宽度Width
file.read((char*) &n_cols sizeof(n_cols));
n_cols = ReverseInt(n_cols);
//获取第i幅图像,保存到vec中
for(int i = 0; i < number_of_images; ++i)
{
cv::Mat tp = cv::Mat::zeros(n_rows n_cols CV_8UC1);
for(int r = 0; r < n_rows; ++r)
{
for(int c = 0; c < n_cols; ++c)
{
unsigned char temp = 0;
file.read((char*) &temp sizeof(temp));
tp.at(r c) = (int) temp;
}
}
vec.push_back(tp);
}
}
}
//读取训练与测试标签
void read_Mnist_Label(string filename vector &vec)
{
ifstream file (filename ios::binary);
if (file.is_open()) {
int magic_number = 0;
int number_of_images = 0;
int n_rows = 0;
int n_cols = 0;
file.read((char*) &magic_number sizeof(magic_number));
magic_number = ReverseInt(magic_number);
file.read((char*) &number_of_imagessizeof(number_of_images));
number_of_images = ReverseInt(number_of_images);
for(int i = 0; i < number_of_images; ++i)
{
unsigned char temp = 0;
file.read((char*) &temp sizeof(temp));
vec[i]= (int)temp;
}
}
}
string GetImageName(int number int arr[])
{
string str1 str2;
for (int i = 0; i < 10; i++) {
if (number == i) {
arr[i]++;
char ch1[10];
sprintf(ch1 “%d“ arr[i]);
- 上一篇:AD7705程序
- 下一篇:2.Newton插值公式.cpp
评论
共有 条评论