您的位置:首页 > 其它

MNIST数据集转为.jpg图片格式

2016-12-03 16:32 645 查看
从mnist官网下载下来的mnist手写数据集是二进制文件流格式的,不能直接查看,如果需要查看,需要将二进制文件转化为jpg格式,可以用各种编程语言实现,如MATLAB、Python、C++等,本文是使用C++在Ubuntu 16.04操作系统下实现的,过程中出现许多问题,现记录如下:

1、问题描述

编译时出现一下错误

no matching function for call to std::basic_ifstream<char>::(std::string&)


原因:根据提示可以看出是读取文件ifstream时出现了问题,查找资料后知道是由于不同版本c++表达不同导致的,所以用g++编译器编译时要显式制定使用那个版本c++,本程序使用c++11,编译指令如下:

g++ -o b.out -std=c++11 mnist_bin2jpg2.cpp


2、问题:

编译时出现:

对‘cv::Mat::copySize(cv::Mat const&)’未定义的引用


原因:编译的时候没有链接到库文件,这样导致函数没有定义,所以变异的时候要手动链接到库文件

g++ -o b.out -std=c++11 mnist_bin2jpg2.cpp `pkg-config opencv --cflags --libs`


3、问题:

用ifstream读取文件时编译通过了,但是一直没有成功读取文件,一开始以为是因为文件夹包含有中文的原因,然后就用了只有英文名字的文件夹,仍就没有成功,找了很久,最后发现是自己文件名打错了(哭晕在厕所。。。)

最后附上网上找的再通过修改成功将mnist转为JPG图片的代码

#include <iostream>
#include <fstream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <cstdio>
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<cv::Mat> &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_images,sizeof(number_of_images));
number_of_images = ReverseInt(number_of_images);
file.read((char*) &n_rows, sizeof(n_rows));
n_rows = ReverseInt(n_rows);
file.read((char*) &n_cols, sizeof(n_cols));
n_cols = ReverseInt(n_cols);
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<uchar>(r, c) = (int) temp;
}
}
vec.push_back(tp);
}
}
}
void read_Mnist_Label(string filename, vector<int> &vec)
{
ifstream file(filename, ios::in | 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_images,sizeof(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]);
str1 = std::string(ch1);
if (arr[i] < 10) {
str1 = "0000" + str1;
} else if (arr[i] < 100) {
str1 = "000" + str1;
} else if (arr[i] < 1000) {
str1 = "00" + str1;
} else if (arr[i] < 10000) {
str1 = "0" + str1;
}
break;
}
}
char ch2[10];
sprintf(ch2, "%d", number);
str2 = std::string(ch2);
str2 = str2 + "_" + str1;

return str2;
}
int main()
{
//reference: http://eric-yuan.me/cpp-read-mnist/ //test images and test labels
//read MNIST image into OpenCV Mat vector
string filename_test_images = "/home/linqingxiang/test/t10k-images-idx3-ubyte";
int number_of_test_images = 10000;
vector<cv::Mat> vec_test_images;
read_Mnist(filename_test_images, vec_test_images);

//read MNIST label into int vector
string filename_test_labels = "/home/linqingxiang/test/t10k-labels-idx1-ubyte";
vector<int> vec_test_labels(number_of_test_images);
read_Mnist_Label(filename_test_labels, vec_test_labels);

if (vec_test_images.size() != vec_test_labels.size()) {
cout<<"parse MNIST test file error"<<endl;
return -1;
}
//save test images
int count_digits[10];
for (int i = 0; i < 10; i++)
count_digits[i] = 0;
string save_test_images_path = "/home/linqingxiang/test/";

for (unsigned int i = 0; i <  vec_test_images.size(); i++) {
int number = vec_test_labels[i];
string image_name = GetImageName(number, count_digits);
image_name = save_test_images_path + image_name + ".jpg";
cv::imwrite(image_name, vec_test_images[i]);
}
//train images and train labels
//read MNIST image into OpenCV Mat vector
string filename_train_images = "/home/linqingxiang/test/train-images-idx3-ubyte";
int number_of_train_images = 60000;
vector<cv::Mat> vec_train_images;
read_Mnist(filename_train_images, vec_train_images);

//read MNIST label into int vector
string filename_train_labels = "/home/linqingxiang/test/train-labels-idx1-ubyte";
vector<int> vec_train_labels(number_of_train_images);
read_Mnist_Label(filename_train_labels, vec_train_labels);

if (vec_train_images.size() != vec_train_labels.size()) {
cout<<"parse MNIST train file error"<<endl;
return -1;
}
//save train images
for (int i = 0; i < 10; i++)
count_digits[i] = 0;
string save_train_images_path = "/home/linqingxiang/test/";

for (size_t i = 0; i < vec_train_images.size(); i++) {
int number = vec_train_labels[i];
string image_name = GetImageName(number, count_digits);
image_name = save_train_images_path + image_name + ".jpg";
cv::imwrite(image_name, vec_train_images[i]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: